Skip to content

Commit

Permalink
flatten-fix (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
maddenp authored Apr 30, 2024
1 parent 05343b5 commit a61b877
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 41 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,6 @@ There's lots to see during the first invocation. Most of the tasks start and end
[2023-10-19T11:49:43] INFO A spoon: Final state: Ready
```

```
[2023-10-19T11:49:43] INFO A cup: Initial state: Not Ready
[2023-10-19T11:49:43] INFO A cup: Checking requirements
[2023-10-19T11:49:43] INFO A cup: Requirement(s) ready
[2023-10-19T11:49:43] INFO A cup: Executing
[2023-10-19T11:49:43] INFO A cup: Final state: Ready
```

We will see in subsequent workflow invocations that these tasks are not revisited, as their assets will be found to be ready.

The on-disk workflow state is:
Expand Down
2 changes: 1 addition & 1 deletion recipe/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
],
"run": []
},
"version": "0.8.0"
"version": "0.8.1"
}
2 changes: 1 addition & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package:
name: iotaa
version: 0.8.0
version: 0.8.1
source:
path: ../src
build:
Expand Down
28 changes: 9 additions & 19 deletions src/iotaa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def update_from_task(self, taskname: str, assets: _AssetT) -> None:
:param taskname: The current task's name.
:param assets: An asset, a collection of assets, or None.
"""
alist = _listify(assets)
alist = _flatten(assets)
self.assets.update({a.ref: a.ready for a in alist})
self.edges |= set((taskname, a.ref) for a in alist)
self.tasks.add(taskname)
Expand Down Expand Up @@ -499,13 +499,18 @@ def _execute(g: Generator, taskname: str) -> None:
pass


def _flatten(assets: _AssetT) -> List[Asset]:
def _flatten(assets: Union[_AssetT, Dict[str, _AssetT], List[_AssetT]]) -> List[Asset]:
"""
Return a simple list of assets formed by collapsing potentially nested lists.
Return a simple list of assets formed by collapsing potentially nested collections.
:param assets: An asset, a collection of assets, or None.
"""
return list(filter(None, chain(*[_listify(a) for a in _listify(assets)])))
if assets is None:
return []
if isinstance(assets, Asset):
return [assets]
xs = assets if isinstance(assets, list) else assets.values()
return list(filter(None, chain.from_iterable(_flatten(x) for x in xs)))


def _formatter(prog: str) -> HelpFormatter:
Expand All @@ -531,21 +536,6 @@ def _i_am_top_task() -> bool:
return True


def _listify(assets: _AssetT) -> List[Asset]:
"""
Return a list representation of the provided asset(s) (may be empty).
:param assets: An asset, a collection of assets, or None.
"""
if assets is None:
return []
if isinstance(assets, Asset):
return [assets]
if isinstance(assets, dict):
return list(assets.values())
return assets


def _parse_args(raw: List[str]) -> Namespace:
"""
Parse command-line arguments.
Expand Down
17 changes: 5 additions & 12 deletions src/iotaa/tests/test_iotaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ def test__flatten():
assert iotaa._flatten(a) == [a]
assert iotaa._flatten([a, a]) == [a, a]
assert iotaa._flatten({"foo": a, "bar": a}) == [a, a]
assert iotaa._flatten([None, a, [a, a], {"foo": a, "bar": a}]) == [a, a, a, a, a]


def test__formatter():
Expand All @@ -593,14 +594,6 @@ def test__i_am_top_task(val):
assert iotaa._i_am_top_task() == val


def test__listify():
a = iotaa.asset(ref=None, ready=lambda: True)
assert iotaa._listify(assets=None) == []
assert iotaa._listify(assets=a) == [a]
assert iotaa._listify(assets=[a]) == [a]
assert iotaa._listify(assets={"a": a}) == [a]


@pytest.mark.parametrize("graph", [None, "-g", "--graph"])
@pytest.mark.parametrize("tasks", [None, "-t", "--tasks"])
@pytest.mark.parametrize("verbose", [None, "-v", "--verbose"])
Expand Down Expand Up @@ -693,10 +686,10 @@ def test__show_tasks(capsys, task_class):

@pytest.mark.parametrize("assets", simple_assets())
def test__task_final(assets):
for a in iotaa._listify(assets):
for a in iotaa._flatten(assets):
assert getattr(a, "taskname", None) is None
assets = iotaa._task_final(False, "task", assets)
for a in iotaa._listify(assets):
for a in iotaa._flatten(assets):
assert getattr(a, "taskname") == "task"


Expand Down Expand Up @@ -770,7 +763,7 @@ def test__Graph_reset():
def test__Graph_update_from_requirements(assets, empty_graph):
taskname_req = "req"
taskname_this = "task"
alist = iotaa._listify(assets)
alist = iotaa._flatten(assets)
edges = {
0: set(),
1: {(taskname_this, taskname_req), (taskname_req, "foo")},
Expand All @@ -792,7 +785,7 @@ def test__Graph_update_from_task(assets, empty_graph):
iotaa._graph.update_from_task(taskname, assets)
assert all(a() for a in iotaa._graph.assets.values())
assert iotaa._graph.tasks == {taskname}
assert iotaa._graph.edges == {(taskname, x.ref) for x in iotaa._listify(assets)}
assert iotaa._graph.edges == {(taskname, x.ref) for x in iotaa._flatten(assets)}


# _State tests
Expand Down

0 comments on commit a61b877

Please sign in to comment.