Skip to content

Commit

Permalink
Add apply_overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
barneydobson committed Feb 23, 2024
1 parent e3b66fe commit 790f562
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
14 changes: 13 additions & 1 deletion tests/test_arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ def test_decay_arc_alt(self):
d1["phosphate"] = d1["phosphate"] * (1 - 0.005 * 1.005 ** (10 - 20))
self.assertDictAlmostEqual(d1, arc1.queue[0])


def test_apply_overrides(self):
node1, node2, _, _, _, arc1, _, _, _ = self.get_simple_model1()
arc1.apply_overrides({'name' : 'new_name'})

self.assertEqual('new_name', arc1.name)
assert 'new_name' in node1.out_arcs.keys()
assert 'new_name' in node1.out_arcs_type['Waste'].keys()
assert 'arc1' not in node1.out_arcs.keys()

assert 'new_name' in node2.in_arcs.keys()
assert 'new_name' in node2.in_arcs_type['Storage'].keys()
assert 'arc1' not in node2.in_arcs.keys()

if __name__ == "__main__":
unittest.main()
52 changes: 51 additions & 1 deletion wsimod/arcs/arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from wsimod.core import constants
from wsimod.core.core import DecayObj, WSIObj

from typing import Any, Dict
# from wsimod.nodes import nodes #Complains about circular imports.
# I don't think it should do..

Expand Down Expand Up @@ -39,7 +40,7 @@ def __init__(
out_port: A WSIMOD node object where the arc ends
"""
# Default essential parameters
self.name = name
self._name = name
self.in_port = in_port
self.out_port = out_port
self.capacity = capacity
Expand Down Expand Up @@ -82,6 +83,55 @@ def __init__(
self.mass_balance_out = [lambda: self.vqip_out]
self.mass_balance_ds = [lambda: self.empty_vqip()]

@property
def name(self):
"""Name of arc."""
return self._name

@name.setter
def name(self,
value: str):
"""Set the name of the arc.
Args:
value (str): New name for the arc.
"""
if not isinstance(value, str):
return

del self.in_port.out_arcs[self._name]
self.in_port.out_arcs[value] = self

del self.out_port.in_arcs[self._name]
self.out_port.in_arcs[value] = self

out_type = self.out_port.__class__.__name__
in_type = self.in_port.__class__.__name__

del self.in_port.out_arcs_type[out_type][self._name]
self.in_port.out_arcs_type[out_type][value] = self

del self.out_port.in_arcs_type[in_type][self._name]
self.out_port.in_arcs_type[in_type][value] = self

self._name = value

def apply_overrides(self,
overrides: Dict[str, Any] = {}) -> None:
"""Apply overrides to the node.
Args:
overrides (dict, optional): Dictionary of overrides. Defaults to {}.
Example:
>>> my_node.apply_overrides({'name': 'new_name'})
"""
for key, value in overrides.items():
if hasattr(self, key):
setattr(self, key, value)
else:
raise AttributeError(f"{key} not found in {self.__class__.__name__}")

def arc_mass_balance(self):
"""Checks mass balance for inflows/outflows/storage change in an arc.
Expand Down
19 changes: 18 additions & 1 deletion wsimod/nodes/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Converted to totals on Thur Apr 21 2022
"""
import logging
from typing import Any, Dict

from wsimod.arcs.arcs import AltQueueArc, DecayArcAlt
from wsimod.core import constants
Expand Down Expand Up @@ -76,7 +77,23 @@ def __init__(self, name, data_input_dict=None):
# Mass balance checking
self.mass_balance_in = [self.total_in]
self.mass_balance_out = [self.total_out]
self.mass_balance_ds = [lambda: self.empty_vqip()]
self.mass_balance_ds = [lambda: self.empty_vqip()]

def apply_overrides(self,
overrides: Dict[str, Any] = {}) -> None:
"""Apply overrides to the node.
Args:
overrides (dict, optional): Dictionary of overrides. Defaults to {}.
Example:
>>> my_node.apply_overrides({'name': 'new_name'})
"""
for key, value in overrides.items():
if hasattr(self, key):
setattr(self, key, value)
else:
raise AttributeError(f"{key} not found in {self.__class__.__name__}")

def total_in(self):
"""Sum flow and pollutant amounts entering a node via in_arcs.
Expand Down

0 comments on commit 790f562

Please sign in to comment.