typeid-python 0.2.3__py3-none-any.whl → 0.3.1__py3-none-any.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 typeid-python might be problematic. Click here for more details.

typeid/typeid.py CHANGED
@@ -1,8 +1,7 @@
1
1
  import warnings
2
2
  from typing import Optional
3
- from uuid import UUID
4
3
 
5
- from uuid6 import uuid7
4
+ import uuid6
6
5
 
7
6
  from typeid import base32
8
7
  from typeid.errors import InvalidTypeIDStringException
@@ -11,7 +10,7 @@ from typeid.validation import validate_prefix, validate_suffix
11
10
 
12
11
  class TypeID:
13
12
  def __init__(self, prefix: Optional[str] = None, suffix: Optional[str] = None) -> None:
14
- suffix = _convert_uuid_to_b32(uuid7()) if not suffix else suffix
13
+ suffix = _convert_uuid_to_b32(uuid6.uuid7()) if not suffix else suffix
15
14
  validate_suffix(suffix=suffix)
16
15
  if prefix:
17
16
  validate_prefix(prefix=prefix)
@@ -25,9 +24,9 @@ class TypeID:
25
24
  return cls(suffix=suffix, prefix=prefix)
26
25
 
27
26
  @classmethod
28
- def from_uuid(cls, suffix: UUID, prefix: Optional[str] = None):
27
+ def from_uuid(cls, suffix: uuid6.UUID, prefix: Optional[str] = None):
29
28
  suffix_str = _convert_uuid_to_b32(suffix)
30
- return TypeID(suffix=suffix_str, prefix=prefix)
29
+ return cls(suffix=suffix_str, prefix=prefix)
31
30
 
32
31
  @property
33
32
  def suffix(self) -> str:
@@ -38,7 +37,7 @@ class TypeID:
38
37
  return self._prefix
39
38
 
40
39
  @property
41
- def uuid(self) -> UUID:
40
+ def uuid(self) -> uuid6.UUID:
42
41
  return _convert_b32_to_uuid(self.suffix)
43
42
 
44
43
  def __str__(self) -> str:
@@ -75,29 +74,29 @@ def from_string(string: str) -> TypeID:
75
74
  return TypeID.from_string(string=string)
76
75
 
77
76
 
78
- def from_uuid(suffix: UUID, prefix: Optional[str] = None) -> TypeID:
77
+ def from_uuid(suffix: uuid6.UUID, prefix: Optional[str] = None) -> TypeID:
79
78
  warnings.warn("Consider TypeID.from_uuid instead.", DeprecationWarning)
80
79
  return TypeID.from_uuid(suffix=suffix, prefix=prefix)
81
80
 
82
81
 
83
82
  def get_prefix_and_suffix(string: str) -> tuple:
84
- parts = string.split("_")
85
- suffix = None
86
- prefix = None
83
+ parts = string.rsplit("_", 1)
87
84
  if len(parts) == 1:
85
+ prefix = None
88
86
  suffix = parts[0]
89
- elif len(parts) == 2 and parts[0] != "":
90
- suffix = parts[1]
91
- prefix = parts[0]
92
- else:
87
+ elif len(parts) == 2 and parts[0] == "":
93
88
  raise InvalidTypeIDStringException(f"Invalid TypeID: {string}")
89
+ else:
90
+ prefix, suffix = parts
94
91
 
95
92
  return prefix, suffix
96
93
 
97
94
 
98
- def _convert_uuid_to_b32(uuid_instance: UUID) -> str:
95
+ def _convert_uuid_to_b32(uuid_instance: uuid6.UUID) -> str:
99
96
  return base32.encode(list(uuid_instance.bytes))
100
97
 
101
98
 
102
- def _convert_b32_to_uuid(b32: str) -> UUID:
103
- return UUID(bytes=bytes(base32.decode(b32)))
99
+ def _convert_b32_to_uuid(b32: str) -> uuid6.UUID:
100
+ uuid_bytes = bytes(base32.decode(b32))
101
+ uuid_int = int.from_bytes(uuid_bytes, byteorder="big")
102
+ return uuid6.UUID(int=uuid_int, version=7)
typeid/validation.py CHANGED
@@ -1,10 +1,13 @@
1
+ import re
2
+
1
3
  from typeid import base32
