digit62='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'#整数转化为62进制字符串#入口:# x : 整数#返回: 字符串defint_to_str62(x):try:x=int(x)except:x=0ifx<0:x=-xifx==0:return"0"s=""whilex>=62:x1=x%62s=digit62[x1]+sx=x//62ifx>0:s=digit62[x]+sreturns#62进制字符串转化为整数#入口:# s : 62进制字符串#返回: 整数defstr62_to_int(s):x=0s=str(s).strip()ifs=="":returnxforyins:k=digit62.find(y)ifk>=0:x=x*62+kreturnximporttimeimportrandomimportstringt=time.strftime('%y%m%d%H%M%S')cut=[t[i:i+2]foriinrange(0,len(t),2)]62t=''.join([int_to_str62(int(x))forxincut])m=62t+''.join([random.choice(string.lowercase+string.digits)for_inrange(6)])
再ps. 有人说random.choice慢而且随机不均匀,我就写了两个小程序测试一下
importrandomimporttimeimportstringimporttimeitimporthashlibimportuuidimportthreadingdefrandomchoice():return''.join([random.choice(string.lowercase+string.digits)for_inrange(6)])def_time(f,n=1000000):print'start timeit function ',ft=timeit.timeit(f,number=n)print'repeat %s times and used %ss'%(n,t)print'end timeit function ',fprint_time(randomchoice)
result
start timeit function <function randomchoice at 0x2a7d6e0>
repeat 1000000 times and used 3.97338795662s
end timeit function <function randomchoice at 0x2a7d6e0>