Python shortuuid生成库学习小结

shortuuid生成库学习小结

by:授客 QQ1033553122

实践环境

win10

 

Python 3.5.4

 

shortuuid-1.0.1-py3-none-any.whl 

下载地址:

https://files.pythonhosted.org/packages/25/a6/2ecc1daa6a304e7f1b216f0896b26156b78e7c38e1211e9b798b4716c53d/shortuuid-1.0.1-py3-none-any.whl

 

shortuuid-1.0.1.tar.gz

https://files.pythonhosted.org/packages/6f/e0/a881ca1332e9195acb4c2b912d58a4278f6950e118b628188e2bc8830589/shortuuid-1.0.1.tar.gz

 

shortuuid库简介

shortuuid通过使用Python内置的uuid模块生成简洁,明确,URL安全的uuid,然后使用大小写字母和数字将它们转换为base57,并删除类似的字符,如l、1、I、O和0

 

使用举例

>>> import shortuuid

>>> shortuuid.uuid()

'4jYtG2xGoFvTcNPbnoyCNq'

 

如果需要生成uui5, 可以将名称( DNS或者 URL ) 传递给函数调用,它将用作产生的UUID的命名空间(UUID.namespace_dns或者 UUID.namespace_url ):

>>> shortuuid.uuid(name="example.com")

'exu3DTbj2ncsn9tLdLWspw'

>>> shortuuid.uuid(name="http://example.com")

'T35fvrnVz6SMSdh9y5hs8c'

 

还可以生成带密码的安全随机字符串(内部使用 os.urandom())

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

'42qBMavZJGyFmCpA49WgMv'

 

查看用于生成新uuid的字母表

>>> shortuuid.get_alphabet()

'23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

 

如果你想使用自定义字母表生成 uuid,客使用set_alphabet():

>>> shortuuid.set_alphabet('LMNPaaaaabcdefgh1230123fghijk')

>>> shortuuid.get_alphabet()

'0123LMNPabcdefghijk'

 

还支持对已生成的uuid进行加解码

>>> import uuid

>>> u = uuid.uuid4()

>>> u

UUID('8b6b07db-d753-4627-8e1c-4873fe427439')

>>> s = shortuuid.encode(u)

>>> s

'0hMMNLce3gecchjf31N11kkh3MN1fej'

>>> shortuuid.decode(s) == u

True

 

参考链接

https://pypi.org/project/shortuuid/#description

 

posted @ 2020-08-02 20:41  授客  阅读(942)  评论(0编辑  收藏  举报