Skip to content

Commit

Permalink
Make Workflow iterator return the step name as well as the result
Browse files Browse the repository at this point in the history
  • Loading branch information
zeddo123 committed Mar 19, 2024
1 parent baeccb1 commit d459b7f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ as it can work with both just fine.
### Workflows
To add steps to a workflow, the class provides the `add` method which takes as input the function (process) and optionally
an OutputHandler. The whole workflow will act as the `|` (pipe) operator in Unix terminals by successively feeding the output of a process as
the input of the next process.
the input of the next process. To run a workflow, you just need to iterate over it with the appropriate input(s).
```
p1 -> p2 -> p3
```
Expand All @@ -42,7 +42,7 @@ In math terms, the workflow runs the function composition of all the steps.
p3 \circ p2 \circ p1 (x)
```

This shows how to define a simple workflow:
This shows how to define and run a simple workflow:
```python
from octopipes.workflow import Workflow

Expand All @@ -55,7 +55,7 @@ print(wf.nsteps)
# `wf` will first run the input on the first function x ** 2, then run the second x - 4 with the result of the previous step.
# So in the first step of the iteration the result will be 16 (4 ** 2) then 12 (16 - 4)
wf_iter = wf(4)
for result in wf_iter:
for step, result in wf_iter:
pass

# return a frozen instance of the workflow run.
Expand Down
5 changes: 4 additions & 1 deletion octopipes/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def __next__(self):
end = time.perf_counter()
self.outputs.append(self.current_output)
self.durations.append(end - start)
return self.current_output
step_name = self.workflow.processes[self.current_step - 1].__name__

return step_name, self.current_output

raise StopIteration

def _parse_requires(self, requires: str) -> list[Any]:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_workflow():
.add(lambda x: x)
assert wf.nsteps == 2
wf_iter = wf(1)
for result in wf_iter:
for _, result in wf_iter:
assert result == 1

wf_iter.recap()
Expand Down

0 comments on commit d459b7f

Please sign in to comment.