typeid-python 0.2.1__tar.gz → 0.2.2__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.

Potentially problematic release.


This version of typeid-python might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typeid-python
3
- Version: 0.2.1
3
+ Version: 0.2.2
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
@@ -82,9 +82,9 @@ This particular implementation provides an pip package that can be used by any P
82
82
  - Create TypeID from string:
83
83
 
84
84
  ```python
85
- from typeid import from_string
85
+ from typeid import TypeID
86
86
 
87
- typeid = from_string("user_01h45ytscbebyvny4gc8cr8ma2")
87
+ typeid = TypeID.from_string("user_01h45ytscbebyvny4gc8cr8ma2")
88
88
 
89
89
  print(str(typeid)) # "user_01h45ytscbebyvny4gc8cr8ma2"
90
90
  ```
@@ -92,13 +92,13 @@ This particular implementation provides an pip package that can be used by any P
92
92
  - Create TypeID from uuid7:
93
93
 
94
94
  ```python
95
- from typeid import from_uuid
95
+ from typeid import TypeID
96
96
  from uuid6 import uuid7
97
97
 
98
98
  uuid = uuid7() # UUID('01890bf0-846f-7762-8605-5a3abb40e0e5')
99
99
  prefix = "user"
100
100
 
101
- typeid = from_uuid(prefix=prefix, suffix=uuid)
101
+ typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid)
102
102
 
103
103
  print(str(typeid)) # "user_01h45z113fexh8c1at7axm1r75"
104
104
  ```
@@ -59,9 +59,9 @@ This particular implementation provides an pip package that can be used by any P
59
59
  - Create TypeID from string:
60
60
 
61
61
  ```python
62
- from typeid import from_string
62
+ from typeid import TypeID
63
63
 
64
- typeid = from_string("user_01h45ytscbebyvny4gc8cr8ma2")
64
+ typeid = TypeID.from_string("user_01h45ytscbebyvny4gc8cr8ma2")
65
65
 
66
66
  print(str(typeid)) # "user_01h45ytscbebyvny4gc8cr8ma2"
67
67
  ```
@@ -69,13 +69,13 @@ This particular implementation provides an pip package that can be used by any P
69
69
  - Create TypeID from uuid7:
70
70
 
71
71
  ```python
72
- from typeid import from_uuid
72
+ from typeid import TypeID
73
73
  from uuid6 import uuid7
74
74
 
75
75
  uuid = uuid7() # UUID('01890bf0-846f-7762-8605-5a3abb40e0e5')
76
76
  prefix = "user"
77
77
 
78
- typeid = from_uuid(prefix=prefix, suffix=uuid)
78
+ typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid)
79
79
 
80
80
  print(str(typeid)) # "user_01h45z113fexh8c1at7axm1r75"
81
81
  ```
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "typeid-python"
3
- version = "0.2.1"
3
+ version = "0.2.2"
4
4
  description = "Python implementation of TypeIDs: type-safe, K-sortable, and globally unique identifiers inspired by Stripe IDs"
5
5
  authors = ["Murad Akhundov <akhundov1murad@gmail.com>"]
6
6
  license = "MIT"
@@ -18,6 +18,16 @@ class TypeID:
18
18
  self._prefix = prefix or ""
19
19
  self._suffix = suffix
20
20
 
21
+ @classmethod
22
+ def from_string(cls, string: str):
23
+ prefix, suffix = get_prefix_and_suffix(string=string)
24
+ return cls(suffix=suffix, prefix=prefix)
25
+
26
+ @classmethod
27
+ def from_uuid(cls, suffix: UUID, prefix: Optional[str] = None):
28
+ suffix_str = _convert_uuid_to_b32(suffix)
29
+ return TypeID(suffix=suffix_str, prefix=prefix)
30
+
21
31
  @property
22
32
  def suffix(self) -> str:
23
33
  return self._suffix
@@ -26,6 +36,10 @@ class TypeID:
26
36
  def prefix(self) -> str:
27
37
  return self._prefix
28
38
 
39
+ @property
40
+ def uuid(self) -> UUID:
41
+ return _convert_b32_to_uuid(self.suffix)
42
+
29
43
  def __str__(self) -> str:
30
44
  value = ""
31
45
  if self.prefix:
@@ -38,15 +52,18 @@ class TypeID:
38
52
  return False
39
53
  return value.prefix == self.prefix and value.suffix == self.suffix
40
54
 
55
+ def __hash__(self) -> int:
56
+ return hash((self.prefix, self.suffix))
57
+
41
58
 
42
59
  def from_string(string: str) -> TypeID:
43
- prefix, suffix = get_prefix_and_suffix(string=string)
44
- return TypeID(suffix=suffix, prefix=prefix)
60
+ """Consider TypeID.from_string instead."""
61
+ return TypeID.from_string(string=string)
45
62
 
46
63
 
47
64
  def from_uuid(suffix: UUID, prefix: Optional[str] = None) -> TypeID:
48
- suffix_str = _convert_uuid_to_b32(suffix)
49
- return TypeID(suffix=suffix_str, prefix=prefix)
65
+ """Consider TypeID.from_uuid instead."""
66
+ return TypeID.from_uuid(suffix=suffix, prefix=prefix)
50
67
 
51
68
 
52
69
  def get_prefix_and_suffix(string: str) -> tuple:
@@ -66,3 +83,7 @@ def get_prefix_and_suffix(string: str) -> tuple:
66
83
 
67
84
  def _convert_uuid_to_b32(uuid_instance: UUID) -> str:
68
85
  return base32.encode(list(uuid_instance.bytes))
86
+
87
+
88
+ def _convert_b32_to_uuid(b32: str) -> UUID:
89
+ return UUID(bytes=bytes(base32.decode(b32)))
File without changes