您当前的位置: 首页 >  Python

IT之一小佬

暂无认证

  • 0浏览

    0关注

    1192博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

python中uuid用法详解

IT之一小佬 发布时间:2022-07-22 23:17:44 ,浏览量:0

uuid源码注解:

r"""UUID objects (universally unique identifiers) according to RFC 4122.

This module provides immutable UUID objects (class UUID) and the functions
uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
UUIDs as specified in RFC 4122.

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.

Typical usage:

    >>> 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.NAMESPACE_DNS, 'python.org')
    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.NAMESPACE_DNS, 'python.org')
    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')
"""

uuid生成唯一随机数。

UUID: 通用唯一标识符 ( Universally Unique Identifier ), 对所有的UUID它可以保证在空间和时间上的唯一性. 它是通过MAC地址, 时间戳, 命名空间, 随机数, 伪随机数来保证生成ID的唯一性, 有着固定的大小( 128 bit ).  它的唯一性和一致性特点使得可以无需注册过程就能够产生一个新的UUID. 

python的uuid模块提供UUID类和函数uuid1(), uuid3(), uuid4(), uuid5() 来生成1, 3, 4, 5各个版本的UUID ( 需要注意的是: python中没有uuid2()这个函数). 对uuid模块中最常用的几个函数总结如下: 

1.  uuid.uuid1([node[, clock_seq]])  :    基于时间戳

        使用主机ID, 序列号, 和当前时间来生成UUID, 可保证全球范围的唯一性. 

示例代码:

import uuid

# make a UUID based on the host ID and current time
a = uuid.uuid1()  # 程序每刷新一次执行结果都会发生变化
print(a)

运行结果:

2.  uuid.uuid3(namespace, name) :   基于名字的MD5散列值

        通过计算命名空间和名字的MD5散列值来生成UUID, 可以保证同一命名空间中不同名字的唯一性和不同命名空间的唯一性, 但同一命名空间的同一名字生成的UUID相同.

示例代码:

import uuid

# make a UUID using an MD5 hash of a namespace UUID and a name
a = uuid.uuid3(uuid.NAMESPACE_DNS, 'I love python!')  # 程序刷新执行结果都是一样的
print(a)
b = uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')  # 此时执行结果和源码注释中结果是一样的
print(b)

运行结果:

3.  uuid.uuid4() :     基于随机数

        通过随机数来生成UUID. 使用的是伪随机数有一定的重复概率. 

示例代码:

import uuid

# make a random UUID
a = uuid.uuid4()  # 程序每刷新一次执行结果都会发生变化
print(a)

运行结果:

4.  uuid.uuid5(namespace, name) :    基于名字的SHA-1散列值

        通过计算命名空间和名字的SHA-1散列值来生成UUID, 算法与 uuid.uuid3() 相同.

示例代码:

import uuid

# make a UUID using a SHA-1 hash of a namespace UUID and a name
a = uuid.uuid5(uuid.NAMESPACE_DNS, 'I love python!')  # 程序刷新执行结果都是一样的
print(a)
b = uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')  # 此时执行结果和源码注释中结果是一样的
print(b)

运行结果:

 补充:

示例代码:

import uuid

# make a UUID from a string of hex digits (braces and hyphens ignored)
x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')
print(x)

# convert a UUID to a string of hex digits in standard form
y = str(x)
print(y)  # '00010203-0405-0607-0809-0a0b0c0d0e0f'

# get the raw 16 bytes of the UUID
z = x.bytes
print(z)  # 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
w = uuid.UUID(bytes=x.bytes)
print(w)  # UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

运行结果:

关注
打赏
1665675218
查看更多评论
立即登录/注册

微信扫码登录

0.0434s