Hi everyone. I'm trying to use the `ifelse` contro...
# prefect-community
d
Hi everyone. I'm trying to use the
ifelse
control flow to determine which task gets applied over a mapped task. Below, I've attached a simple example. When I run the flow, I don't get an error, but rather, each iteration of the mapped task enters the
true
condition despite there being cases where the condition is clearly false. Can someone point me in the right direction? Am I using the
ifelse
correctly?
Copy code
param = [[], 2, 3, [], 4]

@task
def cond(val):
    if isinstance(val, int):
        return True
    else:
        return False
@task
def print_true(val):
    print(f"Value is {val}")

@task
def print_false(val):
    print("This is a list...")

with Flow('Insert Health System Data') as flow:
    flag = cond.map(param)
    true_task = print_true.map(param)
    false_task = print_false.map(param)
    ifelse(flag, true_task, false_task)

flow.run()
k
Hey @dherincx, Let me get some more information for you. In the meantime, there’s a great Prefect Idiom that demonstrates usage of
ifelse
, which you can read here .
d
@Kyle Moon-Wright, thanks! I'm just curious because I have need to apply the
ifelse
over a task being mapped and I'm unsure how the
false
logic is never entered, even in my simple example
k
Hey there @dherincx, got some insight from the team on this one. At this time, Prefect doesn’t support conditions in mapped pipelines - maps are parallel pipes without branches. From what we’ve seen from other use cases is encapsulating the conditional logic inside the mapped task itself, though there may be other options depending on your specific implementation.
d
@Kyle Moon-Wright, thanks A LOT! I wasn't getting too much clarity from the docs and I had begun to implement within my task, but wanted to make sure that if possible to use any of the control flow tasks, then that I did. Thank you for looking into this.
k
Absolutely, hopefully you’ll find usage of that good stuff elsewhere!
d
I love the new implementation of
case
already!
upvote 1
💼 1
😍 1
j
That’s all thanks to @Jim Crist-Harif - I love that new conditional API too
d
Ah man. Freaking awesome @Jim Crist-Harif! It's amazing! It's implementation as a context manager was spot on...