-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyeast.py
More file actions
51 lines (40 loc) · 917 Bytes
/
yeast.py
File metadata and controls
51 lines (40 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/python
# coding: utf-8
import math
import time
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'
length = 64
t_map = {}
seed = 0
i = 0
prev = None
for i in range(length):
t_map[alphabet[i]] = i
def encode(num):
encoded = ''
while True:
encoded = alphabet[int(num % length)] + encoded
num = math.floor(num / length)
# simulate do-while
if not (num > 0):
break
return encoded
def decode(enc_str):
decoded = 0
for i in range(len(enc_str)):
decoded = decoded * length + t_map[enc_str[i]]
return decoded
def yeast():
global prev, seed
ts = int(time.time() * 1000)
now = encode(ts)
if now != prev:
seed = 0
prev = now
return now
else:
r = now + '.' + encode(seed)
seed += 1
return r
if __name__ == '__main__':
print(yeast())