I received a call about stale parameters in a web app. I saw this post...
wtforms+flask today's date as a default value
... which was spot on.
The field's default was getting set on web server start. It was easy to test. A couple of print statements in the file and it was proven.
So now the senior software engineer's question is "how do I pass parameters in?".
So then ...
myfunc(offset=3):
.... some code
fromdate = fields.DateField('From Date', default=myfunc, validators=[validators.required()])
But the senior software engineer would like to be able to inline arguments to the function at the time it is assigned like ...
fromdate = fields.DateField('From Date', default=myfunc(-10), validators=[validators.required()])
... but without invoking it immediately. Basically, storing the function object and it's arguments without executing them, still deferring so that the code runs on page load instead of at server start.
Currently, we have two ideas.
- The senior software engineer is looking at wrapping a method around the method. So then there is one core method and then a bunch of other ones that are scenario specific, such as myfuncminus10
- I'm thinking lambda expressions. Something like myfunc = lambda :datetime.now().date() - timedelta(days=365) It would definitely produce the intended outcome, but it could result in a lot of copying and pasting the same code.
What's the best way to approach this? I'm figuring that there's a pretty good way that isn't either of the two ways I just described and we just don't know it yet.