python 使用UUID库生成唯一ID
生活随笔
收集整理的這篇文章主要介紹了
python 使用UUID库生成唯一ID
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? 首先導(dǎo)包: import uuid uuid1(): # make a UUID based on the host ID and current time? ?? #??基于MAC地址,時間戳,隨機數(shù)來生成唯一的uuid,可以保證全球范圍內(nèi)的唯一性 >>> uuid.uuid1() # doctest: +SKIP 結(jié)果:UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') uuid3(): # make a UUID using an MD5 hash of a namespace UUID and a name #? 通過計算一個命名空間和名字的md5散列值來給出一個uuid,所以可以保證命名空間中的不同名字具有不同的uuid,但是相同的名字就是相同的uuid了 >>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org') 結(jié)果:UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') uuid4(): # make a random UUID #? 通過偽隨機數(shù)得到uuid,是有一定概率重復(fù)的 >>> uuid.uuid4() # doctest: +SKIP 結(jié)果:UUID('16fd2706-8baf-433b-82eb-8c7fada847da') uuid5(): # make a UUID using a SHA-1 hash of a namespace UUID and a name #? 和uuid3基本相同,只不過采用的散列算法是sha1 >>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org') 結(jié)果:UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') 相關(guān)函數(shù): # convert a UUID to a string of hex digits in standard form # 將uuid對象轉(zhuǎn)換成字符串 >>> str(x) 結(jié)果:'00010203-0405-0607-0809-0a0b0c0d0e0f' # get the raw 16 bytes of the UUID # 轉(zhuǎn)化為字節(jié)序列 >>> x.bytes 結(jié)果:b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
轉(zhuǎn)載于:https://www.cnblogs.com/kadima-zy/p/8630349.html
總結(jié)
以上是生活随笔為你收集整理的python 使用UUID库生成唯一ID的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 三大利器 grep sed
- 下一篇: 洛谷 P1019 单词接龙 (DFS)