lastuuid 0.1.2__cp311-cp311-win32.whl → 0.2.2__cp311-cp311-win32.whl

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.

Potentially problematic release.


This version of lastuuid might be problematic. Click here for more details.

lastuuid/__init__.py CHANGED
@@ -1,3 +1,9 @@
1
- from .lastuuid import uuid7
1
+ """
2
+ lastuuid API.
3
+ """
4
+ from .lastuuid import uuid7, uuid7_to_datetime
2
5
 
3
- __all__ = ["uuid7"]
6
+ __all__ = [
7
+ "uuid7",
8
+ "uuid7_to_datetime",
9
+ ]
lastuuid/dummies.py CHANGED
@@ -1,8 +1,15 @@
1
- """A dummy uuid usefull for unit testing purpose."""
1
+ """
2
+ A dummy uuid usefull for unit testing purpose.
3
+
4
+ UUID generated here are full of 0, they does not respect any kind of UUID version,
5
+ they remove a bit of cognitive load while testing.
6
+ """
2
7
 
3
8
  from typing import Iterator
4
9
  from uuid import UUID
5
10
 
11
+ __all__ = ["uuidgen"]
12
+
6
13
 
7
14
  def gen_id() -> Iterator[int]:
8
15
  num = 0
@@ -17,6 +24,43 @@ next_id = gen_id()
17
24
  def uuidgen(i: int = 0, j: int = 0, k: int = 0, x: int = 0, y: int = 0) -> UUID:
18
25
  """
19
26
  A UUID generator that makes UUIDs more readable for humans.
27
+
28
+ # Generate autoincrement UUID that you need to predict
29
+
30
+ Sometime you prepare fixtures with well known UUID and you want to repeat them,
31
+
32
+ uuidgen(1) is more readable that UUID('00000001-0000-0000-0000-000000000000'),
33
+ this is why this function is made for.
34
+ Every section of the uuid can be filled out using `i`, `j`, `k`, `x`, `y` but,
35
+ I personnaly never use more than `i` and `j`.
36
+
37
+ ```python
38
+ >>> from lastuuid.dummies import uuidgen
39
+ >>> uuidgen(1)
40
+ UUID('00000001-0000-0000-0000-000000000000')
41
+ >>> uuidgen(1, 2)
42
+ UUID('00000001-0002-0000-0000-000000000000')
43
+ >>> uuidgen(1, 2, 3, 4, 5)
44
+ UUID('00000001-0002-0003-0004-000000000005')
45
+ ```
46
+
47
+ ```{tip}
48
+ if you don't want a dependency for that, the standard library let you write
49
+ UUID(int=1) which produce UUID('00000000-0000-0000-0000-000000000001').
50
+ ```
51
+
52
+ # Generate autoincrement UUID that you don't need to predict
53
+
54
+ Without any parameter, it will generate UUID where the last bits are incremented.
55
+
56
+ ```python
57
+ >>> from lastuuid.dummies import uuidgen
58
+ >>> uuidgen()
59
+ UUID('00000000-0000-0000-0000-000000000001')
60
+ >>> uuidgen()
61
+ UUID('00000000-0000-0000-0000-000000000002')
62
+ ```
63
+
20
64
  """
21
65
  if i == 0 and y == 0:
22
66
  y = next(next_id)
Binary file
lastuuid/lastuuid.pyi CHANGED
@@ -1,9 +1,14 @@
1
1
  """Pyo3 binding interface definition."""
2
2
 
3
3
  from uuid import UUID
4
+ from datetime import datetime, timezone
4
5
 
5
6
 
6
7
  def uuid7() -> UUID:
7
8
  """
8
9
  Generate an uuid using uuidv7 format, the best format that feet in a BTree.
9
10
  """
