lqshow
10/14/2017 - 9:14 AM

python shortuuid - URL 安全的UUID生成器

安装shortuuid

pip install shortuuid

使用shortuuid

import shortuuid

获取使用的字符集

shortuuid.get_alphabet()

result: '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

设置字符集

shortuuid.set_alphabet("aaaaabcdefgh1230123")
shortuuid.get_alphabet()

result: '0123abcdefgh'

随机生成唯一uuid

shortuuid.uuid()

result: 'cFja3vEJvqayZAxo6aFvcb'

指定Name生成uuid

shortuuid.uuid(name="baidu.com")

result: 'DM59euX6ET7uxUhruf56JK'

产生定长的Uuid

shortuuid.random(length=50)

result: 'D42ozXpg8zNDBDdXYz8D4s2j6PX3j5V3PF8hZkUbgTFAbh8wz2'

ShortUUID类

当需要在每个线程中有不用的字符集来产生uuid时,可以使用ShortUUID类

shortuuid.ShortUUID().random(length=22)

Reference