Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Airflow: set a default value in code when Variable doesn't exist without an exception

I have a little problem, I want to do the typical conditional like

setting_x  = Variable.get('setting_x')
variable = setting_x if setting_x else 0

But since the Airflow model throws an exception when the key doesn't exist is impossible to do it without trycatching and that's not very cool.

Is there any solution that I'm missing to solve that case? I've searched in the whole internet of course, but without a solution yet.

Thanks, Angel

like image 928
skozz Avatar asked Jun 14 '18 17:06

skozz


1 Answers

You can set the default for the Variable if it doesn't exist when you're retrieving it with the get method.

variable = Variable.get('setting_x', default_var=0)

https://github.com/apache/airflow/blob/master/airflow/models/variable.py#L127

like image 138
Ben Gregory Avatar answered Oct 17 '22 21:10

Ben Gregory