Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make energy_system.nodes a dict #35

Merged
merged 6 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Changelog

* Improved code quality
* Add Entity.custom_properties
* Simplify node access (energy_system.nodes[label])
14 changes: 7 additions & 7 deletions src/oemof/network/energy_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __init__(self, **kwargs):
g if isinstance(g, Grouping) else Entities(g)
for g in kwargs.get("groupings", [])
]
self._nodes = []
self._nodes = {}

self.results = kwargs.get("results")

Expand All @@ -155,7 +155,7 @@ def __init__(self, **kwargs):

def add(self, *nodes):
"""Add :class:`nodes <oemof.network.Node>` to this energy system."""
self.nodes.extend(nodes)
self._nodes.update({node.label: node for node in nodes})
for n in nodes:
self.signals[type(self).add].send(n, EnergySystem=self)

Expand All @@ -168,20 +168,20 @@ def groups(self):
(
g(n, gs)
for g in self._groupings
for n in self.nodes[self._first_ungrouped_node_index_ :]
for n in list(self.nodes)[self._first_ungrouped_node_index_ :]
),
maxlen=0,
)
self._first_ungrouped_node_index_ = len(self.nodes)
return self._groups

@property
def nodes(self):
def node(self):
p-snft marked this conversation as resolved.
Show resolved Hide resolved
return self._nodes

@nodes.setter
def nodes(self, value):
self._nodes = value
@property
def nodes(self):
return self._nodes.values()

def flows(self):
return {
Expand Down
8 changes: 5 additions & 3 deletions src/oemof/network/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def create_nx_graph(
grph = nx.DiGraph()

# add nodes
for n in energy_system.nodes:
grph.add_node(str(n.label), label=str(n.label))
for label in energy_system.node.keys():
grph.add_node(str(label), label=str(label))

# add labeled flows on directed edge if an optimization_model has been
# passed or undirected edge otherwise
Expand All @@ -125,7 +125,9 @@ def create_nx_graph(
if remove_nodes_with_substrings is not None:
for i in remove_nodes_with_substrings:
remove_nodes = [
str(v.label) for v in energy_system.nodes if i in str(v.label)
str(label)
for label in energy_system.node.keys()
if i in str(label)
]
grph.remove_nodes_from(remove_nodes)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_network_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ def setup_method(self):
def test_entity_registration(self):
n1 = Node(label="<B1>")
self.es.add(n1)
assert self.es.nodes[0] == n1
assert self.es.node["<B1>"] == n1
n2 = Node(label="<B2>")
self.es.add(n2)
assert self.es.nodes[1] == n2
assert self.es.node["<B2>"] == n2
n3 = Node(label="<TF1>", inputs=[n1], outputs=[n2])
self.es.add(n3)
assert n3 in self.es.nodes
assert self.es.node["<TF1>"] == n3


def test_deprecated_classes():
Expand Down
Loading