zoukankan      html  css  js  c++  java
  • Python3之uuid模块

    UUID是128位的全局唯一标识符,通常由32字节的字母串表示。它可以保证时间和空间的唯一性,也称为GUID。

    全称为:UUID--Universally Unique IDentifier  在python 中叫做UUID,在C#中称为 GUID--Globally Unique IDentifier.

    它通过MAC地址,时间戳,命名空间,随机数,伪随机数来保证生成ID的唯一性。

      UUID主要有五个算法,也就是五种方法来实现。

    (1). uuid1()---基于时间戳

      由MAC地址,当前时间戳,随机数字生成。可以保证全球范围内的唯一性。但是由于MAC地址的使用同时带来了安全问题,

    局域网中可以使用IP来代替MAC。

    (2). uuid2()---基于分布式计算环境DCE(python中没有这个函数)

      算法和uuid1相同,不同的是把时间戳的前4位换位POSIX的UID,实际中很少用到该方法。

    (3). uuid3()---基于名字和MD5散列值

      通过计算名字和命名空间的MD5散列值得到,保证了同一命名空间中不同名字的唯一性,和不同命名空间的唯一性,

    但同一命名空间的名字生成相同的uuid。

    (4). uuid4()---基于随机数

      由伪随机数得到,有一定的重复概率,该概率可以计算出来。

    (5). uuid5()---基于名字的SHA-1散列值

      算法和uuid3()相同,不同的是使用Secure Hash Algorithm 1 算法。

    使用经验:

    1. Python中没有基于DCE的,所以uuid2可以忽略
    2. uuid4存在概率性重复,由无映射性,最好不用
    3. 如果在global的分布式计算环境下,最好用uuid1
    4. 若有名字的唯一性要求,最好使用uuid3或uuid5


    uuid模块的典型使用方法: 

    复制代码
    >>> import uuid
    
    >>> # make a UUID based on the host ID and current time
    >>> uuid.uuid1()
    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()
    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
    'x00x01x02x03x04x05x06x07x08	
    x0bx0c
    x0ex0f'
    
    >>> # make a UUID from a 16-byte string
    >>> uuid.UUID(bytes=x.bytes)
    UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

  • 相关阅读:
    (打包报错)AS打包报错:Generate Signed APK: Errors while building APK. You can find the errors in the 'Messages' view.
    NABCD
    石家庄地铁站项目最终总结报告
    团队冲刺2.7
    团队冲刺2.6
    团队冲刺2.5
    团队冲刺2.4
    团队冲刺2.3
    用户体验评价——win10自带微软拼音输入法
    团队冲刺2.2
  • 原文地址:https://www.cnblogs.com/lykops/p/8263113.html
Copyright © 2011-2022 走看看