From a7657425905b677928c9bf726f7fd8e5f04c6968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Sch=C3=B6nfeldt?= Date: Wed, 8 Nov 2023 21:31:01 +0100 Subject: [PATCH] Let Node(label=name) == name --- src/oemof/network/network/entity.py | 5 ++++- tests/test_network_classes.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/oemof/network/network/entity.py b/src/oemof/network/network/entity.py index f58d792..13fa35d 100644 --- a/src/oemof/network/network/entity.py +++ b/src/oemof/network/network/entity.py @@ -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) diff --git a/tests/test_network_classes.py b/tests/test_network_classes.py index 092a6b0..0e0731d 100644 --- a/tests/test_network_classes.py +++ b/tests/test_network_classes.py @@ -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