Skip to content

Commit

Permalink
* nemo/utils.py (MultiSetter): New class.
Browse files Browse the repository at this point in the history
  • Loading branch information
bje- committed Dec 14, 2024
1 parent c259e33 commit b4874d3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions nemo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@
register_matplotlib_converters()


class MultiSetter:
"""A setter that broadcasts its value to any number of setters.
This is useful for generator pairs such as a Battery and a
BatteryLoad, where there should only be one (tied) capacity for
both objects.
>>> setter1 = (lambda x: print("one", x), 0, 40)
>>> setter2 = (lambda x: print("two", x), 0, 40)
>>> ds = DualSetter(setter1, setter2)
>>> ds.set_capacity(1.2)
one 1.2
two 1.2
"""

def __init__(self, *args):
"""Initialise a MultiSetter with any number of setters."""
self.setters = []
for setter in args:
if not callable(setter):
raise TypeError
self.setters.append(setter[0])

def set_capacity(self, value):
"""Broadcast value to all setters."""
for setter in self.setters:
setter(value)


def thousands(value):
"""Format a value with thousands separator(s).
Expand Down

0 comments on commit b4874d3

Please sign in to comment.