Skip to content

Commit

Permalink
Make values easily accessible as a list
Browse files Browse the repository at this point in the history
  • Loading branch information
liamhuber committed Nov 27, 2023
1 parent f60cca5 commit 31868ee
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pyiron_workflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ def _assign_a_non_channel_value(self, channel: DataChannel, value) -> None:
def to_value_dict(self):
return {label: channel.value for label, channel in self.channel_dict.items()}

def to_list(self):
"""A list of channel values (order not guaranteed)"""
return list(channel.value for channel in self.channel_dict.values())

@property
def ready(self):
return all([c.ready for c in self])
Expand Down
4 changes: 4 additions & 0 deletions pyiron_workflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def __setstate__(self, state):
for k, v in state.items():
self.__dict__[k] = v

def to_list(self):
"""A list of values (order not guaranteed)"""
return list(self.values())


class SeabornColors:
"""
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class TestDataIO(TestCase):
def setUp(self) -> None:
node = DummyNode()
self.inputs = [
InputData(label="x", node=node, default=0, type_hint=float),
InputData(label="y", node=node, default=1, type_hint=float)
InputData(label="x", node=node, default=0., type_hint=float),
InputData(label="y", node=node, default=1., type_hint=float)
]
outputs = [
OutputData(label="a", node=node, type_hint=float),
Expand Down Expand Up @@ -138,6 +138,15 @@ def test_connections_property(self):
"connection"
)

def test_to_list(self):
self.assertListEqual(
[0., 1.],
self.input.to_list(),
msg="Expected a shortcut to channel values. Order is explicitly not "
"guaranteed in the docstring, but it would be nice to appear in the "
"order the channels are added here"
)

@skipUnless(version_info[0] == 3 and version_info[1] >= 10, "Only supported for 3.10+")
class TestSignalIO(TestCase):
def setUp(self) -> None:
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ def test_dot_dict(self):

self.assertEqual(dd['foo'], dd.foo, msg="Dot access should be equivalent.")
dd.bar = "towel"
self.assertEqual("towel", dd["bar"], msg="Dot assignment should be equivalent.")
self.assertEqual("towel", dd["bar"], msg="Dot assignment should be equivalent.")

self.assertListEqual(dd.to_list(), [42, "towel"])

0 comments on commit 31868ee

Please sign in to comment.