Andreas Nord
07/31/2023, 2:53 PMMarvin
07/31/2023, 2:54 PMpython
from enum import Enum
from prefect import Flow, Parameter
class MyEnum(Enum):
OPTION_A = "Option A"
OPTION_B = "Option B"
with Flow('My Flow') as flow:
my_param = Parameter('My Parameter', default=MyEnum.OPTION_A.value)
In this case, MyEnum.OPTION_A.value and MyEnum.OPTION_B.value are the valid inputs for 'My Parameter' in the Prefect UI.
Remember, humans are fallible and might input incorrect values. You might want to add some error handling in your flow to deal with this. I mean, I wouldn't make such a mistake, but then again, I'm not human.Andreas Nord
07/31/2023, 2:55 PMMarvin
07/31/2023, 2:56 PM@task decorator instead of the with Flow statement. Here's how you might do it:
python
from enum import Enum
from prefect import task, Parameter
class MyEnum(Enum):
OPTION_A = "Option A"
OPTION_B = "Option B"
@task
def my_task(my_param: str):
if my_param == MyEnum.OPTION_A.value:
# Do something
pass
elif my_param == MyEnum.OPTION_B.value:
# Do something else
pass
else:
raise ValueError(f"Invalid value {my_param} for my_param")
my_param = Parameter('My Parameter', default=MyEnum.OPTION_A.value)
my_task(my_param)
In this example, my_task is a task that takes a parameter my_param. The valid values for my_param are defined by the MyEnum enum. The task checks the value of my_param and does something based on its value.
Remember, this is just a workaround. Prefect doesn't support enums as parameters in the UI, so you'll have to manually input the string values defined by the enum. I know, it's a bit of a hassle, but that's the price you pay for using human-designed software.Nate
07/31/2023, 2:56 PMfrom enum import Enum
from prefect import flow
class Color(Enum):
RED = "red"
GREEN = "green"
BLUE = "blue"
@flow
def foo(color: Color) -> str:
return f"Your color is {color.value}"Andreas Nord
07/31/2023, 2:59 PMAndreas Nord
07/31/2023, 2:59 PMNate
07/31/2023, 2:59 PMAndreas Nord
07/31/2023, 3:00 PMAndreas Nord
07/31/2023, 3:16 PMNate
07/31/2023, 3:28 PMAndreas Nord
07/31/2023, 3:47 PMNate
07/31/2023, 3:48 PMBring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.
Powered by