Skip to content

Commit bb255b5

Browse files
committed
favour using task_help method instead of pprint(fields_dict(...))
1 parent 2da3a9b commit bb255b5

File tree

6 files changed

+30
-38
lines changed

6 files changed

+30
-38
lines changed

docs/source/tutorial/4-python.ipynb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"\n",
2626
"# Note that we use PascalCase because the object returned by the decorator is actually a class\n",
2727
"@python.define\n",
28-
"def MyFirstTaskDef(a, b):\n",
28+
"def MyFirstTask(a, b):\n",
2929
" \"\"\"Sample function for testing\"\"\"\n",
3030
" return a + b"
3131
]
@@ -45,7 +45,7 @@
4545
"outputs": [],
4646
"source": [
4747
"# Instantiate the task, setting all parameters\n",
48-
"my_first_task = MyFirstTaskDef(a=1, b=2.0)\n",
48+
"my_first_task = MyFirstTask(a=1, b=2.0)\n",
4949
"\n",
5050
"# Execute the task\n",
5151
"outputs = my_first_task()\n",
@@ -69,12 +69,12 @@
6969
"outputs": [],
7070
"source": [
7171
"@python.define(outputs=[\"c\", \"d\"])\n",
72-
"def NamedOutputTaskDef(a, b):\n",
72+
"def NamedOutputTask(a, b):\n",
7373
" \"\"\"Sample function for testing\"\"\"\n",
7474
" return a + b, a - b\n",
7575
"\n",
7676
"\n",
77-
"named_output_task = NamedOutputTaskDef(a=2, b=1)\n",
77+
"named_output_task = NamedOutputTask(a=2, b=1)\n",
7878
"\n",
7979
"outputs = named_output_task()\n",
8080
"\n",
@@ -102,7 +102,7 @@
102102
" \"d\": python.out(type=float, help=\"the difference of the inputs\"),\n",
103103
" },\n",
104104
")\n",
105-
"def AugmentedTaskDef(a, b):\n",
105+
"def AugmentedTask(a, b):\n",
106106
" \"\"\"Sample function for testing\"\"\"\n",
107107
" return a + b, a - b"
108108
]
@@ -163,13 +163,12 @@
163163
"metadata": {},
164164
"outputs": [],
165165
"source": [
166-
"from pprint import pprint\n",
167-
"from pydra.utils import fields_dict\n",
166+
"from pydra.utils import task_help\n",
168167
"\n",
169168
"\n",
170169
"@python.define(outputs=[\"c\", \"d\"])\n",
171-
"def DocStrDef(a: int, b: float) -> tuple[float, float]:\n",
172-
" \"\"\"Sample function for testing\n",
170+
"def DocStrExample(a: int, b: float) -> tuple[float, float]:\n",
171+
" \"\"\"Example python task with help strings pulled from doc-string\n",
173172
"\n",
174173
" Args:\n",
175174
" a: First input\n",
@@ -183,8 +182,7 @@
183182
" return a + b, a * b\n",
184183
"\n",
185184
"\n",
186-
"pprint(fields_dict(DocStrDef))\n",
187-
"pprint(fields_dict(DocStrDef.Outputs))"
185+
"task_help(DocStrExample)"
188186
]
189187
},
190188
{

docs/source/tutorial/5-shell.ipynb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@
134134
"metadata": {},
135135
"outputs": [],
136136
"source": [
137-
"from pprint import pprint\n",
138-
"from pydra.utils import fields_dict\n",
137+
"from pydra.utils import task_help\n",
139138
"\n",
140139
"Cp = shell.define(\n",
141140
" \"cp <in_fs_objects:fs-object+> <out|out_dir:directory> \"\n",
@@ -145,8 +144,7 @@
145144
" \"--tuple-arg <tuple_arg:int,str*> \"\n",
146145
")\n",
147146
"\n",
148-
"pprint(fields_dict(Cp))\n",
149-
"pprint(fields_dict(Cp.Outputs))"
147+
"task_help(Cp)"
150148
]
151149
},
152150
{
@@ -164,6 +162,8 @@
164162
"metadata": {},
165163
"outputs": [],
166164
"source": [
165+
"from pydra.utils import fields_dict\n",
166+
"\n",
167167
"Cp = shell.define(\n",
168168
" \"cp <in_fs_objects:fs-object+> <out|out_dir:directory> \"\n",
169169
" \"-R<recursive=True> \"\n",
@@ -275,8 +275,7 @@
275275
")\n",
276276
"\n",
277277
"\n",
278-
"pprint(fields_dict(Cp))\n",
279-
"pprint(fields_dict(Cp.Outputs))"
278+
"task_help(Cp)"
280279
]
281280
},
282281
{
@@ -390,7 +389,7 @@
390389
"outputs": [],
391390
"source": [
392391
"from fileformats.generic import File\n",
393-
"from pydra.utils import task_fields\n",
392+
"from pydra.utils import task_help\n",
394393
"\n",
395394
"ACommand = shell.define(\n",
396395
" \"a-command\",\n",
@@ -407,9 +406,7 @@
407406
" },\n",
408407
")\n",
409408
"\n",
410-
"\n",
411-
"print(f\"ACommand input fields: {task_fields(ACommand)}\")\n",
412-
"print(f\"ACommand input fields: {task_fields(ACommand.Outputs)}\")"
409+
"task_help(ACommand)"
413410
]
414411
},
415412
{

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
"metadata": {},
4747
"outputs": [],
4848
"source": [
49-
"from pprint import pprint\n",
50-
"from pydra.utils import fields_dict\n",
49+
"from pydra.utils import task_help\n",
5150
"from pydra.compose import python\n",
5251
"\n",
5352
"\n",
@@ -79,8 +78,7 @@
7978
" return a + b, a / b\n",
8079
"\n",
8180
"\n",
82-
"pprint(fields_dict(CanonicalPythonTask))\n",
83-
"pprint(fields_dict(CanonicalPythonTask.Outputs))"
81+
"task_help(CanonicalPythonTask)"
8482
]
8583
},
8684
{
@@ -128,8 +126,7 @@
128126
" return a + b, a / b\n",
129127
"\n",
130128
"\n",
131-
"pprint(fields_dict(CanonicalPythonTask))\n",
132-
"pprint(fields_dict(CanonicalPythonTask.Outputs))"
129+
"task_help(CanonicalPythonTask)"
133130
]
134131
},
135132
{
@@ -178,8 +175,7 @@
178175
" out_file_size: int = shell.out(callable=get_file_size)\n",
179176
"\n",
180177
"\n",
181-
"pprint(fields_dict(CpWithSize))\n",
182-
"pprint(fields_dict(CpWithSize.Outputs))"
178+
"task_help(CpWithSize)"
183179
]
184180
},
185181
{

pydra/engine/tests/test_task.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pydra.utils.messenger import FileMessenger, PrintMessenger, collect_messages
1414
from pydra.engine.audit import AuditFlag
1515
from pydra.engine.hooks import TaskHooks
16-
from pydra.utils.general import task_fields, print_help
16+
from pydra.utils.general import task_fields, task_help
1717
from pydra.engine.submitter import Submitter
1818
from pydra.engine.job import Job
1919
from pydra.utils.general import default_run_cache_dir
@@ -90,7 +90,7 @@ def TestFunc(a: int, b: float = 0.1) -> float:
9090
outputs = funky()
9191
assert outputs.out_out == 2.1
9292

93-
help = print_help(funky)
93+
help = task_help(funky)
9494
assert help == [
9595
"Help for TestFunc",
9696
"Input Parameters:",
@@ -148,7 +148,7 @@ def TestFunc(
148148
assert hasattr(outputs, "integer")
149149
assert outputs.integer == 3
150150

151-
help = print_help(funky)
151+
help = task_help(funky)
152152
assert help == [
153153
"Help for TestFunc",
154154
"Input Parameters:",
@@ -450,7 +450,7 @@ def TestFunc(a, b) -> int:
450450
assert not Path(cache_dir / f"python-{funky._hash}").exists()
451451
outputs = funky(cache_dir=cache_dir)
452452
assert outputs.out == 31
453-
help = print_help(funky)
453+
help = task_help(funky)
454454

455455
assert help == [
456456
"Help for TestFunc",
@@ -491,7 +491,7 @@ def TestFunc(a, b) -> tuple[int, int]:
491491
assert not Path(cache_dir / f"python-{funky._hash}" / "_result.pklz").exists()
492492
outputs = funky(cache_dir=cache_dir)
493493
assert outputs.out1 == 12
494-
help = print_help(funky)
494+
help = task_help(funky)
495495

496496
assert help == [
497497
"Help for TestFunc",

pydra/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
task_fields,
33
fields_dict,
44
plot_workflow,
5-
print_help,
5+
task_help,
66
)
77
from ._version import __version__
88

9-
__all__ = ["__version__", "task_fields", "plot_workflow", "print_help", "fields_dict"]
9+
__all__ = ["__version__", "task_fields", "plot_workflow", "task_help", "fields_dict"]

pydra/utils/general.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def from_list_if_single(obj: ty.Any) -> ty.Any:
297297
return obj
298298

299299

300-
def print_help(defn: "Task[TaskType]") -> list[str]:
300+
def task_help(defn: "Task[TaskType]", to_list: bool = False) -> list[str] | None:
301301
"""Visit a job object and print its input/output interface."""
302302
from pydra.compose.base import NO_DEFAULT
303303

@@ -326,8 +326,9 @@ def print_help(defn: "Task[TaskType]") -> list[str]:
326326
except AttributeError:
327327
name = str(f.type)
328328
lines += [f"- {f.name}: {name}"]
329+
if to_list:
330+
return lines
329331
print("\n".join(lines))
330-
return lines
331332

332333

333334
def position_sort(args):

0 commit comments

Comments
 (0)