您的位置 首页 > 数码极客

【hostid】uuid, 致你我彼此的唯一

今天的主题是唯一的。一定要看文章的结尾!

python知识共享

通用唯一标识符(uuid):全世界,你,独一无二,世界上独一无二,you are the only one。

应用场景

在比较多的场景中,我们需要一个值是唯一的。

1、一个照片文件夹,所有的图片命名不能重复

2、数据库中主键id不能重复

3、订单号的唯一性。。。

应用示例

Python的uuid库可以非常方便的满足你的需求

提供了uuid1(), uuid3(), uuid4(), uuid5()几种不同的方法生成uuid。

If all you want is a unique ID, you should probably call uuid1() or uuid4(). Note that uuid1() may compromise privacy since it creates a UUID containing the computer's network address. uuid4() creates a random UUID.

uuid4就可以满足一般的需求了。

import uuid uuid_str = str()) print(uuid_str) # 7cd93b92-c75e-4377-af98-6650c5bcb211

其他用法可以直接看原文档注释:

>>> import uuid # make a UUID based on the host ID and current time >>> uuid.uuid1() # doctest: +SKIP UUID('a8098c1a-f86e-11da-bd1a-00112444be1e') # make a UUID using an MD5 hash of a namespace UUID and a name >>> uuid.uuid3, ';) UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e') # make a random UUID >>> uuid.uuid4() # doctest: +SKIP UUID('16fd2706-8baf-433b-82eb-8c7fada847da') # make a UUID using a SHA-1 hash of a namespace UUID and a name >>> uuid.uuid5, ';) UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d') # make a UUID from a string of hex digits (braces and hyphens ignored) >>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}') # convert a UUID to a string of hex digits in standard form >>> str(x) '00010203-0405-0607-0809-0a0b0c0d0e0f' # get the raw 16 bytes of the UUID >>> x.bytes b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' # make a UUID from a 16-byte string >>> uuid.UUID(bytes=x.bytes) UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

甜言蜜语分享

以后会偶尔在推文中加入一些我和TA实际生活中的一些甜美的对话分享给大家。

老婆: 你今天想我吗?有多想我?

我: 非常想,连我的眉毛都在想你,only you。



大家评论区留下你们的甜言蜜语吧


关于作者: admin

无忧经验小编鲁达,内容侵删请Email至wohenlihai#qq.com(#改为@)

热门推荐