2
- from typeid.constants import PREFIX_MAX_LEN, SUFFIX_LEN
4
+ from typeid.constants import SUFFIX_LEN
3
5
  from typeid.errors import PrefixValidationException, SuffixValidationException
4
6
 
5
7
 
6
8
  def validate_prefix(prefix: str) -> None:
7
- if not prefix.islower() or not prefix.isascii() or len(prefix) > PREFIX_MAX_LEN or not prefix.isalpha():
9
+ # See https://github.com/jetify-com/typeid/tree/main/spec
10
+ if not re.match("^([a-z]([a-z_]{0,61}[a-z])?)?$", prefix):
8
11
  raise PrefixValidationException(f"Invalid prefix: {prefix}.")
9
12
 
10
13
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typeid-python
3
- Version: 0.2.3
3
+ Version: 0.3.1
4
4
  Summary: Python implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs
5
5
  Home-page: https://github.com/akhundMurad/typeid-python
6
6
  License: MIT
@@ -18,7 +18,7 @@ Classifier: Programming Language :: Python :: 3.10
18
18
  Classifier: Programming Language :: Python :: 3.11
19
19
  Classifier: Programming Language :: Python :: 3.12
20
20
  Provides-Extra: cli
21
- Requires-Dist: uuid6 (>=2023.5.2,<2024.0.0)
21
+ Requires-Dist: uuid6 (>=2023.5.2)
22
22
  Project-URL: Repository, https://github.com/akhundMurad/typeid-python
23
23
  Description-Content-Type: text/markdown
24
24
 
@@ -3,9 +3,9 @@ typeid/base32.py,sha256=UTYXJjb95_nn-vYIpw7nquguBac4GZ0TL0oE6BuujPg,6504
3
3
  typeid/cli.py,sha256=h93_EAOzWO3bqubgL83Q-smHp11lvxMjgAunwyTWwFM,872
4
4
  typeid/constants.py,sha256=2ApV0agNys2yI9NM8oQZbyTR2rgP-VtxXGrM6glBB2Q,37
5
5
  typeid/errors.py,sha256=QPHZ-DJg5OpPOHtuetsqMCYu-E_aHMJZTTeFqx9Gz_4,207
6
- typeid/typeid.py,sha256=jGMZ3FZXSe6x8Ib6AQi5lVbmlb2PWJcwKhXDEScW_pU,2927
7
- typeid/validation.py,sha256=yvLO5lfMkJpduZzql_FJlj4hW6HcWURWYLRQvR92SSM,899
8
- typeid_python-0.2.3.dist-info/LICENSE,sha256=f98oZ9FId4i3835UJYGw066BU8PSc57L1utGWS1ypcs,1070
9
- typeid_python-0.2.3.dist-info/METADATA,sha256=HLC7cDDSYyth-xd1bRDxNeCLPAMI2Pa6jAhz9RzHkaE,4124
10
- typeid_python-0.2.3.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
11
- typeid_python-0.2.3.dist-info/RECORD,,
6
+ typeid/typeid.py,sha256=6deYh-fDaJdcKjs14TVzMVNnfzsU2M9f_4FdqWnlNw4,2996
7
+ typeid/validation.py,sha256=Cb9IkHrK1-HFukFYpSzyNGhllaF3MIZDcD5RnmsfrK8,910
8
+ typeid_python-0.3.1.dist-info/LICENSE,sha256=f98oZ9FId4i3835UJYGw066BU8PSc57L1utGWS1ypcs,1070
9
+ typeid_python-0.3.1.dist-info/METADATA,sha256=3b8dCEoGwAlMgk3GcDuh2j5IoDBCWAEtcVeMQcvk5kQ,4114
10
+ typeid_python-0.3.1.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
11
+ typeid_python-0.3.1.dist-info/RECORD,,