Skip to content

Commit 5c17c94

Browse files
committed
v0.0.3: more examples in README, unified signature of make() and .max_value(), added --max-value to uuid05 utility
1 parent 3b4d3e0 commit 5c17c94

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

Diff for: README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,25 @@ pip install uuid05
3737
``` python
3838
from uuid05 import UUID05
3939

40-
# May be parametrized by workers: int, ttl: int, precision: int
41-
# defaults are: workers=10, ttl=2 days, precision=1
40+
# basic usage
4241
uid = UUID05.make()
4342
suffix: str = uid.as_b64()
4443
object_name: str = f'autotest_object_{suffix}'
44+
45+
# .make() may be parametrized by workers: int, ttl: int, precision: int
46+
# defaults are: workers=10, ttl=2 days, precision=1
47+
uid: int = UUID05.make(workers=16, ttl=86400, precision=6) # 1419554951415
48+
uid.as_b64() # 'AUqEXzNy'
49+
50+
# you may also want to just shorten your existing integer identifiers.
51+
suffix: str = UUID05(123123123123).as_b64()
52+
assert suffix == 'HKq1w7M'
53+
54+
# How to get maximum UUID value/length with given params?
55+
max_value = UUID05.max_value(machines=16, ttl=86400, precision=6) # 1586399913600
56+
len(str(max_value)) == 13
57+
len(max_value.as_b64()) == 8 # AXFczaaA
58+
len(f'autotest_object_{max_value.as_b64()}') == 23 # autotest_object_AXFczaaA
4559
```
4660

4761
It can be also used as an utility from command-line:
@@ -83,7 +97,8 @@ Otherwise Redis, Memcached or another database with a single INCRementing counte
8397
- If your objects are persistent - you'd better use [py-nanoid](https://github.com/puyuan/py-nanoid).
8498
- If you need to generate multiple UIDs for multiple object really _quick_:
8599
- generate one and reuse it, using a semantic or loop variable as a suffix;
86-
- pass **precision** argument to `uuid05()`. It scales automatically with worker count, but if there are less than 16 workers, default is 1 which means 1 uuid per 0.1 second, usually it's enough.
100+
- pass **precision** argument to `uuid05()`. It scales automatically with worker count,
101+
but if there are less than 16 workers, default is 1 which means 1 uuid per 0.1 second, usually it's enough.
87102
- `precision=3` argument will use milliseconds.
88103
- `precision=6` for microseconds.
89104
- if `precision=6` is not enough stop trying to make your identifier compact.

Diff for: cli/uuid05

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def main():
1212
parser.add_argument('-p', '--precision', type=int, help='Increase up to 6 if you create objects frequently.')
1313
parser.add_argument('-b', '--base64', action='store_true', help='Use base64 to compact it event more')
1414
parser.add_argument('-a', '--altchars', type=str, help='Alternative characters for b64encode')
15+
parser.add_argument('-m', '--max-value', action='store_true', help='Return maximum possible value for given params')
1516
args = parser.parse_args()
1617
kwargs = {}
1718
if args.workers:
@@ -20,7 +21,7 @@ def main():
2021
kwargs['ttl'] = args.ttl
2122
if args.precision:
2223
kwargs['precision'] = args.precision
23-
uid: UUID05 = UUID05.make(**kwargs)
24+
uid: UUID05 = UUID05.max_value(**kwargs) if args.max_value else UUID05.make(**kwargs)
2425
print((uid.as_b64(altchars=args.altchars.encode()) if args.altchars else uid.as_b64()) if args.base64 else uid)
2526

2627

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='uuid05',
6-
version='0.0.2',
6+
version='0.0.3',
77
packages=['uuid05'],
88
url='https://github.com/strizhechenko/uuid05',
99
license='MIT',

Diff for: uuid05/__init__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def as_b64(self, *args, **kwargs) -> str:
1717
If you're using uuid05 results as strings and want it to be more compact, and being integer or not doesn't matter
1818
you may use this function to encode it to base64. You can transparently pass arguments of b64encode function here.
1919
As there's not decoding assumed padding symbols are removed.
20-
%timeit b64(uuid05()) - 2.87 µs ± 13.9 ns per loop @ 3.2GHz
20+
%timeit UUID05.make().as_b64() - 2.87 µs ± 13.9 ns per loop @ 3.2 GHz
21+
%timeit UUID05.make().as_b64() - 1.54 µs ± 5.25 ns per loop @ 3.8 GHz
22+
%timeit UUID05(1333).as_b64() - 608 ns ± 2.68 ns per loop (with static UUID (skips generation))
2123
>>> UUID05(34714).as_b64(altchars=b'_-')
2224
'h5o'
2325
>>> UUID05(11402098).as_b64(altchars=b'_-')
@@ -36,7 +38,9 @@ def make(cls, workers=10, ttl=2 * day, precision=0) -> 'UUID05':
3638
Compact human-readable unique identifiers for temporary objects in small non-synchronizing distributed systems.
3739
If your objects are persistent - you'd better use nanoid.
3840
If you need to generate multiple UIDs for multiple object at once - generate one and use loop variable as suffix.
39-
%timeit uuid05() - 2.1 µs ± 2.08 ns per loop @ 3.2GHz
41+
%timeit UUID05.make() - 2.1 µs ± 2.08 ns per loop @ 3.2GHz
42+
%timeit UUID05.make() - 849 ns ± 1.73 ns per loop @ 3.8GHz
43+
%timeit UUID05.make() - 717 ns ± 1.76 ns per loop @ 4.7GHz
4044
:arg workers - count of hosts in a system;
4145
:arg ttl - how many seconds a temporary object lives in a system (maximum) before being deleted;
4246
:arg precision (optional) - you can override (increase) a precision if objects are created frequently (max - 6);
@@ -53,10 +57,10 @@ def make(cls, workers=10, ttl=2 * day, precision=0) -> 'UUID05':
5357
return UUID05(f'{run_id}{time_id}')
5458

5559
@classmethod
56-
def uuid05_max_value(cls, machines=10, ttl=2 * day, precision=0) -> 'UUID05':
60+
def max_value(cls, workers=10, ttl=2 * day, precision=0) -> 'UUID05':
5761
""":returns - maximum value of an uuid05 with given params"""
58-
precision = min(precision or int(machines ** (1 / 4)), 6)
59-
run_id = machines - 1
62+
precision = min(precision or int(workers ** (1 / 4)), 6)
63+
run_id = workers - 1
6064
time_id = ttl * ((10 ** precision) - 1)
6165
return UUID05(f'{run_id}{time_id}')
6266

@@ -67,6 +71,6 @@ def uuid05_max_value(cls, machines=10, ttl=2 * day, precision=0) -> 'UUID05':
6771
for _workers in 2, 4, 10, 16, 32, 256:
6872
worker_id = randint(0, _workers)
6973
example_value = UUID05.make(_workers, _ttl)
70-
max_value = UUID05.uuid05_max_value(_workers, _ttl)
74+
max_value = UUID05.max_value(_workers, _ttl)
7175
max_in_b64 = max_value.as_b64(altchars=b'-_')
7276
print(f'| {_ttl} | {_workers} | {worker_id} | {example_value} | {max_value} | {max_in_b64} |')

0 commit comments

Comments
 (0)