Skip to content

Commit

Permalink
Handle pairs with elements of different type
Browse files Browse the repository at this point in the history
  • Loading branch information
rafguns committed Apr 30, 2019
1 parent db561cb commit 58ff2e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions linkpred/evaluation/scoresheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ def __init__(self, *args):
@staticmethod
def _sorted_tuple(t):
a, b = t
return (a, b) if a > b else (b, a)
try:
return (a, b) if a > b else (b, a)
except TypeError:
# Different node types. This does not hande all possible edge
# cases but should be enough for most real-world scenarios.
return (a, b) if str(a) > str(b) else (b, a)

def __eq__(self, other):
try:
Expand All @@ -145,7 +150,6 @@ def __lt__(self, other):
try:
return self.elements < other.elements
except AttributeError:

return self.elements < self._sorted_tuple(other)

def __gt__(self, other):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_scoresheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def test_pair_identical_elements():
Pair('a', 'a')


def test_pair_different_types():
# Should not raise an error
assert_equal(Pair('a', 1), Pair(1, 'a'))


def test_scoresheet():
sheet = Scoresheet()
t = ('a', 'b')
Expand Down

0 comments on commit 58ff2e1

Please sign in to comment.