Skip to content

Instantly share code, notes, and snippets.

@RRRoger
Created November 5, 2018 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RRRoger/a9eeacf62ae837274be83d055b21c0d3 to your computer and use it in GitHub Desktop.
Save RRRoger/a9eeacf62ae837274be83d055b21c0d3 to your computer and use it in GitHub Desktop.
获取short_uuid函数
# -*- encoding: utf-8 -*-
from uuid import uuid4
uuidChars = (
"a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"
)
"""
1.每四位从16进制转成10进制
2.取62的余数作为index从`uuidChars`中取字符
"""
def short_uuid():
uuid = str(uuid4()).replace('-', '')
result = ''
for i in range(0, 8):
sub = uuid[i * 4: i * 4 + 4]
x = int(sub, 16)
result += uuidChars[x % 0x3E]
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment