Skip to content

Commit 6c11da4

Browse files
committed
add sep arg to morebuiltins.utils.gen_id to customize the separator between date and time.
1 parent 70ce9f3 commit 6c11da4

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### 1.3.5 (2025-10-15)
44
1. fix writer.wait_closed() ConnectionResetError in ipc.py, proxy_checker.py, log_server.py
55
2. add `morebuiltins.funcs.debounce` decorator to debounce function calls.
6+
3. add `sep` arg to `morebuiltins.utils.gen_id` to customize the separator between date and time.
67

78
### 1.3.4 (2025-10-15)
89
1. fix `morebuiltins.utils.gen_id` wrong length issue.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ print(morebuiltins.__file__)
138138

139139
1.36 `base_decode` - Decode a base-N string to a number.
140140

141-
1.37 `gen_id` - Generate a unique ID based on the current time and random bytes.
141+
1.37 `gen_id` - Generate a readable & unique ID based on the current time(ms) and random bytes.
142142

143143
1.38 `timeti` - Return the number of iterations per second for a given statement.
144144

doc.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,15 +1163,30 @@ Examples:
11631163

11641164

11651165

1166-
1.37 `gen_id` - Generate a unique ID based on the current time and random bytes.
1166+
1.37 `gen_id` - Generate a readable & unique ID based on the current time(ms) and random bytes.
11671167

11681168

11691169
```python
1170+
The performance is about 400000 IDs per second.
11701171

11711172
If rand_len=0 the length of ID will be 18, rand_len=4 the length of ID will be 22.
11721173
ID format:
11731174
- {YYMMDD_HHMMSS}_{4-digit base62 of microsecond}{rand_len urandom hex string}
11741175

1176+
The following table shows the relationship between rand_len and the safe range of unique IDs per microsecond:
1177+
1178+
rand_len | urandom_size | Safe Range
1179+
---------------------------------------------
1180+
2 | 1 | 10
1181+
4 | 2 | 100
1182+
6 | 3 | 1000
1183+
8 | 4 | 10000
1184+
10 | 5 | 100000
1185+
12 | 6 | 1000000
1186+
14 | 7 | 10000000
1187+
1188+
Seems like rand_len -> 10 ** (rand_len // 2) safe range.
1189+
11751190
Args:
11761191
rand_len (int, optional): Defaults to 4.
11771192

@@ -1188,6 +1203,8 @@ True
11881203
1000
11891204
>>> [len(gen_id(i)[:]) for i in range(10)]
11901205
[18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
1206+
>>> # gen_id() => 251111_235204_0xYfc4f8
1207+
>>> # gen_id(sep="") => 2511112352291nTq972c
11911208

11921209
```
11931210

morebuiltins/utils.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,13 +1863,28 @@ def base_decode(
18631863
return result
18641864

18651865

1866-
def gen_id(rand_len=4) -> str:
1867-
"""Generate a unique ID based on the current time and random bytes.
1866+
def gen_id(rand_len=4, sep="_") -> str:
1867+
"""Generate a readable & unique ID based on the current time(ms) and random bytes.
1868+
The performance is about 400000 IDs per second.
18681869
18691870
If rand_len=0 the length of ID will be 18, rand_len=4 the length of ID will be 22.
18701871
ID format:
18711872
- {YYMMDD_HHMMSS}_{4-digit base62 of microsecond}{rand_len urandom hex string}
18721873
1874+
The following table shows the relationship between rand_len and the safe range of unique IDs per microsecond:
1875+
1876+
rand_len | urandom_size | Safe Range
1877+
---------------------------------------------
1878+
2 | 1 | 10
1879+
4 | 2 | 100
1880+
6 | 3 | 1000
1881+
8 | 4 | 10000
1882+
10 | 5 | 100000
1883+
12 | 6 | 1000000
1884+
14 | 7 | 10000000
1885+
1886+
Seems like rand_len -> 10 ** (rand_len // 2) safe range.
1887+
18731888
Args:
18741889
rand_len (int, optional): Defaults to 4.
18751890
@@ -1886,15 +1901,17 @@ def gen_id(rand_len=4) -> str:
18861901
1000
18871902
>>> [len(gen_id(i)[:]) for i in range(10)]
18881903
[18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
1904+
>>> # gen_id() => 251111_235204_0xYfc4f8
1905+
>>> # gen_id(sep="") => 2511112352291nTq972c
18891906
"""
18901907
now = datetime.now()
1891-
s1 = now.strftime("%y%m%d_%H%M%S")
1908+
s1 = now.strftime(f"%y%m%d{sep}%H%M%S")
18921909
s2 = base_encode(now.microsecond)
18931910
if rand_len:
18941911
s3 = os.urandom(rand_len // 2 + 1).hex()[:rand_len]
18951912
else:
18961913
s3 = ""
1897-
return f"{s1}_{s2:>04}{s3}"
1914+
return f"{s1}{sep}{s2:>04}{s3:0>{rand_len}}"
18981915

18991916

19001917
def timeti(

0 commit comments

Comments
 (0)