Skip to content

Selecting tasks

Multiple options exist if you want to run a subset of tasks.

Markers

You can use marker expressions to select tasks if you assign markers to task functions. For example, here is a task with the wip marker, which indicates work-in-progress.

@pytask.mark.wip
def task_1():
    pass

Run this command in your terminal to execute only tasks with the wip marker.

$ pytask -m wip

You can pass more complex expressions to pytask build -m by using multiple markers and and, or, not, and (). The following pattern selects all tasks that belong to the data management but not those that produce plots and plots for the analysis.

$ pytask -m "(data_management and not plots) or (analysis and plots)"

Markers can also store metadata in keyword arguments. For example, the following marker records the data and model used by a prediction task.

@pytask.mark.prediction(data="customer_churn", model="random_forest")
def task_create_predictions():
    pass

Pass keyword arguments in a marker expression to select tasks by this metadata.

$ pytask -m 'prediction(data="customer_churn", model="random_forest")'

A task matches if one marker with the requested name contains all keyword arguments in the expression. The marker can contain additional keyword arguments. Marker expressions support unescaped strings, positive and negative integers, True, False, and None. Keyword arguments are only supported in marker expressions passed to -m.

If you create your markers, use the pytask markers command to register and document them.

Expressions

Expressions are similar to markers and offer the same syntax but target the task ids with pytask build -k. Assume you have the following tasks.

def task_1():
    pass


def task_2():
    pass


def task_12():
    pass

Then, the following command will run the first and third tasks.

$ pytask -k 1

The following command will only execute the first task.

$ pytask -k "1 and not 2"

This command only runs the first two tasks.

$ pytask -k "1 or 2 and not 12"

To execute a single task, say task_run_this_one in task_example.py, use one of the following commands.

$ pytask -k task_example.py::task_run_this_one

$ pytask -k task_run_this_one

Selecting repeated tasks

If you have a parametrized task, you can select individual parametrizations.

from pytask import task


for i in range(2):

    @task
    def task_parametrized(i=i): ...

To run the task where i = 1, run this command.

$ pytask -k "task_parametrized[1]"

pytask uses booleans, floats, integers, and strings in the task id. It replaces other Python objects like tuples with a combination of the argument name and an iteration counter and separates multiple arguments via dashes.

Note

Read this section for more information on how ids for repeated tasks are created and can be customized.