Skip to content

Commit

Permalink
chore: Refactored render.py:get_render_method for better readability,…
Browse files Browse the repository at this point in the history
… added explanitory comments
  • Loading branch information
KamenDimitrov97 committed Sep 2, 2024
1 parent c32469e commit f6f478b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
3 changes: 0 additions & 3 deletions docs/renderer_tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,3 @@ RawType = BasicType | list[str] | list["RawType"] | dict[str, "RawType"]
```shell
python snakemake_tasks.py
```

### Q: Should I add a brief description of dewret in step 1? Should link dewret types/docs etc here?
### A: Get details on how that happens and probably yes.
13 changes: 6 additions & 7 deletions src/dewret/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ def __init__(self, fn: Callable[..., Any]):
If `fn` is a class, it takes the constructor, and if it is a method, it takes
the `__func__` attribute.
"""
self.fn = (
fn.__init__
if inspect.isclass(fn) else
fn.__func__
if inspect.ismethod(fn) else
fn
)
if inspect.isclass(fn):
self.fn = fn.__init__
elif inspect.ismethod(fn):
self.fn = fn.__func__
else:
self.fn = fn

@property
def return_type(self) -> Any:
Expand Down
20 changes: 2 additions & 18 deletions src/dewret/backends/backend_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,10 @@ def run(workflow: Workflow | None, task: Lazy | list[Lazy] | tuple[Lazy], thread
Args:
workflow: `Workflow` in which to record the execution.
task: `dask.delayed` function, wrapped by dewret, that we wish to compute.
thread_pool: thread pool for executing the workflows, to allow initialization of configuration contextvars.
thread_pool: custom thread pool for executing workflows, copies in correct values for contextvars to each thread before they are accessed by a dask worker.
**kwargs: any configuration arguments for this backend.
"""
# def _check_delayed(task: Lazy | list[Lazy] | tuple[Lazy]) -> Delayed:
# # We need isinstance to reassure type-checker.
# if isinstance(task, list) or isinstance(task, tuple):
# lst: list[Delayed] | tuple[Delayed, ...] = [_check_delayed(elt) for elt in task]
# if isinstance(task, tuple):
# lst = tuple(lst)
# return delayed(lst)
# elif not isinstance(task, Delayed) or not is_lazy(task):
# raise RuntimeError(
# f"{task} is not a dask delayed, perhaps you tried to mix backends?"
# )
# return task
# computable = _check_delayed(task)
# if not is_lazy(task):
# raise RuntimeError(
# f"{task} is not a dask delayed, perhaps you tried to mix backends?"
# )
# def _check_delayed was here, but we decided to delegate this to dask

if isinstance(task, Delayed) and is_lazy(task):
computable = task
Expand Down
5 changes: 4 additions & 1 deletion src/dewret/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
BasicType = str | float | bool | bytes | int | None
RawType = BasicType | list["RawType"] | dict[str, "RawType"]
FirmType = RawType | list["FirmType"] | dict[str, "FirmType"] | tuple["FirmType", ...]
ExprType = (FirmType | Basic | list["ExprType"] | dict[str, "ExprType"] | tuple["ExprType", ...]) # type: ignore
ExprType = (
FirmType | Basic | list["ExprType"] | dict[str, "ExprType"] | tuple["ExprType", ...]
) # type: ignore

U = TypeVar("U")
T = TypeVar("T")
Expand All @@ -71,6 +73,7 @@ def strip_annotations(parent_type: type) -> tuple[type, tuple[str]]:
return parent_type, tuple(metadata)


# Generic type for configuration settings for the renderer
RenderConfiguration = dict[str, RawType]


Expand Down
21 changes: 11 additions & 10 deletions src/dewret/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ def get_render_method(renderer: Path | RawRenderModule | StructuredRenderModule,
render_module = cast(BaseRenderModule, module)
else:
render_module = renderer
if not isinstance(render_module, StructuredRenderModule):
if not isinstance(render_module, RawRenderModule):
raise NotImplementedError("This render module neither seems to be a structured nor a raw render module.")

if isinstance(render_module, RawRenderModule):
return render_module.render_raw
elif isinstance(render_module, (StructuredRenderModule)):
def _render(workflow: Workflow, render_module: StructuredRenderModule, pretty: bool=False, **kwargs: RenderConfiguration) -> dict[str, str]:
rendered = render_module.render(workflow, **kwargs)
return {
key: structured_to_raw(value, pretty=pretty)
for key, value in rendered.items()
}

def _render(workflow: Workflow, render_module: StructuredRenderModule, pretty: bool=False, **kwargs: RenderConfiguration) -> dict[str, str]:
rendered = render_module.render(workflow, **kwargs)
return {
key: structured_to_raw(value, pretty=pretty)
for key, value in rendered.items()
}
return cast(RenderCall, partial(_render, render_module=render_module, pretty=pretty))

return cast(RenderCall, partial(_render, render_module=render_module, pretty=pretty))
raise NotImplementedError("This render module neither seems to be a structured nor a raw render module.")

def base_render(
workflow: Workflow, build_cb: Callable[[Workflow], T]
Expand Down

0 comments on commit f6f478b

Please sign in to comment.