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 ...
随机推荐
- bzoj 3626
http://www.lydsy.com/JudgeOnline/problem.php?id=3626 让我比较惊讶的一道链剖裸题(' ' ) 做法很精妙 首先我们考虑对于单个询问时可以拆分成 ...
- Python基础教程(011)--程序开发中的错误及原因
前言 排查代码开发中的错误 内容 1,编写的程序不能正常执行,或者执行的结果不是我们期望的 2,俗称bug,是程序开发常见的,初学常见的原因有 手误 对已经学习的知识点理解不足 对语音还有需要学习和提 ...
- delphi 动态获取文件类型的图标
delphi 动态获取文件类型的图标.txt我不奢望什么,只希望你以后的女人一个不如一个.真怀念小时候啊,天热的时候我也可以像男人一样光膀子!在应用程序的编写中,组合框(ComboBox).列表框(L ...
- webpack 导出、导入模块(及路径)
参考:https://blog.csdn.net/xyphf/article/details/83411552 (下面的代码亲测有效) 注:导入的模块的方法,只有两种方法 import 和 requ ...
- (转)使用OpenGL显示图像(三)绘制Shapes
绘制形状 编写:jdneo - 原文:http://developer.android.com/training/graphics/opengl/draw.html 在定义了使用OpenGL绘制的形状 ...
- python基础二(基本数据类型)
python的基本数据类型:数字.字符串.列表.元祖.字典.集合 一.基本数据类型 1.1 数字int 数字主要是用来计算用的,使用方法并不多. # bit_length() 当十进制用二进制表示的时 ...
- 剑指offer---3、按之字形顺序打印二叉树
剑指offer---3.按之字形顺序打印二叉树 一.总结 一句话总结: |||-begin 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照 ...
- 68、Schema的相关类
public class SObjectSchema { public void testSchema(){ //获取SObject的token //1.先获取所有token,然后通过key获取需要的 ...
- 63、saleforce 的 Merchandise 的简单的增删改查
自定义的controller public with sharing class MerchandiseController { public List<Merchandise__c> m ...
- web开发小知识
session共享机制:f5刷新是再次提交之前的数据请求 地址栏回车属于不同的请求 不同浏览器获取不到之前数据 同一浏览器可以获取同步数据 session注销:session.invalidate() ...