|
2 | 2 |
|
3 | 3 | Do you want to repeat a task over a range of inputs? Loop over your task function! |
4 | 4 |
|
5 | | -:::{important} |
6 | | -Before v0.2.0, pytask supported only one approach to repeat tasks. It is also called |
7 | | -parametrizations, and similarly to pytest, it uses a |
8 | | -{func}`@pytask.mark.parametrize <pytask.mark.parametrize>` decorator. If you want to |
9 | | -know more about it, you can find it |
10 | | -{doc}`here <../how_to_guides/repeating_tasks_with_different_inputs_the_pytest_way>`. |
11 | | - |
12 | | -Here you find the new and preferred approach. |
13 | | -::: |
14 | | - |
15 | 5 | ## An example |
16 | 6 |
|
17 | 7 | We reuse the task from the previous {doc}`tutorial <write_a_task>`, which generates |
@@ -71,9 +61,40 @@ and the name of the task function. Here is an example. |
71 | 61 | ``` |
72 | 62 |
|
73 | 63 | This behavior would produce duplicate ids for parametrized tasks. By default, |
74 | | -auto-generated ids are used which are explained {ref}`here <auto-generated-ids>`. |
| 64 | +auto-generated ids are used. |
| 65 | + |
| 66 | +(auto-generated-ids)= |
| 67 | + |
| 68 | +### Auto-generated ids |
| 69 | + |
| 70 | +pytask construct ids by extending the task name with representations of the values used |
| 71 | +for each iteration. Booleans, floats, integers, and strings enter the task id directly. |
| 72 | +For example, a task function that receives four arguments, `True`, `1.0`, `2`, and |
| 73 | +`"hello"`, one of each dtype, has the following id. |
| 74 | + |
| 75 | +``` |
| 76 | +task_data_preparation.py::task_create_random_data[True-1.0-2-hello] |
| 77 | +``` |
| 78 | + |
| 79 | +Arguments with other dtypes cannot be converted to strings and, thus, are replaced with |
| 80 | +a combination of the argument name and the iteration counter. |
75 | 81 |
|
76 | | -More powerful are user-defined ids. |
| 82 | +For example, the following function is parametrized with tuples. |
| 83 | + |
| 84 | +```python |
| 85 | +for i in [(0,), (1,)]: |
| 86 | + |
| 87 | + @pytask.mark.task |
| 88 | + def task_create_random_data(i=i): |
| 89 | + pass |
| 90 | +``` |
| 91 | + |
| 92 | +Since the tuples are not converted to strings, the ids of the two tasks are |
| 93 | + |
| 94 | +``` |
| 95 | +task_data_preparation.py::task_create_random_data[i0] |
| 96 | +task_data_preparation.py::task_create_random_data[i1] |
| 97 | +``` |
77 | 98 |
|
78 | 99 | (ids)= |
79 | 100 |
|
|
0 commit comments