Skip to content

Commit

Permalink
Let Node(label=name) == name
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Nov 8, 2023
1 parent aa66673 commit a765742
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/oemof/network/network/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def __init__(self, label=None, *, custom_properties=None, **kwargs):
self._outputs = Outputs(self)

def __eq__(self, other):
return id(self) == id(other)
try:
return self.label == other.label
except AttributeError:
return self.label == other

def __lt__(self, other):
return str(self) < str(other)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_network_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,18 @@ def test_comparision():
node1 = Node(label=2)
node2 = Node(label=-5)

assert node0 == 0
assert 2 == node1
assert node0 < node1
assert node0 > node2


def test_dict_access():
node0 = Node(label="label_0")
test_dict = {node0: 5}
assert test_dict[node0] == 5
assert test_dict["label_0"] == 5

test_dict = {"label_0": 5}
assert test_dict[node0] == 5
assert test_dict["label_0"] == 5

0 comments on commit a765742

Please sign in to comment.