Created
November 5, 2018 08:38
-
-
Save RRRoger/a9eeacf62ae837274be83d055b21c0d3 to your computer and use it in GitHub Desktop.
获取short_uuid函数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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