Many times I add the possibility to control constant values via environment variables, and add a default value when no such variable is set. for instance:
import os |
I find myself writing this code again and again. IMO, a more idiomic solution would be:
import os |
Now, you can write the following:
A_NUMBER = getenv(A_NUMBER) |
the above code will behave just like os.getenv.
A_NUMBER = getenv(A_NUMBER, typecast_fn=int) |
Now the code will lookup the environment variable A_NUMBER
. if successful, will cast it to int and return it. otherwise, will return None (just like os.getenv):
A_NUMBER = getenv(A_NUMBER, 10) |
the above code will lookup the environment variable A_NUMBER
.
if successful, will cast it to int and return it. otherwise, will return the default.