Python 练习册 => 0003 将 0001 题生成的 200 个激活码保存到 Redis 非关系型数据库中

心得

  • 安装redis-py:pip install redis
  • redis 模块的 api 文档
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import redis
import uuid
import pprint


def main():
    r = redis.StrictRedis(host='localhost', port=6379, db=0)
    code_list = get_activation_code_list()
    # 使用 redis 的集合
    r.delete('ActivationCodes')  # 先清空
    r.sadd('ActivationCodes', *code_list)  # 再插入
    # result 是一个包含所有查询数据的 set
    result = r.smembers('ActivationCodes')
    pprint.pprint(result)


def get_activation_code_list():
    result = []
    for i in range(200):
        result.append(str(uuid.uuid4()))
    return result


if __name__ == "__main__":
    main()

运行结果:

➜  0003 git:(master) ✗ ./answer.py
{b'008ad5f2-7558-4d15-b876-6184eb46b978',
 b'0122ea37-49ce-49be-8bad-b7ca9113d639',
 b'02fe066b-739d-4d88-80f5-1b7b09818865',
 b'03876cc2-5af4-494e-8334-86b0bb149618',
 b'04d0db96-274e-4741-84bd-8ac966c740e9',
 b'04efb0f1-9255-4038-8e89-74155d505c9a',
 b'05761870-8a3c-443d-a90e-6b75eeeecfbc',
 b'05c86297-00a9-4875-92ad-1a79933e51c3',
 b'06116c87-3387-4185-80be-09e88e707f6e',
 b'067c7880-139a-4ba6-bb35-eae3532b5aaa',
 b'07ae1ed9-fc8a-4b04-810c-fc491897f2f3',
 b'088fc265-0aa2-4828-bc94-455399bac115',
 b'0927ac3f-cad9-4f6a-b66f-62af1a3652d4',
 b'0b0f0f94-6622-4c0e-b256-a818f73dec2d',
 b'0b968c8f-9d54-4cc9-a5b2-a1a93884fede',
 b'0c5a9ec0-52c8-4e71-b79b-a74151fa8781',
 b'0c8b868a-341a-4283-974c-0f96c34374d1',
 b'0dc7cadb-d180-4f5e-b651-14e30de520ba',
 b'0f8c57c0-c1f4-403f-ad2e-e9e406d4dc81',
 b'1126eafc-2f01-4cbf-b543-0f98a6bb417a',
 b'1168c49a-8d1d-40d1-84ae-f06ed87b9348',
 b'12973704-dcea-4d4a-ab97-de786c40fd5c',
 b'129e093c-2457-46ce-be34-06889970ee4e',
 b'1404c485-5e0e-4f09-abd3-f50d8f774f3d',
 b'14df4793-d744-4eb3-84ec-04fa8dcd508b',
 b'175e6d75-7640-44b2-9582-4b7dae1d5434',
 b'186fda19-2aef-43b0-ab81-0bcafe31dddb',
 b'18953768-8d27-456f-b712-02bf69775336',
 b'1987edd1-fecb-41e1-b117-bdbbbfd96127',
 b'19de16f4-ab5a-4c7e-ac7f-ed6413c80a7d',
 b'1b56e4a4-321c-4270-be32-2c267c8b4fc3',
 b'1ba339ac-472d-4992-9235-0829a6eb1720',
 b'1f339410-d9a1-4f48-8b9c-766309387b1c',
 b'21da6e3a-536e-410c-9cb8-18026f3533d8',
 b'23500036-1638-4273-a2ea-b02cc9d5938b',
 b'249daf1e-f1dd-43ec-949a-3aa824f11f2b',
 b'24b7b3cd-2d0e-4959-8aa6-d7930425c9f1',
 b'24c9bf36-d1ff-4e7b-bd21-b437202271d3',
 b'2592b15c-f093-44af-a4cb-67c968a89151',
 b'29164fe7-35b8-4d8d-b0bf-dd6f2a7b40ca',
 b'2a600d1a-65b6-4144-8009-577652f0b8b3',
 b'2ab6131b-f738-4088-be37-560431616447',
 b'2d29cbdc-309c-4d64-9203-e52175a62796',
 b'2d667fc2-146e-49bb-887f-4d17afda424f',
 b'301ce2ac-f4cf-49f6-bafc-f0e699418806',
 b'306e8b76-c607-4341-8d66-c9ad4be8320c',
 b'3205559a-69b9-4975-83a9-9173da50c318',
 b'3415209b-9cb7-485c-9c1d-1561565fb9a3',
 b'36597f9f-7b67-48e4-9edc-e582ac6851ae',
 b'3675b1d0-df22-4abd-8604-4fde14b19761',
 b'38037d7c-2de3-4b42-81b1-7421ba207588',
 b'39495e7d-9368-4ffd-9473-a442540611da',
 b'3a721426-9b09-4c59-ae5c-9fbcd3e5d13b',
 b'3ad1741f-e97e-4eac-9504-4efb0a775388',
 b'3ca57b2d-62dc-495a-a063-d0fdedd635bd',
 b'3e027857-c697-4f9b-87cc-2930f51a8129',
 b'3f1093ce-85c9-42b7-87b6-cd769295a87f',
 b'4060a0a2-d1c8-41f6-8449-6743e2bc891a',
 b'40e5ac05-8fdb-4d61-bf57-3edd06d3a45d',
 b'40fa8666-b3d0-49be-a55b-d25b6aceb387',
 b'415704d2-4258-44e3-8ad7-ef0a01337139',
 b'4215601d-f821-43d6-902d-d186d4f630a6',
 b'427f7524-675a-4387-b472-9662d13e5552',
 b'42822ad0-2632-4fa3-a147-fa1a98559ee3',
 b'434725bd-907a-4f0d-9dc1-88acae694457',
 b'460b12e5-5aaf-4969-865d-85fcc1243125',
 b'48594b8b-5728-48a2-a2f8-6d61e290a4e8',
 b'493ea1f9-9b88-4dd2-90f1-31076980f65b',
 b'4b673ea6-ed60-4b9a-9cbf-aec966d59898',
 b'4e7b10be-d046-4dd7-a3a5-0fbd84f5ef41',
 b'4fcafe74-dcb3-433e-98c7-045fe69d2558',
 b'51f210bf-668d-4641-8c7d-00b68385f291',
 b'5233106f-1ecd-4af9-aa6c-4c0aeaf68dac',
 b'52d38f25-cb79-4947-8a1e-65859c958412',
 b'53453fc1-456b-488b-8d2f-d4ae234afb7b',
 b'53aa8bf8-6d85-498b-b686-18e6a5e893e4',
 b'5737073a-2dc6-453d-8038-0afb09cf11e9',
 b'57d950a7-367f-4b5e-a03e-0bb89b73a750',
 b'5a3254e4-c20c-46e9-954a-e57b7a316069',
 b'5c3a2a8d-9ce7-435c-ad1a-9979126a6fdb',
 b'5cd0d0b2-d5db-4db3-be09-624ff0dd50be',
 b'5f6cf0c0-e779-46dc-b0e1-1aa98b18727c',
 b'60d4ea98-75b0-4b27-9bb3-26d7d9e3b024',
 b'60dc430e-3e4b-4356-bb34-d1cee4a586b0',
 b'61a67147-9536-4d90-b7ec-57f5308aa304',
 b'62c4840d-c3b9-419c-b885-128cb869aff7',
 b'631904d6-c05e-49d3-9816-f9eb9fed56df',
 b'644f8c50-3b82-4b73-bdbc-b467dd22d936',
 b'64c0eafe-9aef-46a8-aaf8-ede25332611b',
 b'65a34e69-8e27-4cb3-8808-c826b5c30b59',
 b'6688a809-e903-476f-b9fe-55a55b34b0e9',
 b'6a0f98ba-032b-4a6d-b251-5cf7f938becf',
 b'6a46bf38-7232-4dd9-9609-50c1d965d54b',
 b'6b268d0d-fc4c-4c68-80ea-35d5d09db5a9',
 b'6cb49e7e-2839-47b5-a251-03e75888ed91',
 b'6cbf598d-dd8e-4c64-adfe-dd03431135ee',
 b'6e68e167-70da-41b9-9366-4cd4c61ee576',
 b'6e90b5aa-1b15-4e0c-99c0-6e28c0e52b2f',
 b'6ee28ec8-cada-418a-a47a-e7bcfe23ea45',
 b'6fdd61ae-091d-450e-a130-53e08a2f4fac',
 b'74162dbc-a2e6-493f-9bc7-e6f999e48c73',
 b'783331ec-9eab-4350-b360-681465c4f9e4',
 b'7ae0a23e-2aa7-4cc2-9df0-3f4d6e88c8e7',
 b'7af96039-c2d6-4262-845e-b206fd19a260',
 b'7b323a13-890a-4fa5-a492-5d2008cf478f',
 b'7bd1f75f-4588-476c-bf01-b366cb421ba0',
 b'7bdccd2f-1609-40b8-bb77-8ab7e957baa9',
 b'7e429484-14f7-4cc2-8abc-d94f6e15f525',
 b'7f62253b-136c-4cae-a6b9-bfb147357047',
 b'7f985513-2d1a-42c9-a821-2e54559da2b1',
 b'8043c15a-c9bc-466d-a728-72d49673567d',
 b'84783828-b310-44b8-aaf6-5d4be52de2be',
 b'85f8c46d-4ff4-48a2-96d7-a3fa6ec9ca16',
 b'88a866a5-c4d3-4f92-a93c-be037e0522ac',
 b'8a08018c-2ce0-4ae9-9fb8-99f95a2dedad',
 b'8d528d81-1c38-440a-ad99-ef36cd625bcd',
 b'8edfbbd6-4009-41c2-b8d7-4ce5333ca611',
 b'8fb2fedd-7e4d-42e2-be69-430925d33839',
 b'924ea55c-2f63-4972-81a8-69a2514036bd',
 b'94195228-d790-44e1-a6e6-f9bbd4e6b316',
 b'9432083f-6cec-40f9-8b58-20e448ee56e5',
 b'9689a960-0b16-4e1c-8407-11ce03b358b9',
 b'96fea5ec-ae91-4ff4-81ad-6577b173165e',
 b'98fd45e4-89d0-4ff7-9b2d-114e75812a41',
 b'9ccfaacd-ab0c-40cc-a570-cd535dc452f6',
 b'9db45d97-285e-4004-ad09-c0e01b413af1',
 b'9e0d2109-cd3b-40b0-aa27-16b50ef47b4b',
 b'9f304609-4165-4c16-b342-8839b41c20bf',
 b'a0f58388-7b9a-4578-aa59-3dc81a51582b',
 b'a16c6e8d-1d4d-4e5c-9fe2-e1ba4fdb89e8',
 b'a1fabce6-3a42-44ee-8ec4-67e465e03043',
 b'a570073d-a166-4402-821a-e749ecb6667a',
 b'a98270c9-378b-4ea0-b38c-004f7f0e7751',
 b'aa6a32a1-3351-47e5-a887-1fab9e66b8f2',
 b'abb02f90-07c8-4e4e-864f-5299a55fa90c',
 b'ac0eafab-342e-48e7-bd16-136e0c4d41b7',
 b'acb7abe4-0911-47fa-a8e0-58d4bab46e5f',
 b'ae236aa9-1c5a-4a81-bf45-601978208118',
 b'ae3a781f-b3c1-46cc-92b8-7d3a71e28963',
 b'ae3b2f95-3bc5-4fb6-917a-2fd935e4bc8e',
 b'ae7f8a4e-e14f-4203-b93f-60e785e029c2',
 b'af5a0ff9-3fd7-423f-b032-ac78e031b1fb',
 b'b1d24428-40f2-415c-bfdc-a736a81b226a',
 b'b21d2064-8a29-409f-a0b7-da92af5ae633',
 b'b25ae3fe-1b8c-4057-826e-da045acde33c',
 b'b46edba1-fd1b-47df-8698-2ccc02514ff7',
 b'b510673c-8c49-4f38-afe5-25f1837b8d6e',
 b'b64ce277-7509-4e13-8004-493594f35374',
 b'b80cb0db-d1dd-48e0-910e-5a8ca5dd3445',
 b'b8d9a2cd-cefe-4c82-a9ca-a161edabf7a4',
 b'bc16cd17-47ad-4383-afc0-4e990916163d',
 b'bdd12d0a-1aa8-4535-8c32-db1389bafc9b',
 b'c0a228a5-dc87-4d27-8b60-f745ca6591e2',
 b'c226d690-602e-42f7-a9c6-2627a0ce216e',
 b'c30a843c-0034-4a30-9a01-e6dbc28771c0',
 b'c45c2e81-d446-436a-9658-44cfc2b168aa',
 b'c46351da-5e2c-4ded-9bd3-4c38ab44ab6f',
 b'c5ee91c0-f1ac-4082-a1e3-7c7f6a9df709',
 b'c8941c04-3cb6-430e-93e4-0955f84620a7',
 b'c8bec4a6-6f05-4dcb-b7c6-96765f225442',
 b'c9b7c7c5-29b7-4678-b066-991ab72c3484',
 b'cacc9134-9e86-450c-9a25-421e893b0eb2',
 b'ccf12616-1791-459c-b26d-ccf453177514',
 b'cdc09c36-db0a-483d-9c91-43ff8f422fe5',
 b'cdd83cca-311d-418e-bc48-471834961107',
 b'ce13c31a-7e01-46fe-93f6-e01335ddbbc8',
 b'ceb3727a-5959-42a3-a702-ee91ebe36f94',
 b'd01026dc-2a45-4bfd-b2ed-b05ab7bd12d7',
 b'd126185d-3765-4aa5-9f5e-4eedbe9944df',
 b'd1417572-7cd6-4c01-a367-bda8696cc4b8',
 b'd1bae2c1-6e06-4ec1-b547-7b24db156f9d',
 b'd2af0102-266c-42b7-8bd7-fe1971d2fcfc',
 b'd4ffea27-3135-4c3a-92f8-60ffed6862b1',
 b'd731c946-0494-4191-b1a9-bf0523926992',
 b'd7a829ba-4ebe-4fdd-83a1-dbdc664081c5',
 b'd814236f-fa58-4e3c-a568-656f120726d9',
 b'd86d0382-d1c1-4bf1-b86f-e6e9f1f4c7e4',
 b'de98d05b-f922-434d-bd5a-f68187db2baf',
 b'df5391d5-3312-4022-a229-9c415e41a0a2',
 b'e06e437d-e66b-497d-ad92-031dd1275dd6',
 b'e20b68de-1ab2-48b2-933f-bb258a3a8bab',
 b'e26e31e0-3f4b-4f94-b5d0-6d7eafa27404',
 b'e2f75ef6-93fb-4751-ab21-14e7bf059715',
 b'e43f354f-c9fb-46b5-b891-cfbf2df94b5a',
 b'e59e025e-eaf2-475d-ad09-6ac683358051',
 b'e7492d65-7dcb-462f-bf08-82d5ef5d0f0b',
 b'e89a7a56-1af1-44b3-a811-fcfc9ef62861',
 b'ed51ce20-14ce-41e6-9393-2b95ce8b9127',
 b'f1b8490a-fc83-4393-bef8-434a5ed2f959',
 b'f1d394a6-e8cf-4119-94dc-4cac53f72cdd',
 b'f2a07a56-08cd-4c91-b68d-472b93c601a5',
 b'f2fce424-b95a-4371-a17d-5dda81b27d62',
 b'f39a0fe1-5f64-47ba-8bfe-826f5a5dacaa',
 b'f5aee920-4aa3-4813-917e-2f82db08cc3b',
 b'f699d694-f290-4aed-9e0c-82bb6ea943f4',
 b'f6dd0c20-9441-40ee-a8c5-1c921244f6a2',
 b'f8ce6614-4b1f-4cb8-9b11-54b3d46917af',
 b'fa44a13e-06d4-4beb-8290-37f462562bc5',
 b'fb56efaf-3ffa-4c23-b713-388c647c764a',
 b'fca483ab-cda9-4dd4-9111-fa7344f09425'}