zid 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
zid-1.0.0/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
zid-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,117 @@
1
+ Metadata-Version: 2.1
2
+ Name: zid
3
+ Version: 1.0.0
4
+ Summary: ZID is a unique identifier with nice properties
5
+ Home-page: https://github.com/Zaczero/zid
6
+ License: Unlicense
7
+ Keywords: uuid,unique identifier,identifier,primary key
8
+ Author: Kamil Monicz
9
+ Author-email: kamil@monicz.dev
10
+ Requires-Python: >=3.8,<4.0
11
+ Classifier: Development Status :: 5 - Production/Stable
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved
14
+ Classifier: License :: OSI Approved :: The Unlicense (Unlicense)
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.8
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Database
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Classifier: Typing :: Typed
26
+ Project-URL: Repository, https://github.com/Zaczero/zid
27
+ Description-Content-Type: text/markdown
28
+
29
+ # ZID
30
+
31
+ [![PyPI - Python Version](https://shields.monicz.dev/pypi/pyversions/zid)](https://pypi.org/project/zid)
32
+ [![Liberapay Patrons](https://shields.monicz.dev/liberapay/patrons/Zaczero?logo=liberapay&label=Patrons)](https://liberapay.com/Zaczero/)
33
+ [![GitHub Sponsors](https://shields.monicz.dev/github/sponsors/Zaczero?logo=github&label=Sponsors&color=%23db61a2)](https://github.com/sponsors/Zaczero)
34
+
35
+ **ZID** is a unique identifier with nice properties:
36
+
37
+ - It behaves like a 64-bit signed integer, so it can be safely used with external software, e.g., in a database. ZIDs will never overflow into negative values.
38
+
39
+ - ZIDs are numerically sortable since the timestamp is stored in the most significant bits. Additional randomness is stored only in the least significant bits.
40
+
41
+ - The specification is very simple, reducing the potential for bugs and making ZIDs highly efficient to generate and parse. Scroll down for the installation-free copy-and-paste code - it's that short!
42
+
43
+ - CSPRNG-initialized sequence numbers enhance the privacy of the generated identifiers while remaining collision-resistant. You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine.
44
+
45
+ ## Installation
46
+
47
+ ```sh
48
+ pip install zid
49
+ ```
50
+
51
+ ## Installation (copy & paste)
52
+
53
+ Alternatively, you can copy and paste the following code for an installation-free ZID generator. This code excludes some performance optimizations and utility methods for the sake of simplicity:
54
+
55
+ ```py
56
+ from os import urandom
57
+ from time import time_ns
58
+
59
+ _last_time: int = -1
60
+ _last_sequence: int = -1
61
+
62
+ def zid() -> int:
63
+ global _last_time, _last_sequence
64
+
65
+ # UNIX timestamp in milliseconds
66
+ time: int = time_ns() // 1_000_000
67
+ if time > 0x7FFFFFFFFFFF:
68
+ raise OverflowError('Time value is too large')
69
+
70
+ # CSPRNG-initialized sequence numbers
71
+ sequence: int
72
+ if _last_time == time:
73
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
74
+ else:
75
+ _last_sequence = sequence = int.from_bytes(urandom(2))
76
+ _last_time = time
77
+
78
+ return (time << 16) | sequence
79
+ ```
80
+
81
+ ## Basic Usage
82
+
83
+ ```py
84
+ from zid import zid
85
+ zid() # -> 112723768038396241
86
+ zid() # -> 112723768130153517
87
+ zid() # -> 112723768205368402
88
+
89
+ from zid import parse_zid_timestamp
90
+ parse_zid_timestamp(112723768038396241)
91
+ # -> 1720028198828 (UNIX timestamp in milliseconds)
92
+ ```
93
+
94
+ ## Format specification
95
+
96
+ ZID is 64 bits long in binary. Only 63 bits are used to fit in a signed integer. The first 47 bits are a UNIX timestamp in milliseconds. The remaining 16 bits are CSPRNG-initialized sequence numbers.
97
+
98
+ ```txt
99
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100
+ |0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|
101
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102
+ |0| timestamp (31 bits) |
103
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104
+ | timestamp (16 bits) | random+sequence (16 bits) |
105
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106
+ ```
107
+
108
+ ## Limitations
109
+
110
+ - Timestamps support years from 1970 to approx. 6429. To verify this, you can follow the formula *1970 + (2^47 − 1) / 1000 / (3600 * 24) / 365.25*
111
+
112
+ - If several ZIDs are generated with the same millisecond timestamp, knowing one of them will allow you to discover the others due to linearly increasing sequence numbers. Otherwise, guessing ZID values is difficult (but not impossible) due to the millisecond precision of the timestamp and the additional 16 bits of entropy. Do not rely on ZIDs alone for your security!
113
+
114
+ - You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine. With two separate machines, you will generate on average 16,384 ZIDs each before a collision occurs within the same millisecond timestamp. With three separate machines, the average number is 10,240 ZIDs each.
115
+
116
+ - ZIDs are not strictly increasing within the same millisecond timestamp due to the possible sequence number overflow.
117
+
zid-1.0.0/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # ZID
2
+
3
+ [![PyPI - Python Version](https://shields.monicz.dev/pypi/pyversions/zid)](https://pypi.org/project/zid)
4
+ [![Liberapay Patrons](https://shields.monicz.dev/liberapay/patrons/Zaczero?logo=liberapay&label=Patrons)](https://liberapay.com/Zaczero/)
5
+ [![GitHub Sponsors](https://shields.monicz.dev/github/sponsors/Zaczero?logo=github&label=Sponsors&color=%23db61a2)](https://github.com/sponsors/Zaczero)
6
+
7
+ **ZID** is a unique identifier with nice properties:
8
+
9
+ - It behaves like a 64-bit signed integer, so it can be safely used with external software, e.g., in a database. ZIDs will never overflow into negative values.
10
+
11
+ - ZIDs are numerically sortable since the timestamp is stored in the most significant bits. Additional randomness is stored only in the least significant bits.
12
+
13
+ - The specification is very simple, reducing the potential for bugs and making ZIDs highly efficient to generate and parse. Scroll down for the installation-free copy-and-paste code - it's that short!
14
+
15
+ - CSPRNG-initialized sequence numbers enhance the privacy of the generated identifiers while remaining collision-resistant. You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine.
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ pip install zid
21
+ ```
22
+
23
+ ## Installation (copy & paste)
24
+
25
+ Alternatively, you can copy and paste the following code for an installation-free ZID generator. This code excludes some performance optimizations and utility methods for the sake of simplicity:
26
+
27
+ ```py
28
+ from os import urandom
29
+ from time import time_ns
30
+
31
+ _last_time: int = -1
32
+ _last_sequence: int = -1
33
+
34
+ def zid() -> int:
35
+ global _last_time, _last_sequence
36
+
37
+ # UNIX timestamp in milliseconds
38
+ time: int = time_ns() // 1_000_000
39
+ if time > 0x7FFFFFFFFFFF:
40
+ raise OverflowError('Time value is too large')
41
+
42
+ # CSPRNG-initialized sequence numbers
43
+ sequence: int
44
+ if _last_time == time:
45
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
46
+ else:
47
+ _last_sequence = sequence = int.from_bytes(urandom(2))
48
+ _last_time = time
49
+
50
+ return (time << 16) | sequence
51
+ ```
52
+
53
+ ## Basic Usage
54
+
55
+ ```py
56
+ from zid import zid
57
+ zid() # -> 112723768038396241
58
+ zid() # -> 112723768130153517
59
+ zid() # -> 112723768205368402
60
+
61
+ from zid import parse_zid_timestamp
62
+ parse_zid_timestamp(112723768038396241)
63
+ # -> 1720028198828 (UNIX timestamp in milliseconds)
64
+ ```
65
+
66
+ ## Format specification
67
+
68
+ ZID is 64 bits long in binary. Only 63 bits are used to fit in a signed integer. The first 47 bits are a UNIX timestamp in milliseconds. The remaining 16 bits are CSPRNG-initialized sequence numbers.
69
+
70
+ ```txt
71
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72
+ |0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|
73
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74
+ |0| timestamp (31 bits) |
75
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76
+ | timestamp (16 bits) | random+sequence (16 bits) |
77
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78
+ ```
79
+
80
+ ## Limitations
81
+
82
+ - Timestamps support years from 1970 to approx. 6429. To verify this, you can follow the formula *1970 + (2^47 − 1) / 1000 / (3600 * 24) / 365.25*
83
+
84
+ - If several ZIDs are generated with the same millisecond timestamp, knowing one of them will allow you to discover the others due to linearly increasing sequence numbers. Otherwise, guessing ZID values is difficult (but not impossible) due to the millisecond precision of the timestamp and the additional 16 bits of entropy. Do not rely on ZIDs alone for your security!
85
+
86
+ - You can generate up to 65,536 ZIDs within the same millisecond timestamp on a single machine. With two separate machines, you will generate on average 16,384 ZIDs each before a collision occurs within the same millisecond timestamp. With three separate machines, the average number is 10,240 ZIDs each.
87
+
88
+ - ZIDs are not strictly increasing within the same millisecond timestamp due to the possible sequence number overflow.
@@ -0,0 +1,154 @@
1
+ [tool.poetry]
2
+ authors = ["Kamil Monicz <kamil@monicz.dev>"]
3
+ classifiers = [
4
+ "Development Status :: 5 - Production/Stable",
5
+ "Intended Audience :: Developers",
6
+ "License :: OSI Approved :: The Unlicense (Unlicense)",
7
+ "Operating System :: OS Independent",
8
+ "Programming Language :: Python :: 3.8",
9
+ "Programming Language :: Python :: 3.9",
10
+ "Programming Language :: Python :: 3.10",
11
+ "Programming Language :: Python :: 3.11",
12
+ "Programming Language :: Python :: 3.12",
13
+ "Programming Language :: Python :: 3.13",
14
+ "Topic :: Database",
15
+ "Topic :: Software Development :: Libraries :: Python Modules",
16
+ "Typing :: Typed",
17
+ ]
18
+ description = "ZID is a unique identifier with nice properties"
19
+ keywords = ["uuid", "unique identifier", "identifier", "primary key"]
20
+ license = "Unlicense"
21
+ name = "zid"
22
+ readme = "README.md"
23
+ repository = "https://github.com/Zaczero/zid"
24
+ version = "1.0.0"
25
+
26
+ [tool.poetry.dependencies]
27
+ python = "^3.8"
28
+
29
+ [tool.poetry.group.test.dependencies]
30
+ mypy = "^1.10.1"
31
+ pytest = "^8.2.2"
32
+ pytest-cov = "^5.0.0"
33
+
34
+ [build-system]
35
+ build-backend = "poetry.core.masonry.api"
36
+ requires = ["poetry-core"]
37
+
38
+ [tool.ruff]
39
+ exclude = [
40
+ ".direnv",
41
+ ".git",
42
+ ".mypy_cache",
43
+ ".ruff_cache",
44
+ ".venv",
45
+ "build",
46
+ "dist",
47
+ ]
48
+
49
+ indent-width = 4
50
+ line-length = 120
51
+ target-version = "py38"
52
+
53
+ [tool.ruff.lint]
54
+ # custom + https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
55
+ ignore = [
56
+ "B017", # assert-raises-exception
57
+ "TRY003", # raise-vanilla-args
58
+ "S101", # assert
59
+ "PT011", # pytest-raises-too-broad
60
+ "W191", # tab-indentation
61
+ "E111", # indentation-with-invalid-multiple
62
+ "E114", # indentation-with-invalid-multiple-comment
63
+ "E117", # over-indented
64
+ "E501", # line-too-long
65
+ "D206", # indent-with-spaces
66
+ "D300", # triple-single-quotes
67
+ "Q000", # bad-quotes-inline-string
68
+ "Q001", # bad-quotes-multiline-string
69
+ "Q002", # bad-quotes-docstring
70
+ "Q003", # avoidable-escaped-quote
71
+ "COM812", # missing-trailing-comma
72
+ "COM819", # prohibited-trailing-comma
73
+ "ISC001", # single-line-implicit-string-concatenation
74
+ "ISC002", # multi-line-implicit-string-concatenation
75
+ ]
76
+ # see https://docs.astral.sh/ruff/rules/ for rules documentation
77
+ select = [
78
+ "A", # flake8-builtins
79
+ "ARG", # flake8-unused-arguments
80
+ "ASYNC", # flake8-async
81
+ "B", # flake8-bugbear
82
+ "C4", # flake8-comprehensions
83
+ "DTZ", # flake8-datetimez
84
+ "E4", # pycodestyle
85
+ "E7",
86
+ "E9",
87
+ "F", # pyflakes
88
+ "FA", # flake8-future-annotations
89
+ "FLY", # flynt # "FURB", # refurb
90
+ "G", # flake8-logging-format
91
+ "I", # isort
92
+ "ICN", # flake8-import-conventions
93
+ "INT", # flake8-gettext
94
+ "ISC", # flake8-implicit-str-concat
95
+ "LOG", # flake8-logging
96
+ "N", # pep8-naming
97
+ "NPY", # numpy
98
+ "Q", # flake8-quotes
99
+ "PERF", # perflint
100
+ "PIE", # flake8-pie
101
+ "PT", # flake8-pytest-style
102
+ "PTH", # flake8-use-pathlib
103
+ "PYI", # flake8-pyi
104
+ "RSE", # flake8-raise
105
+ "RUF", # ruff
106
+ "S", # flake8-bandit
107
+ "SIM", # flake8-simplify
108
+ "SLF", # flake8-self
109
+ "SLOT", # flake8-slots
110
+ "T10", # flake8-debugger
111
+ "TID", # flake8-tidy-imports
112
+ "TRY", # tryceratops
113
+ "UP", # pyupgrade
114
+ "W6",
115
+ "YTT", # flake8-2020
116
+ ]
117
+
118
+ # allow fix for all enabled rules (when `--fix`) is provided.
119
+ fixable = ["ALL"]
120
+ unfixable = []
121
+
122
+ # allow unused variables when underscore-prefixed.
123
+ dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
124
+
125
+ [tool.ruff.format]
126
+ indent-style = "space"
127
+ line-ending = "lf"
128
+ quote-style = "single"
129
+ skip-magic-trailing-comma = false
130
+
131
+ [tool.ruff.lint.flake8-quotes]
132
+ docstring-quotes = "double"
133
+ inline-quotes = "single"
134
+ multiline-quotes = "double"
135
+
136
+ [tool.ruff.lint.pylint]
137
+ max-args = 10
138
+
139
+ [tool.mypy]
140
+ check_untyped_defs = true
141
+ disable_error_code = "import-untyped"
142
+ explicit_package_bases = true
143
+ mypy_path = "$MYPY_CONFIG_FILE_DIR"
144
+ packages = ["zid"]
145
+ warn_redundant_casts = true
146
+ warn_return_any = true
147
+ warn_unreachable = true
148
+ warn_unused_configs = true
149
+ warn_unused_ignores = true
150
+
151
+ [tool.pytest.ini_options]
152
+ addopts = "--quiet"
153
+ minversion = "6.0"
154
+ testpaths = ["tests"]
@@ -0,0 +1,69 @@
1
+ from __future__ import annotations
2
+
3
+ from io import BytesIO
4
+ from os import urandom
5
+ from time import time_ns
6
+ from typing import Final
7
+
8
+ RANDOM_BUFFER_SIZE: int = 128 * 1024 # 128 KiB
9
+ """
10
+ The size of the buffer when requesting random bytes from the operating system.
11
+ """
12
+
13
+ _random_buffer: Final[BytesIO] = BytesIO()
14
+
15
+ _last_time: int = -1
16
+ _last_sequence: int = -1
17
+
18
+
19
+ def zid() -> int:
20
+ """
21
+ Generate a unique identifier.
22
+ """
23
+ global _last_time, _last_sequence
24
+
25
+ # UNIX timestamp in milliseconds
26
+ time: int = time_ns() // 1_000_000
27
+ if time > 0x7FFFFFFFFFFF:
28
+ raise OverflowError('Time value is too large')
29
+
30
+ # CSPRNG-initialized sequence numbers
31
+ sequence: int
32
+ if _last_time == time:
33
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
34
+ else:
35
+ rand: bytes = _random_buffer.read(2)
36
+ if len(rand) < 2:
37
+ _random_buffer.write(urandom(RANDOM_BUFFER_SIZE))
38
+ _random_buffer.seek(0)
39
+ rand = _random_buffer.read(2)
40
+ _last_sequence = sequence = int.from_bytes(rand)
41
+ _last_time = time
42
+
43
+ return (time << 16) | sequence
44
+
45
+
46
+ def _zid_simple() -> int:
47
+ global _last_time, _last_sequence
48
+
49
+ # UNIX timestamp in milliseconds
50
+ time: int = time_ns() // 1_000_000
51
+ if time > 0x7FFFFFFFFFFF:
52
+ raise OverflowError('Time value is too large')
53
+
54
+ # CSPRNG-initialized sequence numbers
55
+ sequence: int
56
+ if _last_time == time:
57
+ _last_sequence = sequence = (_last_sequence + 1) & 0xFFFF
58
+ else:
59
+ _last_sequence = sequence = int.from_bytes(urandom(2))
60
+ _last_time = time
61
+
62
+ return (time << 16) | sequence
63
+
64
+
65
+ def parse_zid_timestamp(zid: int) -> int:
66
+ """
67
+ Extract the UNIX timestamp in milliseconds from a ZID.
68
+ """
69
+ return zid >> 16