python模块之uuid 整理

uuid是一种唯一标识,在许多领域作为标识用途。python的uuid模块就是用来生成它的。
闲话不说,python提供的生成uuid的方法一共有4种,分别是:

1.从硬件地址和时间生成 ——uuid1
2.从md5算法生成——uuid3
3.随机生成—— uuid4
4.从SHA-1算法生成——sha1

他们在uuid模块里对应uuid1, uuid3, uuid4, uuid5这几个方法,注意没有uuid2。

下面是示例: 

#-*- encoding: gb2312 -*-
import uuid

print uuid.uuid1()
print uuid.uuid3(uuid.NAMESPACE_DNS, 'testme')
print uuid.uuid4()
print uuid.uuid5(uuid.NAMESPACE_DNS, 'testme')


import uuid
>>> print uuid.uuid1()
5cfdedb0-01e8-11df-ba6a-001636e99c1a
>>> uuid.uuid1().get_hex()
'7b6d5f0f01e811dfbf6d001636e99c1a'
# 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
     '\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')



参考博客
http://blog.csdn.net/zhaoweikid/article/details/1649786

http://hi.baidu.com/derris/item/0e00f7ee7f8320d0ea34c9ab

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值