from prefect.tasks.control_flow import switch, merge
# note: this will raise some warnings, but they're ok for this use case!
with Flow('Arithmetic') as flow:
x, y = Parameter('x'), Parameter('y')
operations = {
'+': x + y,
'-': x - y,
'*': x * y,
'/': x / y
}
switch(condition=Parameter('op'), cases=operations)
result = merge(*operations.values())
@task
def parse_input(expression):
x, op, y = expression.split(' ')
return dict(x=float(x), op=op, y=float(y))
with Flow('Arithmetic') as flow:
inputs = parse_input(Parameter('expression'))
# once we have our inputs, everything else is the same:
x, y = inputs['x'], inputs['y']
operations = {
'+': x + y,
'-': x - y,
'*': x * y,
'/': x / y
}
switch(condition=inputs['op'], cases=operations)
result = merge(*operations.values())