Skip to content

Commit

Permalink
add missing magic methods for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Ferland committed Aug 13, 2020
1 parent 8c7634b commit 813e947
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="py-xid",
version="0.2.0",
version="0.3.0",
description="",
url="http://github.com/alexferl/xid",
author="Alexandre Ferland",
Expand Down
20 changes: 12 additions & 8 deletions tests/test_xid.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,6 @@ def test_from_bytes_invariant():
assert xid1.bytes() == xid2.bytes()


def test_sort():
xid1 = XID()
xid2 = XID()

assert xid1 < xid2
assert xid2 > xid1


def test_repr():
xid = XID(
id_=bytes(
Expand All @@ -125,3 +117,15 @@ def test_pass_timestamp():
def test_pass_wrong_type():
with pytest.raises(TypeError):
XID(id_=[])


def test_compare():
xid1 = XID()
xid2 = XID()

assert (xid1 == xid2) is False
assert xid1 != xid2
assert xid1 < xid2
assert xid1 <= xid2
assert xid2 > xid1
assert xid2 >= xid1
21 changes: 18 additions & 3 deletions xid/xid.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,33 @@ def counter(self) -> int:
def bytes(self) -> bytes:
return self.id

def __repr__(self):
return "XID('%s')" % self.__str__()

def __str__(self):
return self.string()

def __hash__(self):
return hash(self.id)

def __eq__(self, other: XID) -> bool:
return self.string() == other.string()

def __ne__(self, other: XID) -> bool:
return self.string() != other.string()

def __lt__(self, other: XID) -> bool:
return self.string() < other.string()

def __le__(self, other: XID) -> bool:
return self.string() <= other.string()

def __gt__(self, other: XID) -> bool:
return self.string() > other.string()

def __ge__(self, other: XID) -> bool:
return self.string() >= other.string()

def __repr__(self):
return "XID('%s')" % self.__str__()


def _uint8(n: int) -> int:
return ctypes.c_uint8(n).value

0 comments on commit 813e947

Please sign in to comment.