Skip to content

Commit

Permalink
Add comparatives __gt__ and __ge__
Browse files Browse the repository at this point in the history
  • Loading branch information
oselz committed Mar 26, 2024
1 parent 4716d9e commit d66a4f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 14 additions & 5 deletions tests/test_typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ def test_construct_typeid() -> None:


def test_compare_typeid() -> None:
prefix = "plov"
suffix = "00041061050r3gg28a1c60t3gf"
prefix_1 = "plov"
suffix_1 = "00041061050r3gg28a1c60t3gf"
prefix_2 = "abcd"
suffix_2 = "00000000000000000000000000"

typeid_1 = TypeID(prefix=prefix, suffix=suffix)
typeid_2 = TypeID(prefix=prefix, suffix=suffix)
typeid_3 = TypeID(suffix=suffix)
typeid_1 = TypeID(prefix=prefix_1, suffix=suffix_1)
typeid_2 = TypeID(prefix=prefix_1, suffix=suffix_1)
typeid_3 = TypeID(suffix=suffix_1)
typeid_4 = TypeID(prefix=prefix_2, suffix=suffix_1)
typeid_5 = TypeID(prefix=prefix_1, suffix=suffix_2)

assert typeid_1 == typeid_2
assert typeid_1 <= typeid_2
assert typeid_1 >= typeid_2
assert typeid_3 != typeid_1
assert typeid_3 < typeid_1
assert typeid_4 <= typeid_1
assert typeid_1 > typeid_5


def test_construct_type_from_string() -> None:
Expand Down
10 changes: 10 additions & 0 deletions typeid/typeid.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def __eq__(self, value: object) -> bool:
return False
return value.prefix == self.prefix and value.suffix == self.suffix

def __gt__(self, other):
if isinstance(other, TypeID):
return str(self) > str(other)
return NotImplemented

def __ge__(self, other):
if isinstance(other, TypeID):
return str(self) >= str(other)
return NotImplemented

def __hash__(self) -> int:
return hash((self.prefix, self.suffix))

Expand Down

0 comments on commit d66a4f2

Please sign in to comment.