Skip to content

Commit

Permalink
add classmethod constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
beheh committed Sep 25, 2023
1 parent 04f20ae commit f438060
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ This particular implementation provides an pip package that can be used by any P
- Create TypeID from string:

```python
from typeid import from_string
from typeid import TypeID

typeid = from_string("user_01h45ytscbebyvny4gc8cr8ma2")
typeid = TypeID.from_string("user_01h45ytscbebyvny4gc8cr8ma2")

print(str(typeid)) # "user_01h45ytscbebyvny4gc8cr8ma2"
```

- Create TypeID from uuid7:

```python
from typeid import from_uuid
from typeid import TypeID
from uuid6 import uuid7

uuid = uuid7() # UUID('01890bf0-846f-7762-8605-5a3abb40e0e5')
prefix = "user"

typeid = from_uuid(prefix=prefix, suffix=uuid)
typeid = TypeID.from_uuid(prefix=prefix, suffix=uuid)

print(str(typeid)) # "user_01h45z113fexh8c1at7axm1r75"
```
Expand Down
20 changes: 20 additions & 0 deletions tests/test_typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def test_compare_typeid() -> None:
def test_construct_type_from_string() -> None:
string = "00041061050r3gg28a1c60t3gf"

typeid = TypeID.from_string(string)

assert isinstance(typeid, TypeID)
assert typeid.prefix == ""
assert isinstance(typeid.suffix, str)


def test_construct_type_from_string_standalone() -> None:
string = "00041061050r3gg28a1c60t3gf"

typeid = from_string(string)

assert isinstance(typeid, TypeID)
Expand All @@ -48,6 +58,16 @@ def test_construct_type_from_string() -> None:
def test_construct_type_from_string_with_prefix() -> None:
string = "prefix_00041061050r3gg28a1c60t3gf"

typeid = TypeID.from_string(string)

assert isinstance(typeid, TypeID)
assert typeid.prefix == "prefix"
assert isinstance(typeid.suffix, str)


def test_construct_type_from_string_with_prefix_standalone() -> None:
string = "prefix_00041061050r3gg28a1c60t3gf"

typeid = from_string(string)

assert isinstance(typeid, TypeID)
Expand Down
18 changes: 14 additions & 4 deletions typeid/typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def __init__(self, prefix: Optional[str] = None, suffix: Optional[str] = None) -
self._prefix = prefix or ""
self._suffix = suffix

@classmethod
def from_string(cls, string: str):
prefix, suffix = get_prefix_and_suffix(string=string)
return cls(suffix=suffix, prefix=prefix)

@classmethod
def from_uuid(cls, suffix: UUID, prefix: Optional[str] = None):
suffix_str = _convert_uuid_to_b32(suffix)
return TypeID(suffix=suffix_str, prefix=prefix)

@property
def suffix(self) -> str:
return self._suffix
Expand All @@ -40,13 +50,13 @@ def __eq__(self, value: object) -> bool:


def from_string(string: str) -> TypeID:
prefix, suffix = get_prefix_and_suffix(string=string)
return TypeID(suffix=suffix, prefix=prefix)
"""Consider TypeID.from_string instead."""
return TypeID.from_string(string=string)


def from_uuid(suffix: UUID, prefix: Optional[str] = None) -> TypeID:
suffix_str = _convert_uuid_to_b32(suffix)
return TypeID(suffix=suffix_str, prefix=prefix)
"""Consider TypeID.from_uuid instead."""
return TypeID.from_uuid(suffix=suffix, prefix=prefix)


def get_prefix_and_suffix(string: str) -> tuple:
Expand Down

0 comments on commit f438060

Please sign in to comment.