1
1
# Repeating tasks with different inputs - The pytest way
2
2
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
-
10
3
:::{important}
11
4
This guide shows you how to parametrize tasks with the pytest approach. For the new and
12
5
preferred approach, see this
13
6
{doc}` tutorial <../tutorials/repeating_tasks_with_different_inputs> ` .
14
7
:::
15
8
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
17
10
your task function!
18
11
12
+ :::{hint}
13
+ The process of repeating a function with different inputs is called parametrizations.
14
+ :::
15
+
19
16
:::{seealso}
20
17
If 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.
23
20
:::
24
21
25
22
## An example
26
23
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.
29
26
30
27
First, we write the task for one seed.
31
28
@@ -61,12 +58,12 @@ specifies the name of a task function argument.
61
58
The signature is explained in detail {ref}` below <parametrize-signature> ` .
62
59
:::
63
60
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.
67
64
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 ` .
70
67
71
68
:::{note}
72
69
If 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
77
74
78
75
## Un-parametrized dependencies
79
76
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
81
78
{func}` @pytask.mark.depends_on <pytask.mark.depends_on> ` .
82
79
83
80
``` python
@@ -95,10 +92,10 @@ def task_create_random_data(seed, produces):
95
92
96
93
## The signature
97
94
98
- The signature can be passed in three different formats.
95
+ pytask allows for three different kinds of formats for the signature .
99
96
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
102
99
readability. Here are some examples:
103
100
104
101
``` python
@@ -114,41 +111,41 @@ The signature can be passed in three different formats.
114
111
(" first_argument" , " second_argument" )
115
112
```
116
113
117
- 1 . Finally, it is also possible to use a list of strings.
114
+ 1 . Finally, using a list of strings is also possible .
118
115
119
116
``` python
120
117
[" first_argument" , " second_argument" ]
121
118
```
122
119
123
120
## The id
124
121
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.
128
125
Here is an example.
129
126
130
127
```
131
128
../task_example.py::task_example
132
129
```
133
130
134
131
This 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.
136
133
137
134
(auto-generated-ids)=
138
135
139
136
### Auto-generated ids
140
137
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.
145
142
146
143
```
147
144
task_example.py::task_example[True-1.0-2-hello]
148
145
```
149
146
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.
152
149
153
150
For example, the following function is parametrized with tuples.
154
151
@@ -192,10 +189,10 @@ task_example.py::task_example[second] # (1,)
192
189
To change the representation of tuples and other objects, you can pass a function to the
193
190
` ids ` argument of the {func}` @pytask.mark.parametrize <pytask.mark.parametrize> `
194
191
decorator. 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
196
193
auto-generated value is used.
197
194
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.
199
196
200
197
``` python
201
198
def tuple_to_hash (value ):
@@ -208,7 +205,7 @@ def task_example(i):
208
205
pass
209
206
```
210
207
211
- This produces the following ids:
208
+ The tasks have the following ids:
212
209
213
210
```
214
211
task_example.py::task_example[3430018387555] # (0,)
0 commit comments