Skip to content

Commit 8dd5429

Browse files
committed
renamed task_def_as/from_dict to task_class_as/from_dict
1 parent 5f9e2d4 commit 8dd5429

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

docs/source/tutorial/7-canonical-form.ipynb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@
246246
"\n",
247247
"As well as the dataclass-like canonical form, it is also possible to represent all tasks\n",
248248
"in a nested dictionary form, which could be written to a static file (e.g. in JSON or\n",
249-
"YAML format). The dictionary form of a class can be generated by the `pydra.utils.task_def_as_dict`\n",
249+
"YAML format). The dictionary form of a class can be generated by the `pydra.utils.task_class_as_dict`\n",
250250
"function. For example, the following shell command"
251251
]
252252
},
@@ -278,9 +278,9 @@
278278
"outputs": [],
279279
"source": [
280280
"from pprint import pprint\n",
281-
"from pydra.utils import task_def_as_dict\n",
281+
"from pydra.utils import task_class_as_dict\n",
282282
"\n",
283-
"my_cmd_dict = task_def_as_dict(MyCmd)\n",
283+
"my_cmd_dict = task_class_as_dict(MyCmd)\n",
284284
"\n",
285285
"pprint(my_cmd_dict)"
286286
]
@@ -301,7 +301,7 @@
301301
"metadata": {},
302302
"outputs": [],
303303
"source": [
304-
"cp_with_size_dict = task_def_as_dict(CpFileWithSize)\n",
304+
"cp_with_size_dict = task_class_as_dict(CpFileWithSize)\n",
305305
"\n",
306306
"pprint(cp_with_size_dict)"
307307
]
@@ -311,7 +311,7 @@
311311
"metadata": {},
312312
"source": [
313313
"To unserialize the general dictionary form back into a Task class, you can use the\n",
314-
"`pydra.utils.task_def_from_dict` method"
314+
"`pydra.utils.task_class_from_dict` method"
315315
]
316316
},
317317
{
@@ -320,9 +320,9 @@
320320
"metadata": {},
321321
"outputs": [],
322322
"source": [
323-
"from pydra.utils import task_def_from_dict\n",
323+
"from pydra.utils import task_class_from_dict\n",
324324
"\n",
325-
"ReloadedCpFileWithSize = task_def_from_dict(cp_with_size_dict)"
325+
"ReloadedCpFileWithSize = task_class_from_dict(cp_with_size_dict)"
326326
]
327327
},
328328
{

pydra/utils/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
show_workflow,
66
task_help,
77
print_help,
8-
task_def_as_dict,
9-
task_def_from_dict,
8+
task_class_as_dict,
9+
task_class_from_dict,
1010
)
1111
from ._version import __version__
1212

@@ -18,6 +18,6 @@
1818
"show_workflow",
1919
"task_help",
2020
"print_help",
21-
"task_def_as_dict",
22-
"task_def_from_dict",
21+
"task_class_as_dict",
22+
"task_class_from_dict",
2323
]

pydra/utils/general.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -587,21 +587,21 @@ def get_plugin_classes(namespace: types.ModuleType, class_name: str) -> dict[str
587587
}
588588

589589

