from prefect.tasks.control_flow import switch, merge# note: this will raise some warnings, but they're ok for this use case!withFlow('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())
@taskdefparse_input(expression): x, op, y = expression.split(' ')returndict(x=float(x), op=op, y=float(y))withFlow('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())