11How to parametrize a task
22=========================
33
4- Often times, you define a task which should be repeated over a range of inputs. Just
5- like pytest, pytask has a parametrize decorator for this reason.
4+ Often times, you define a task which should be repeated over a range of inputs. pytask
5+ has a parametrize decorator for this reason.
66
77Since pytask needs to track dependencies and products of tasks, there is a little bit
88more logic bound to the parametrize decorator which is explained in this section.
@@ -20,10 +20,9 @@ First, we write the task for one number.
2020.. code-block :: python
2121
2222 import pytask
23- from pathlib import Path
2423
2524
26- @pytask.mark.produces (Path( " 0.txt" ) )
25+ @pytask.mark.produces (" 0.txt" )
2726 def task_save_number (produces , i = 0 ):
2827 produces.write_text(str (i))
2928
@@ -32,10 +31,9 @@ In the next step, we parametrize the task.
3231.. code-block :: python
3332
3433 import pytask
35- from pathlib import pathlib
3634
3735
38- @pytask.mark.parametrize (" produces, i" , [(Path( f " { i} .txt " ) , i) for i in range (5 )])
36+ @pytask.mark.parametrize (" produces, i" , [(f " { i} .txt " , i) for i in range (5 )])
3937 def task_save_number (produces , i ):
4038 produces.write_text(str (i))
4139
@@ -68,7 +66,7 @@ mapped to the argument ``produces`` and ``i`` receives the number.
6866
6967 .. code-block :: python
7068
71- @pytask.mark.produces (Path( " 1.txt" ) )
69+ @pytask.mark.produces (" 1.txt" )
7270 def task_save_number (produces , i = 1 ):
7371 produces.write_text(str (i))
7472
@@ -82,7 +80,10 @@ example is equivalent to the former example.
8280
8381.. code-block :: python
8482
85- @pytask.mark.parametrize (" produces, i" , [(Path(f " { i} .txt " ), i) for i in range (5 )])
83+ from pathlib import Path
84+
85+
86+ @pytask.mark.parametrize (" produces, i" , [(f " { i} .txt " , i) for i in range (5 )])
8687 def task_save_number (i ):
8788 Path(__file__ ).parent.joinpath(f " { i} .txt " ).write_text(str (i))
8889
@@ -107,7 +108,7 @@ all generated tasks and save it along a generated number to a file.
107108.. code-block :: python
108109
109110 @pytask.mark.depends_on (Path(" additional_text.txt" ))
110- @pytask.mark.parametrize (" produces, i" , [(Path( f " { i} .txt " ) , i) for i in range (5 )])
111+ @pytask.mark.parametrize (" produces, i" , [(f " { i} .txt " , i) for i in range (5 )])
111112 def task_save_number (depends_on , produces , i ):
112113 additional_text = depends_on.read_text()
113114 produces.write_text(additional_text + str (i))
0 commit comments