From 31868ee5af3a9f977311fbd6338e7e07aa99ea5d Mon Sep 17 00:00:00 2001 From: liamhuber Date: Mon, 27 Nov 2023 14:28:42 -0800 Subject: [PATCH] Make values easily accessible as a list --- pyiron_workflow/io.py | 4 ++++ pyiron_workflow/util.py | 4 ++++ tests/unit/test_io.py | 13 +++++++++++-- tests/unit/test_util.py | 4 +++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pyiron_workflow/io.py b/pyiron_workflow/io.py index 9a540be4..2462aa71 100644 --- a/pyiron_workflow/io.py +++ b/pyiron_workflow/io.py @@ -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]) diff --git a/pyiron_workflow/util.py b/pyiron_workflow/util.py index eff32b27..b7ab487e 100644 --- a/pyiron_workflow/util.py +++ b/pyiron_workflow/util.py @@ -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: """ diff --git a/tests/unit/test_io.py b/tests/unit/test_io.py index c03e5341..6aa23614 100644 --- a/tests/unit/test_io.py +++ b/tests/unit/test_io.py @@ -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), @@ -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: diff --git a/tests/unit/test_util.py b/tests/unit/test_util.py index f25df9f8..57c2abd9 100644 --- a/tests/unit/test_util.py +++ b/tests/unit/test_util.py @@ -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.") \ No newline at end of file + self.assertEqual("towel", dd["bar"], msg="Dot assignment should be equivalent.") + + self.assertListEqual(dd.to_list(), [42, "towel"])