11
+
12
+
13
+ def uuid7_to_datetime(uuid7: UUID, tz=timezone.utc) -> datetime:
14
+ """Extract the datetime part of an uuid 7."""
lastuuid/utils.py ADDED
@@ -0,0 +1,83 @@
1
+ """
2
+ Utilities methods that may be used for database querying purpose.
3
+
4
+ Because UUIDv7 are timestamped ordered and monotonically increasing,
5
+ there are a good solution for generating primary keys.
6
+
7
+ The design of UUIDv7 feet well with the design of BTree.
8
+
9
+ Because they contains a date time, a UUID range can be compute
10
+ in order to retrieve UUIDs generated at a given time.
11
+
12
+
13
+ ```{important}
14
+ In a distributed system, relying solely on a datetime or UUIDv7 for sorting
15
+ has limitations.
16
+ While UUIDv7 ensures sequential ordering on a single machine, there is no guarantee
17
+ that a UUIDv7 generated later on one machine will be greater than one generated
18
+ earlier on another machine.
19
+
20
+ This documentation does not cover the book Designing Data-Intensive Applications ;).
21
+ ```
22
+ """
23
+
24
+ from datetime import UTC, date, datetime, time, timedelta
25
+ from typing_extensions import Tuple
26
+ from uuid import UUID
27
+
28
+ __all__ = [
29
+ "uuid7_bounds_from_datetime",
30
+ "uuid7_bounds_from_date",
31
+ ]
32
+
33
+
34
+ def _datetime_to_uuid7_lowest(dt: datetime) -> UUID:
35
+ unix_ts_ms = int(dt.timestamp() * 1000)
36
+ version = 0x07
37
+ var = 2
38
+ final_bytes = unix_ts_ms.to_bytes(6)
39
+ final_bytes += (version << 12).to_bytes(2)
40
+ final_bytes += ((var << 62) + 0x3000000000000000).to_bytes(8)
41
+ return UUID(bytes=final_bytes)
42
+
43
+
44
+ def uuid7_bounds_from_datetime(
45
+ dt_lower: datetime,
46
+ dt_upper: datetime | None = None,
47
+ ) -> Tuple[UUID, UUID]:
48
+ """
49
+ Get uuid bound for a half-open interval.
50
+
51
+ This function can be usefull to search for any rows based on a uuid7 in a sql query.
52
+ If one parameter is set, then the search is based on a millisecond, because uuid7
53
+ are only millisecond precision.
54
+
55
+ The returned bound are half open, so the upper bound, from the ``dt_upper`` will
56
+ not include in the result, only the first value to be excluded.
57
+
58
+ If the the second parameter is ommited, then the bound only contains a millisecond,
59
+ of dt_lower.
60
+
61
+ :param dt_lower: the included left bound of the range.
62
+ :param dt_upper: the excluded right bound of the range.
63
+ """
64
+ return _datetime_to_uuid7_lowest(dt_lower), _datetime_to_uuid7_lowest(
65
+ dt_upper or (dt_lower + timedelta(milliseconds=1))
66
+ )
67
+
68
+
69
+ def uuid7_bounds_from_date(dt: date, tz=UTC) -> Tuple[UUID, UUID]:
70
+ """
71
+ Get uuid bound for a particular day.
72
+
73
+ This function can be usefull to search for any rows based on a uuid7 in a sql query.
74
+ The right bound return is the first uuid of the next day that should be excluded.
75
+
76
+ :param dt: the included left bound of the range.
77
+ :param tz: the timezone used to compute the UUID, it should always be ommited.
78
+ """
79
+ return _datetime_to_uuid7_lowest(
80
+ datetime.combine(dt, time=time(tzinfo=tz))
81
+ ), _datetime_to_uuid7_lowest(
82
+ datetime.combine(dt + timedelta(days=1), time=time(tzinfo=tz))
83
+ )
@@ -0,0 +1,98 @@
1
+ Metadata-Version: 2.4
2
+ Name: lastuuid
3
+ Version: 0.2.2
4
+ Classifier: License :: OSI Approved :: MIT License
5
+ Classifier: Programming Language :: Python :: 3
6
+ Classifier: Programming Language :: Rust
7
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
8
+ Classifier: Typing :: Typed
9
+ Requires-Dist: furo>=2024.5.6 ; extra == 'docs'
10
+ Requires-Dist: linkify-it-py>=2.0.3,<3 ; extra == 'docs'
11
+ Requires-Dist: myst-parser>=3.0.0,<4 ; python_full_version < '3.10' and extra == 'docs'
12
+ Requires-Dist: myst-parser>=4.0.0,<5 ; python_full_version >= '3.10' and extra == 'docs'
13
+ Requires-Dist: sphinx>=7.0.1,<8 ; extra == 'docs'
14
+ Requires-Dist: sphinx-autodoc2>=0.5.0,<1 ; extra == 'docs'
15
+ Provides-Extra: docs
16
+ Summary: Fast UUIDv7 Compatible with standard library type, and utils.
17
+ Keywords: uuid,UUIDv7
18
+ Author: Guillaume Gauvrit <guillaume@gauvr.it>
19
+ Author-email: Guillaume Gauvrit <guillaume@gauvr.it>
20
+ License: MIT License
21
+ Requires-Python: >=3.9
22
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
23
+ Project-URL: Homepage, https://github.com/mardiros/lastuuid
24
+ Project-URL: Documentation, https://mardiros.github.io/lastuuid/
25
+ Project-URL: Repository, https://github.com/mardiros/lastuuid.git
26
+ Project-URL: Issues, https://github.com/mardiros/lastuuid/issues
27
+ Project-URL: Changelog, https://mardiros.github.io/lastuuid/changelog.html
28
+
29
+ # lastuuid - yet another uuid library
30
+
31
+ [![Documentation](https://github.com/mardiros/lastuuid/actions/workflows/publish-doc.yml/badge.svg)](https://mardiros.github.io/lastuuid/)
32
+ [![Continuous Integration](https://github.com/mardiros/lastuuid/actions/workflows/tests.yml/badge.svg)](https://github.com/mardiros/lastuuid/actions/workflows/tests.yml)
33
+ [![Maintainability](https://api.codeclimate.com/v1/badges/8e7293fabe7508b2ec6c/maintainability)](https://codeclimate.com/github/mardiros/lastuuid/maintainability)
34
+
35
+ UUID type is awesome, but, at the moment, the UUID type in the standard library
36
+ does not support the uuid7 format.
37
+
38
+ **lastuuid** provide **fast UUIDv7 generation** made by the
39
+ [rust crate uuid7](https://crates.io/crates/uuid7) **compatible with Pydantic**.
40
+
41
+ It has additional features that may helps for testing or inspecting UUIDv7.
42
+
43
+ ```{note}
44
+ lastuuid is a developer joke based on the nature of UUIDv7,
45
+
46
+ where the most recently generated UUID is always the last one when sorted.
47
+ ```
48
+
49
+ ## Usage
50
+
51
+ ### UUID7
52
+
53
+ ```python
54
+ >>> from lastuuid import uuid7
55
+ >>> uuid7()
56
+ UUID('019316cc-f99a-77b3-89d5-ed8c3cf1f50e')
57
+ ```
58
+
59
+ There is no parameter here, the uuid is generated from the current time.
60
+
61
+ The implementation of uuid7 algorithm is made in the uuid7 rust crate.
62
+
63
+ #### Pydantic
64
+
65
+ This lib has been created because all the other library that implement uuid7
66
+ create there own UUID type, so its not easy to use with pydantic.
67
+
68
+ ```python
69
+ from uuid import UUID
70
+ from pydantic import BaseModel, Field
71
+
72
+ from lastuuid import uuid7
73
+
74
+
75
+ class Event(BaseModel):
76
+ id: UUID = Field(default_factory=uuid7)
77
+
78
+ ```
79
+
80
+ #### Performance
81
+
82
+ On my machine the uuid7 is as fast (or slow) as the native uuid4.
83
+
84
+ ```bash
85
+ $ python -m timeit "from lastuuid import uuid7; uuid7()"
86
+ 200000 loops, best of 5: 1.8 usec per loop
87
+
88
+ $ python -m timeit "from uuid import uuid4; uuid4()"
89
+ 200000 loops, best of 5: 1.82 usec per loop
90
+ ```
91
+
92
+ ### Read More
93
+
94
+ There are other usefull function in the library that cab be found in the
95
+ [API documentation](https://mardiros.github.io/lastuuid/).
96
+
97
+ https://mardiros.github.io/lastuuid/
98
+
@@ -0,0 +1,9 @@
1
+ lastuuid-0.2.2.dist-info/METADATA,sha256=_Inh1YdmPXQaKA9R8-o7bSriL_ZwyEYB-6Bx7HwaLA4,3494
2
+ lastuuid-0.2.2.dist-info/WHEEL,sha256=cazBU6xJtRuKVkT3lvqxOZ_3XVozNavIaITeCYtVC60,92
3
+ lastuuid/dummies.py,sha256=DD8trFboMNNEAknUk2SoQH7C15WJAK6MV9FUS_FFCJc,1950
4
+ lastuuid/lastuuid.pyi,sha256=hu4g_4eOmq8ixpQ2E3FhjD1xfdtPqNRJI4iFm6mMCTU,354
5
+ lastuuid/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ lastuuid/utils.py,sha256=Ocx0R9aXmIcj2hgsnwrtvjArFryRiao6BzhWBePX7R8,2917
7
+ lastuuid/__init__.py,sha256=yW5FLV4ITqTqPOxW-_Lha9ge5jjAfDUz2auXqboXnYU,131
8
+ lastuuid/lastuuid.cp311-win32.pyd,sha256=m-2xcbKP7kQtsULPTjvJJJLRA-hc3jIH4UpqCDbTiD8,194560
9
+ lastuuid-0.2.2.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: maturin (1.7.8)
2
+ Generator: maturin (1.8.2)
3
3
  Root-Is-Purelib: false
4
4
  Tag: cp311-cp311-win32
@@ -1,101 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: lastuuid
3
- Version: 0.1.2
4
- Classifier: License :: OSI Approved :: MIT License
5
- Classifier: Programming Language :: Python :: 3
6
- Classifier: Programming Language :: Rust
7
- Summary: UUID gen for the python standard library.
8
- Keywords: environment,substitution,variables
9
- Author: Guillaume Gauvrit <guillaume@gauvr.it>
10
- Author-email: Guillaume Gauvrit <guillaume@gauvr.it>
11
- License: MIT License
12
- Requires-Python: >=3.9
13
- Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
14
- Project-URL: Homepage, https://github.com/mardiros/lastuuid
15
- Project-URL: Documentation, https://github.com/mardiros/lastuuid/blob/main/README.md
16
- Project-URL: Repository, https://github.com/mardiros/lastuuid.git
17
- Project-URL: Issues, https://github.com/mardiros/lastuuid/issues
18
- Project-URL: Changelog, https://github.com/mardiros/lastuuid/blob/main/CHANGELOG.md
19
-
20
- # lastuuid - yet another uuid library
21
-
22
- UUID type is awesome, but, at the moment, the UUID type in the standard library
23
- does not support the uuid7 format.
24
-
25
- This is where lastuuid library is usefull.
26
-
27
- ## Usage
28
-
29
- ### UUID7
30
-
31
- ```python
32
- >>> from lastuuid import uuid7
33
- >>> uuid7()
34
- UUID('019316cc-f99a-77b3-89d5-ed8c3cf1f50e')
35
- ```
36
-
37
- There is no parameter here, the uuid is generated from the current time.
38
-
39
- The implementation of uuid7 algorithm is made in the uuid7 rust crate.
40
-
41
- #### Pydantic
42
-
43
- This lib has been created because all the other library that implement uuid7
44
- create there own UUID type, so its not easy to use with pydantic.
45
-
46
- ```python
47
- from uuid import UUID
48
- from pydantic import BaseModel, Field
49
-
50
- from lastuuid import uuid7
51
-
52
-
53
- class Dummy(BaseModel):
54
- id: UUID = Field(default_factory=uuid7)
55
-
56
- ```
57
-
58
- #### Performance
59
-
60
- On my machine the uuid7 is as fast (or slow) as the native uuid4.
61
-
62
- ```bash
63
- $ python -m timeit "from lastuuid import uuid7; uuid7()"
64
- 200000 loops, best of 5: 1.8 usec per loop
65
-
66
- $ python -m timeit "from uuid import uuid4; uuid4()"
67
- 200000 loops, best of 5: 1.82 usec per loop
68
- ```
69
-
70
- ### Testing with uuid without a brain
71
-
72
- The uuidgen method is not made for production code, it is not suited to be
73
- fast, it is here to generate uuid has autoincrement or as redictable ids,
74
- because UUID are made to create an identifier before it's saved into a
75
- database.
76
-
77
-
78
- Autoincrement your uuid in a test suite avoid some brain pain:
79
-
80
- ```python
81
- >>> from lastuuid.dummies import uuidgen
82
- >>> uuidgen()
83
- UUID('00000000-0000-0000-0000-000000000001')
84
- >>> uuidgen()
85
- UUID('00000000-0000-0000-0000-000000000002')
86
- ```
87
-
88
- Or even more usefull:
89
-
90
- UUID predicted, where only the first bunch of bytes needs to be read; or a few,
91
- to arrange some object ids.
92
-
93
-
94
- ```python
95
- >>> from lastuuid.dummies import uuidgen
96
- >>> uuidgen(1)
97
- UUID('00000001-0000-0000-0000-000000000000')
98
- >>> uuidgen(1, 2, 3, 4, 5)
99
- UUID('00000001-0002-0003-0004-000000000005')
100
- ```
101
-
@@ -1,8 +0,0 @@
1
- lastuuid-0.1.2.dist-info/METADATA,sha256=rvx50UZUSvA6VpX5t24NKyMcqjh9uHu9LI6LVg-uDsI,2870
2
- lastuuid-0.1.2.dist-info/WHEEL,sha256=cIWnBshq9UErEeNtadr9w7TUZu32emQUVzK31LjJSaM,92
3
- lastuuid/dummies.py,sha256=appIhKdQLL_8Mo5Xb0fzYjBV6w3iJrHZzgk-JNAnej0,520
4
- lastuuid/lastuuid.pyi,sha256=UWNZIdqlzkbln21QRVGqfOX8sn6FLJUjbG3jlAT22O4,192
5
- lastuuid/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- lastuuid/__init__.py,sha256=GwcZ4cGLHj95prUoK_-Te0TY3gxoLguXp-OEMIJNJzo,52
7
- lastuuid/lastuuid.cp311-win32.pyd,sha256=P_ajE9DwLWvmSwP8VXAYt-IKolN5Ko2T4QAwF5NGZk0,167936
8
- lastuuid-0.1.2.dist-info/RECORD,,