Skip to content

Commit

Permalink
Merge pull request #93 from pyiron/expose_dict_values_as_list
Browse files Browse the repository at this point in the history
Expose dict values as list
  • Loading branch information
liamhuber authored Nov 28, 2023
2 parents 56a7dfc + c0f929f commit 4487bb3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 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
29 changes: 21 additions & 8 deletions tests/unit/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sys import version_info

from pyiron_workflow.channels import (
InputData, InputSignal, OutputData, OutputSignal
InputData, InputSignal, OutputData, OutputSignal, ChannelConnectionError
)
from pyiron_workflow.io import Inputs, Outputs, Signals

Expand All @@ -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 @@ -65,11 +65,15 @@ def test_assignment(self):
)

def test_connection(self):
self.input.x = self.input.y
with self.assertRaises(
ChannelConnectionError,
msg="Shouldn't be allowed to connect two inputs"
):
self.input.x = self.input.y
self.assertEqual(
0,
len(self.input.x.connections),
msg="Shouldn't be allowed to connect two inputs, but only passes warning"
msg="Sanity check that the above error-raising connection never got made"
)

self.input.x = self.output.a
Expand All @@ -79,8 +83,8 @@ def test_connection(self):
msg="Should be able to create connections by assignment"
)

self.input.x = 7
self.assertEqual(self.input.x.value, 7)
self.input.x = 7.
self.assertEqual(self.input.x.value, 7.)

self.input.y = self.output.a
disconnected = self.input.disconnect()
Expand Down Expand Up @@ -138,8 +142,17 @@ 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 TestDataIO(TestCase):
class TestSignalIO(TestCase):
def setUp(self) -> None:
node = DummyNode()

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 4487bb3

Please sign in to comment.