Skip to content

Commit

Permalink
feat(tests): test examples in markdown docs
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Mar 7, 2024
1 parent f9f89f5 commit f2fca1a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-test-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ jobs:
pip install pytest pytest-cov
pip install --no-build-isolation --no-deps --disable-pip-version-check -e .
python -m pytest --doctest-modules
python -m doctest -v docs/**/*.md
31 changes: 31 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,34 @@ steps:
default: 3
run: increment
```
### Programmatic Usage
Building and rendering may be done programmatically,
which provides the opportunity to use custom renderers
and backends, as well as bespoke serialization or formatting.
```python
>>> import sys
>>> import yaml
>>> from dewret.tasks import task, run
>>> from dewret.renderers.cwl import render
>>>
>>> @task()
... def increment(num: int):
... return num + 1
>>>
>>> result = increment(num=3)
>>> workflow = run(result)
>>> cwl = render(workflow)
>>> yaml.dump(cwl, sys.stdout, indent=2)
class: Workflow
cwlVersion: 1.2
steps:
increment-012ef3b3ffb9d15c3f2837aa4bb20a8d:
in:
num:
default: 3
run: increment

```
19 changes: 7 additions & 12 deletions src/dewret/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
Typical usage example:
@task()
def increment(num: int) -> int:
return num + 1
>>> @task()
... def increment(num: int) -> int:
... return num + 1
"""

import importlib
Expand All @@ -41,11 +41,6 @@ class Backend(Enum):

DEFAULT_BACKEND = Backend.DASK

def get_dask_backend() -> BackendModule:
"""Initialize the dask backend."""
from .backends import dask
return dask

class TaskManager:
"""Overarching backend-agnostic task manager.
Expand Down Expand Up @@ -92,7 +87,7 @@ def backend(self) -> BackendModule:
if backend is None:
backend = self.set_backend(DEFAULT_BACKEND)

backend_mod = importlib.import_module(f".backends.{backend.value}", "dewret")
backend_mod = importlib.import_module(f".backends.backend_{backend.value}", "dewret")
return backend_mod

def make_lazy(self) -> LazyFactory:
Expand Down Expand Up @@ -128,9 +123,9 @@ def task() -> Callable[[Target], StepExecution]:
Decorator for the current backend to mark lazy-executable tasks.
For example:
@task()
def increment(num: int) -> int:
return num + 1
>>> @task()
... def increment(num: int) -> int:
... return num + 1
If the backend is `dask` (the default), it is will evaluate this
as a `dask.delayed`. Note that, with any backend, dewret will
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import cast
from dewret.workflow import Lazy
from dewret.backends.dask import lazy, run
from dewret.backends.backend_dask import lazy, run

def inc_task(base: int) -> int:
"""Increments a value by one and returns it."""
Expand Down

0 comments on commit f2fca1a

Please sign in to comment.