Skip to content

Commit 3de7a22

Browse files
committed
renamed task_class_to/from_dict to un/serialize_task_class
1 parent 2f77b22 commit 3de7a22

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
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_class_as_dict`\n",
249+
"YAML format). The dictionary form of a class can be generated by the `pydra.utils.serialize_task_class`\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_class_as_dict\n",
281+
"from pydra.utils import serialize_task_class\n",
282282
"\n",
283-
"my_cmd_dict = task_class_as_dict(MyCmd)\n",
283+
"my_cmd_dict = serialize_task_class(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_class_as_dict(CpFileWithSize)\n",
304+
"cp_with_size_dict = serialize_task_class(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_class_from_dict` method"
314+
"`pydra.utils.unserialize_task_class` method"
315315
]
316316
},
317317
{
@@ -320,9 +320,9 @@
320320
"metadata": {},
321321
"outputs": [],
322322
"source": [
323-
"from pydra.utils import task_class_from_dict\n",
323+
"from pydra.utils import unserialize_task_class\n",
324324
"\n",
325-
"ReloadedCpFileWithSize = task_class_from_dict(cp_with_size_dict)"
325+
"ReloadedCpFileWithSize = unserialize_task_class(cp_with_size_dict)"
326326
]
327327
},
328328
{

pydra/compose/base/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def extract_fields_from_class(
414414

415415
input_helps, _ = parse_doc_string(klass.__doc__)
416416

417-
def get_fields(klass, field_type, auto_attribs, helps) -> dict[str, Field]:
417+
def extract_fields(klass, field_type, auto_attribs, helps) -> dict[str, Field]:
418418
"""Get the fields from a class"""
419419
fields_dict = {}
420420
# Get fields defined in base classes if present
@@ -468,7 +468,7 @@ def get_fields(klass, field_type, auto_attribs, helps) -> dict[str, Field]:
468468
f"tasks, {klass} must inherit from {spec_type}"
469469
)
470470

471-
inputs = get_fields(klass, arg_type, auto_attribs, input_helps)
471+
inputs = extract_fields(klass, arg_type, auto_attribs, input_helps)
472472

473473
try:
474474
outputs_klass = klass.Outputs
@@ -483,7 +483,7 @@ def get_fields(klass, field_type, auto_attribs, helps) -> dict[str, Field]:
483483
)
484484

485485
output_helps, _ = parse_doc_string(outputs_klass.__doc__)
486-
outputs = get_fields(outputs_klass, out_type, auto_attribs, output_helps)
486+
outputs = extract_fields(outputs_klass, out_type, auto_attribs, output_helps)
487487

488488
return inputs, outputs
489489

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_class_as_dict,
9-
task_class_from_dict,
8+
serialize_task_class,
9+
unserialize_task_class,
1010
)
1111
from ._version import __version__
1212

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

pydra/utils/general.py

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

589589

590-
def task_class_as_dict(
590+
def serialize_task_class(
591591
task_class: "type[Task]",
592592
filter: ty.Callable[[attrs.Attribute, ty.Any], bool] | None = None,
593593
value_serializer: (
@@ -666,8 +666,8 @@ def task_class_as_dict(
666666
return dct
667667

668668

669-
def task_class_from_dict(task_class_dict: dict[str, ty.Any]) -> type["Task"]:
670-
"""Unserializes a task definition from a dictionary created by `task_class_as_dict`
669+
def unserialize_task_class(task_class_dict: dict[str, ty.Any]) -> type["Task"]:
670+
"""Unserializes a task definition from a dictionary created by `serialize_task_class`
671671
672672
Parameters
673673
----------

pydra/utils/tests/test_general.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import attrs
33
from pydra.compose import python, workflow, shell
44
from fileformats.text import TextFile
5-
from pydra.utils.general import task_class_as_dict, task_class_from_dict, task_fields
5+
from pydra.utils.general import (
6+
serialize_task_class,
7+
unserialize_task_class,
8+
task_fields,
9+
)
610
from pydra.utils.tests.utils import Concatenate
711

812

@@ -26,28 +30,28 @@ def Add(a: int, b: int | None = None, c: int | None = None) -> int:
2630
return a + (b if b is not None else c)
2731

2832

29-
def test_python_task_class_as_dict(tmp_path):
33+
def test_python_serialize_task_class(tmp_path):
3034

31-
dct = task_class_as_dict(Add)
32-
Reloaded = task_class_from_dict(dct)
35+
dct = serialize_task_class(Add)
36+
Reloaded = unserialize_task_class(dct)
3337
assert task_fields(Add) == task_fields(Reloaded)
3438

3539
add = Reloaded(a=1, b=2)
3640
assert add(cache_root=tmp_path / "cache").out_int == 3
3741

3842

39-
def test_shell_task_class_as_dict():
43+
def test_shell_serialize_task_class():
4044

4145
MyCmd = shell.define(
4246
"my-cmd <in_file> <out|out_file> --an-arg <an_arg:int=2> --a-flag<a_flag>"
4347
)
4448

45-
dct = task_class_as_dict(MyCmd)
46-
Reloaded = task_class_from_dict(dct)
49+
dct = serialize_task_class(MyCmd)
50+
Reloaded = unserialize_task_class(dct)
4751
assert task_fields(MyCmd) == task_fields(Reloaded)
4852

4953

50-
def test_workflow_task_class_as_dict(tmp_path):
54+
def test_workflow_serialize_task_class(tmp_path):
5155

5256
@workflow.define(outputs=["out_file"])
5357
def AWorkflow(in_file: TextFile, a_param: int) -> TextFile:
@@ -56,8 +60,8 @@ def AWorkflow(in_file: TextFile, a_param: int) -> TextFile:
5660
)
5761
return concatenate.out_file
5862

59-
dct = task_class_as_dict(AWorkflow)
60-
Reloaded = task_class_from_dict(dct)
63+
dct = serialize_task_class(AWorkflow)
64+
Reloaded = unserialize_task_class(dct)
6165
assert task_fields(AWorkflow) == task_fields(Reloaded)
6266

6367
foo_file = tmp_path / "file1.txt"
@@ -67,7 +71,7 @@ def AWorkflow(in_file: TextFile, a_param: int) -> TextFile:
6771
assert outputs.out_file.contents == "foo\nfoo\nfoo\nfoo"
6872

6973

70-
def test_task_class_as_dict_with_value_serializer():
74+
def test_serialize_task_class_with_value_serializer():
7175

7276
def frozen_set_to_list_serializer(
7377
mock_class: ty.Any, atr: attrs.Attribute, value: ty.Any
@@ -79,5 +83,5 @@ def frozen_set_to_list_serializer(
7983
)
8084
return value
8185

82-
dct = task_class_as_dict(Add, value_serializer=frozen_set_to_list_serializer)
86+
dct = serialize_task_class(Add, value_serializer=frozen_set_to_list_serializer)
8387
assert dct["xor"] == [["b", "c"]] or dct["xor"] == [["c", "b"]]

0 commit comments

Comments
 (0)