Is there a nature way to work with Parameters? e.g...
# ask-community
e
Is there a nature way to work with Parameters? e.g the example below will throw an exception because for_date has type Parameter
Copy code
with Flow("Demo") as flow:
    for_date = Parameter('for_date', required=True, default='20200101')
    for_date_str = dt.datetime.strptime(for_date, "%Y%m%d")
I think what I’d like to ask is how to extract the value of a parameter in order to use it somewhere
k
Hi @Enda Peng! I think the issue here is that
for_date
is a deferred execution that is population at runtime while
for_date_str
happens at run time. You would need to create a helper task to perform that operation and then return it so that the execution gets deferred as well
Copy code
@task
def string_to_time(for_date):
    return dt.datetime.strptime(for_date, "%Y%m%d")

with Flow("Demo") as flow:
    for_date = Parameter('for_date', required=True, default='20200101')
    for_date_str = string_to_time(for_date)
e
ha, that’s what I am doing, sound to be the
nature
way
k
That is indeed the
nature
way
e
thank you!
👍 1
j
@Kevin Kho Most times, in operation I'm querying for a date range of 24-48 hours. However, when I'm launching a new flow, I tend to back date or play around with the date ranges to make sure the flow is robust. Is there a way to use Parameters to manually set the parameters while the default is dynamically set?
When I tried to set
Parameter('end_date', default=date().today().strptime(...))
the default date is static to when I deployed the flow.
k
Hi @Jimmy Le, maybe you can try using the context so it gets populated at runtime? prefect.context.today, and then maybe pass this to a task to format it the way you need?
Otherwise you may need to do this:
Copy code
import pendulum
date = Parameter(name="date", default="yesterday")
@task
def generate_date(date_parameter):
    if date_parameter == 'yesterday'
        return pendulum.now().subtract(days=1)
    return pendulum.parse(date_parameter)
j
Oooh nice, I like that. Never played around with any of these utilities actually. Going to dive in thanks!