env_cast¶
-
privex.helpers.common.
env_cast
(env_key: str, cast: callable, env_default=None)[source]¶ Obtains an environment variable
env_key
, if it’s empty or not set,env_default
will be returned. Otherwise, it will be converted into a type of your choice using the callablecast
parameterExample:
>>> os.environ['HELLO'] = '1.234' >>> env_cast('HELLO', Decimal, Decimal('0')) Decimal('1.234')
- Parameters
cast (callable) – A function to cast the user’s env data such as
int
str
orDecimal
etc.env_key (str) – Environment var to attempt to load
env_default (any) – Fallback value if the env var is empty / not set (Default: None)