11# Repeating tasks with different inputs - The pytest way
22
3- You want to define a task which should be repeated over a range of inputs? Loop over
4- your task function!
5-
6- :::{hint}
7- The process of repeating a function with different inputs is called parametrizations.
8- :::
9-
103:::{important}
114This guide shows you how to parametrize tasks with the pytest approach. For the new and
125preferred approach, see this
136{doc}` tutorial <../tutorials/repeating_tasks_with_different_inputs> ` .
147:::
158
16- You want to define a task which should be repeated over a range of inputs? Parametrize
9+ Do you want to define a task repeating an action over a range of inputs? Parametrize
1710your task function!
1811
12+ :::{hint}
13+ The process of repeating a function with different inputs is called parametrizations.
14+ :::
15+
1916:::{seealso}
2017If you want to know more about best practices for parametrizations, check out this
21- {doc}` guide <../how_to_guides/bp_scalable_repititions_of_tasks> ` after you made yourself
22- familiar this tutorial.
18+ {doc}` guide <../how_to_guides/bp_scalable_repititions_of_tasks> ` after you have made
19+ yourself familiar with this tutorial.
2320:::
2421
2522## An example
2623
27- We reuse the previous example of a task which generates random data and repeat the same
28- operation over a number of seeds to receive multiple, reproducible samples.
24+ We reuse the previous example of a task that generates random data and repeat the same
25+ operation over some seeds to receive multiple, reproducible samples.
2926
3027First, we write the task for one seed.
3128
@@ -61,12 +58,12 @@ specifies the name of a task function argument.
6158The signature is explained in detail {ref}` below <parametrize-signature> ` .
6259:::
6360
64- The second argument of the parametrize decorator is a list (or any iterable) which has
65- as many elements as there are iterations over the task function. Each element has to
66- provide one value for each argument name in the signature - two in this case.
61+ The second argument of the parametrize decorator is a list with one element per
62+ iteration. Each element must provide one value for each argument name in the signature -
63+ two in this case.
6764
68- Putting all together, the task is executed three times and each run the path from the
69- list is mapped to the argument ` produces ` and ` seed ` receives the seed.
65+ pytask executes the task function three times and passes the path from the list to the
66+ argument ` produces ` and the seed to ` seed ` .
7067
7168:::{note}
7269If you use ` produces ` or ` depends_on ` in the signature of the parametrize decorator, the
@@ -77,7 +74,7 @@ values are handled as if they were attached to the function with
7774
7875## Un-parametrized dependencies
7976
80- To specify a dependency which is the same for all parametrizations, add it with
77+ To specify a dependency that is the same for all parametrizations, add it with
8178{func}` @pytask.mark.depends_on <pytask.mark.depends_on> ` .
8279
8380``` python
@@ -95,10 +92,10 @@ def task_create_random_data(seed, produces):
9592
9693## The signature
9794
98- The signature can be passed in three different formats.
95+ pytask allows for three different kinds of formats for the signature .
9996
100- 1 . The signature can be a comma-separated string like an entry in a csv table. Note that
101- white- space is stripped from each name which you can use to separate the names for
97+ 1 . The signature can be a comma-separated string like an entry in a CSV table. Note that
98+ white space is stripped from each name which you can use to separate the names for
10299 readability. Here are some examples:
103100
104101 ``` python
@@ -114,41 +111,41 @@ The signature can be passed in three different formats.
114111 (" first_argument" , " second_argument" )
115112 ```
116113
117- 1 . Finally, it is also possible to use a list of strings.
114+ 1 . Finally, using a list of strings is also possible .
118115
119116 ``` python
120117 [" first_argument" , " second_argument" ]
121118 ```
122119
123120## The id
124121
125- Every task has a unique id which can be used to
126- {doc}` select it <../tutorials/selecting_tasks> ` . The normal id combines the path to
127- the module where the task is defined, a double colon, and the name of the task function.
122+ Every task has a unique id that can be used to
123+ {doc}` select it <../tutorials/selecting_tasks> ` . The normal id combines the path to the
124+ module where the task is defined, a double colon, and the name of the task function.
128125Here is an example.
129126
130127```
131128../task_example.py::task_example
132129```
133130
134131This behavior would produce duplicate ids for parametrized tasks. Therefore, there exist
135- multiple mechanisms to produce unique ids.
132+ multiple mechanisms to have unique ids.
136133
137134(auto-generated-ids)=
138135
139136### Auto-generated ids
140137
141- To avoid duplicate task ids, the ids of parametrized tasks are extended with
142- descriptions of the values they are parametrized with . Booleans, floats, integers and
143- strings enter the task id directly. For example, a task function which receives four
144- arguments, ` True ` , ` 1.0 ` , ` 2 ` , and ` "hello" ` , one of each dtype, has the following id.
138+ pytask construct ids by extending the task name with representations of the values used
139+ for each iteration . Booleans, floats, integers, and strings enter the task id directly.
140+ For example, a task function that receives four arguments, ` True ` , ` 1.0 ` , ` 2 ` , and
141+ ` "hello" ` , one of each dtype, has the following id.
145142
146143```
147144task_example.py::task_example[True-1.0-2-hello]
148145```
149146
150- Arguments with other dtypes cannot be easily converted to strings and, thus, are
151- replaced with a combination of the argument name and the iteration counter.
147+ Arguments with other dtypes cannot be converted to strings and, thus, are replaced with
148+ a combination of the argument name and the iteration counter.
152149
153150For example, the following function is parametrized with tuples.
154151
@@ -192,10 +189,10 @@ task_example.py::task_example[second] # (1,)
192189To change the representation of tuples and other objects, you can pass a function to the
193190` ids ` argument of the {func}` @pytask.mark.parametrize <pytask.mark.parametrize> `
194191decorator. The function is called for every argument and may return a boolean, number,
195- or string which will be integrated into the id. For every other return, the
192+ or string, which will be integrated into the id. For every other return, the
196193auto-generated value is used.
197194
198- To get a unique representation of a tuple, we can use the hash value .
195+ We can use the hash value to get a unique representation of a tuple.
199196
200197``` python
201198def tuple_to_hash (value ):
@@ -208,7 +205,7 @@ def task_example(i):
208205 pass
209206```
210207
211- This produces the following ids:
208+ The tasks have the following ids:
212209
213210```
214211task_example.py::task_example[3430018387555] # (0,)
0 commit comments