Python每日一题 002
做为 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的更多相关文章
- Python:每日一题002
题目: 企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40万 ...
- Python每日一题 003
将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...
- Python每日一题 004
将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...
- Python每日一题 009
题目 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. 代码 参照网络上代码 # coding: utf-8 import os import re # ...
- Python每日一题 008
题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...
- Python每日一题 007
题目 你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词. 很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出 ...
- Python每日一题 006
题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...
- Python每日一题 005
任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...
- Python每日一题 001
Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...
随机推荐
- shell脚本编程运算
一算术运算 bash中的算术运算:help let+, -, *, /, %取模(取余), **(乘方)实现算术运算:(1) let var=算术表达式(2) var=$[算术表达式](3) var= ...
- CF1061E Politics E. Politics 解题报告
CF1061E Politics E. Politics 考虑利用树的性质,因为是子树问题,所以放到dfs序上. 只考虑一个树,问题是每个区间选恰好\(k\)个.因为区间其实是子树,所以区间要么包含, ...
- Linux下dd和od命令备份查看硬盘mbr,并用vim修改!
主引导记录(Master Boot Record,MBR),位于一个硬盘的0柱面.0盘面.1扇区,共512字节.具体划分依次为:引导代码区440字节.磁盘签名4字节.空白(Ox0000)2字节.MBR ...
- 左闭右开线段树 2019牛客多校(第七场)E_Find the median(点代表区间
目录 题意 一种解析 AC_Code @(2019第七场牛客 E_Find the median 左闭右开线段树) 题意 链接:here 我理解的题意就是:初始序列为空,有\(n(400000)\)次 ...
- Oracle中使用REGEXP_SUBSTR,regexp_replace,wm_concat函数
REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)__srcstr ...
- 公司-ofo:ofo
ylbtech-公司-ofo:ofo ofo小黄车是一个无桩共享单车出行平台,缔造了“无桩单车共享”模式,致力于解决城市出行问题.用户只需在微信公众号或App扫一扫车上的二维码或直接输入对应车牌号,即 ...
- java中子类继承父类程序执行顺序
java中子类继承父类程序执行顺序 FatherTest.java public class FatherTest { private String name; public FatherTest() ...
- java 子类对象实例化的过程
大家往往听说 子类继承了父类就有了父类中的所有的非私有的属性,但是怎么就有了父类的属性了呢 ?且听下面分解 子类对象实例化的过程: 1 从结果上来看:(继承性) 子类继承了父类以后 就获取了父类中声 ...
- git rollback
http://stackoverflow.com/questions/1616957/how-do-you-roll-back-reset-a-git-repository-to-a-particul ...
- Gym 102021D : Down the Pyramid(思维)
Do you like number pyramids? Given a number sequence that represents the base, you are usually suppo ...