做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)?

在此生成由数字,字母组成的20位字符串

uuid模块

import uuid

def get_id():
file_object = open('uudi.txt','w+')
for i in range(200):
ID = str(uuid.uuid1()) + '\n'
file_object.write("ID"+str(i+1)+":"+ID)
file_object.close() if __name__ == '__main__':
get_id()

random模块

# coding:utf-8
# python3环境
# 通过多线程来生成200个激活码
import random
import string
import threading def get_str():
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 20))
return ran_str class myThread (threading.Thread):
def __init__(self, threadID, name, counter):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print ("开始线程:" + self.name)
write_id(self.name, 40)
print ("退出线程:" + self.name) def write_id(threadName, counter):
file_object=open('id.txt','a+')
while counter:
file_object.write("%s: %s\n" % (threadName+str(counter), get_str()))
counter -= 1
file_object.close() # 创建新线程
thread1 = myThread(1, "Group1-", 1 )
thread2 = myThread(2, "Group2-", 2)
thread3 = myThread(3, "Group3-", 3)
thread4 = myThread(4, "Group4-", 4)
thread5 = myThread(5, "Group5-", 5) # 开启新线程
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start() thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join() print ("退出主线程")

参考链接

https://docs.python.org/3/library/random.html

https://www.runoob.com/python/func-number-random.html

https://www.runoob.com/python/python-files-io.html

https://docs.python.org/3.6/library/uuid.html

Python每日一题 002的更多相关文章

  1. Python:每日一题002

    题目: 企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万 ...

  2. Python每日一题 003

    将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...

  3. Python每日一题 004

    将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...

  4. Python每日一题 009

    题目 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. 代码 参照网络上代码 # coding: utf-8 import os import re # ...

  5. Python每日一题 008

    题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...

  6. Python每日一题 007

    题目 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词. 很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出 ...

  7. Python每日一题 006

    题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...

  8. Python每日一题 005

    任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...

  9. Python每日一题 001

    Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...

随机推荐

  1. 双目立体匹配经典算法之Semi-Global Matching(SGM)概述:匹配代价计算之互信息(Mutual Information,MI)

      半全局立体匹配算法Semi-Global Matching,SGM由学者Hirschmüller在2005年所提出1,提出的背景是一方面高效率的局部算法由于所基于的局部窗口视差相同的假设在很多情况 ...

  2. laravel定义全局变量

    laravel中config()函数可以获取 bootstrap/cache/config.php中的内容,而config文件夹下的所有配置文件夹中的内容可以通过  php artisan confi ...

  3. CPython,PyPy?Python和这两个东西有什么关系

    https://blog.csdn.net/fu6543210/article/details/90770794 python是一种编程语言.但这种语言有多种实现,而且与其他语言不同,python并没 ...

  4. Python List reverse() 与list[::-1]

    reverse() https://www.runoob.com/python/att-list-reverse.html list[::-1] https://blog.csdn.net/usern ...

  5. Linux(四)—— 项目部署与ansible自动化部署

    目录 项目部署与ansible自动化部署 一.项目部署 二.ansible自动化部署(python自动化运维) 1.安装ansible 2.ansible例子 3.ansible自动化部署nginx ...

  6. GNU Linux 64汇编学习

    函数调用传参: 第一个参数:rdi, 第二个参数:rsi 函数调用栈结构: 返回值 第一个参数 第二个参数 +----------+ rsp-24 | a | +----------+ rsp-16 ...

  7. 一步一步学Vue(六)https://www.cnblogs.com/Johnzhang/p/7242640.html

    一步一步学Vue(六):https://www.cnblogs.com/Johnzhang/p/7237065.html  路由 一步一步学Vue(七):https://www.cnblogs.com ...

  8. 关于软件IntelliJ IDEA的使用技巧(一)

    一,IntelliJ IDEA的下载 点击网址http://www.jetbrains.com/idea/进入官网,点击Download 会出现如下页面 点击Ultimate下的Download,下载 ...

  9. iView + vue-quill-editor 实现一个富文本编辑器(包含图片,视频上传)

    1. 引入插件(注意IE10以下不支持) npm install vue-quill-editor --savenpm install quill --save (Vue-Quill-Editor需要 ...

  10. sudo之后出现“unable to resolve host ××××”

    对主机IP反解即可 打开 /etc/hosts 文件, 在 127.0.0.1 localhost 那行的后面添加你自己的主机名就好啦