Hello! I was wondering if it is possible to map pa...
# ask-community
a
Hello! I was wondering if it is possible to map parameters in dictionary to their inputs of the task. Here’s the example:
Copy code
from prefect import Task, Flow, task

@task()
def task_success():
    print("Success!")
    return {'p': 1, 'k': 2, 'j': 3}


@task()
def task_to_fail(p, k):
    print(f"p: {p}; k: {k}")
    raise NotImplementedError()


with Flow("catch-all-failed-flow", state_handlers=[STATE_HANDLER_CATCH_ALL_FAILED]) as flow:
    ts = task_success()
    tf = task_to_fail(ts) # how can I map p and k params to the task_to_fail task instead of passing the whole dict in p but nothing as k

flow.run()
I know I can do ts[‘p’] and ts[‘k’], but I’m looking for something like **ts Thank you!
k
Hi @Anze Kravanja! You can only map over a list do you likely either need to make a List[Tuple] or List[Dict[ and map over that.
a
I see. Okay thanks. I believe mapping a list will create multiple tasks, one per each mapping. But I just want one task with dict keys mapped to the right task inputs.
k
Will make an example for you
I see what you’re saying now. You can’t do it that way because
ts
is not a dictionary, but it’s a Task. You can only unpack the dictionary value inside the task.
Copy code
from prefect import Task, Flow, task
@task()
def task_success():
    print("Success!")
    return {'p': 1, 'k': 2, 'j': 3}
@task()
def task_to_fail(d):
    def add(p=0, k=0, j=0):
        return p + k + j
    x = add(**d)
    print("The sum is " + str(x))
    return

with Flow("catch-all-failed-flow") as flow:
    ts = task_success()
    tf = task_to_fail(ts) # how can I map p and k params to the task_to_fail task instead of passing the whole dict in p but nothing as k
flow.run()
a
Cool! Yeah, that is a nice solution that should work well for us I believe. Thank you!