590-
def task_def_as_dict(
591-
task_def: "type[Task]",
590+
def task_class_as_dict(
591+
task_class: "type[Task]",
592592
filter: ty.Callable[[attrs.Attribute, ty.Any], bool] | None = None,
593593
value_serializer: (
594594
ty.Callable[[ty.Any, attrs.Attribute, ty.Any], ty.Any] | None
595595
) = None,
596596
**kwargs: ty.Any,
597597
) -> ty.Dict[str, ty.Any]:
598598
"""Converts a Pydra task class into a dictionary representation that can be serialized
599-
and saved to a file, then read and passed to an appropriate `pydra.compose.*.define`
599+
(e.g. to a file), then reread and passed to an appropriate `pydra.compose.*.define`
600600
method to recreate the task.
601601
602602
Parameters
603603
----------
604-
task_def : type[pydra.compose.base.Task]
604+
task_class : type[pydra.compose.base.Task]
605605
The Pydra task class to convert.
606606
filter : callable, optional
607607
A function to filter out certain attributes from the task definition passed
@@ -626,54 +626,54 @@ def task_def_as_dict(
626626
if filter is None:
627627
filter = _filter_out_defaults
628628

629-
input_fields = task_fields(task_def)
630-
executor = input_fields.pop(task_def._executor_name).default
629+
input_fields = task_fields(task_class)
630+
executor = input_fields.pop(task_class._executor_name).default
631631
input_dicts = [
632632
attrs.asdict(i, filter=filter, value_serializer=value_serializer, **kwargs)
633633
for i in input_fields
634634
if (
635635
not isinstance(i, Out) # filter out outarg fields
636-
and i.name not in task_def.BASE_ATTRS
636+
and i.name not in task_class.BASE_ATTRS
637637
)
638638
]
639639
output_dicts = [
640640
attrs.asdict(o, filter=filter, value_serializer=value_serializer, **kwargs)
641-
for o in task_fields(task_def.Outputs)
642-
if o.name not in task_def.Outputs.BASE_ATTRS
641+
for o in task_fields(task_class.Outputs)
642+
if o.name not in task_class.Outputs.BASE_ATTRS
643643
]
644644
dct = {
645-
"type": task_def._task_type(),
646-
task_def._executor_name: executor,
647-
"name": task_def.__name__,
645+
"type": task_class._task_type(),
646+
task_class._executor_name: executor,
647+
"name": task_class.__name__,
648648
"inputs": {d.pop("name"): d for d in input_dicts},
649649
"outputs": {d.pop("name"): d for d in output_dicts},
650650
}
651-
class_attrs = {a: getattr(task_def, "_" + a) for a in task_def.TASK_CLASS_ATTRS}
651+
class_attrs = {a: getattr(task_class, "_" + a) for a in task_class.TASK_CLASS_ATTRS}
652652
if value_serializer:
653-
attrs_fields = {f.name: f for f in attrs.fields(task_def)}
653+
attrs_fields = {f.name: f for f in attrs.fields(task_class)}
654654
class_attrs = {
655-
n: value_serializer(task_def, attrs_fields[n], v)
655+
n: value_serializer(task_class, attrs_fields[n], v)
656656
for n, v in class_attrs.items()
657657
}
658658
dct.update(class_attrs)
659659

660660
return dct
661661

662662

663-
def task_def_from_dict(task_def_dict: dict[str, ty.Any]) -> type["Task"]:
664-
"""Unserializes a task definition from a dictionary created by `task_def_as_dict`
663+
def task_class_from_dict(task_class_dict: dict[str, ty.Any]) -> type["Task"]:
664+
"""Unserializes a task definition from a dictionary created by `task_class_as_dict`
665665
666666
Parameters
667667
----------
668-
task_def_dict: dict[str, Any]
668+
task_class_dict: dict[str, Any]
669669
the dictionary representation to unserialize
670670
671671
Returns
672672
-------
673673
type[pydra.compose.base.Task]
674674
the unserialized task class
675675
"""
676-
dct = copy(task_def_dict)
676+
dct = copy(task_class_dict)
677677
task_type = dct.pop("type")
678678
mod = importlib.import_module(f"pydra.compose.{task_type}")
679679
return mod.define(dct.pop(mod.Task._executor_name), **dct)

pydra/utils/tests/test_general.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from pydra.compose import python, workflow, shell
22
from fileformats.generic import File
3-
from pydra.utils.general import task_def_as_dict, task_def_from_dict, task_fields
3+
from pydra.utils.general import task_class_as_dict, task_class_from_dict, task_fields
44
from pydra.utils.tests.utils import SpecificFuncTask, Concatenate
55

66

7-
def test_python_task_def_as_dict():
7+
def test_python_task_class_as_dict():
88

99
@python.define(outputs=["out_int"], xor=["b", "c"])
1010
def Add(a: int, b: int | None = None, c: int | None = None) -> int:
@@ -25,23 +25,23 @@ def Add(a: int, b: int | None = None, c: int | None = None) -> int:
2525
"""
2626
return a + (b if b is not None else c)
2727

28-
dct = task_def_as_dict(Add)
29-
Reloaded = task_def_from_dict(dct)
28+
dct = task_class_as_dict(Add)
29+
Reloaded = task_class_from_dict(dct)
3030
assert task_fields(Add) == task_fields(Reloaded)
3131

3232

33-
def test_shell_task_def_as_dict():
33+
def test_shell_task_class_as_dict():
3434

3535
MyCmd = shell.define(
3636
"my-cmd <in_file> <out|out_file> --an-arg <an_arg:int=2> --a-flag<a_flag>"
3737
)
3838

39-
dct = task_def_as_dict(MyCmd)
40-
Reloaded = task_def_from_dict(dct)
39+
dct = task_class_as_dict(MyCmd)
40+
Reloaded = task_class_from_dict(dct)
4141
assert task_fields(MyCmd) == task_fields(Reloaded)
4242

4343

44-
def test_workflow_task_def_as_dict():
44+
def test_workflow_task_class_as_dict():
4545

4646
@workflow.define
4747
def AWorkflow(in_file: File, a_param: int) -> tuple[File, File]:
@@ -53,6 +53,6 @@ def AWorkflow(in_file: File, a_param: int) -> tuple[File, File]:
5353
)
5454
return concatenate.out_file
5555

56-
dct = task_def_as_dict(AWorkflow)
57-
Reloaded = task_def_from_dict(dct)
56+
dct = task_class_as_dict(AWorkflow)
57+
Reloaded = task_class_from_dict(dct)
5858
assert task_fields(AWorkflow) == task_fields(Reloaded)

0 commit comments

Comments
 (0)