[docs]defenv_var_is_set(env_var:str)->bool:"""Check if an environment variable is set. Args: env_var (str): The name of the environment variable. Returns: bool: True if the environment variable is set, False otherwise. """returnenv_varinos.environandos.environ[env_var]notin("","0","false","False",)
[docs]defget_from_dict_or_env(data:dict[str,Any],key:Union[str,list[str]],env_key:str,default:Optional[str]=None,)->str:"""Get a value from a dictionary or an environment variable. Args: data: The dictionary to look up the key in. key: The key to look up in the dictionary. This can be a list of keys to try in order. env_key: The environment variable to look up if the key is not in the dictionary. default: The default value to return if the key is not in the dictionary or the environment. Defaults to None. """ifisinstance(key,(list,tuple)):forkinkey:ifkindataanddata[k]:returndata[k]ifisinstance(key,str)andkeyindataanddata[key]:returndata[key]key_for_err=key[0]ifisinstance(key,(list,tuple))elsekeyreturnget_from_env(key_for_err,env_key,default=default)
[docs]defget_from_env(key:str,env_key:str,default:Optional[str]=None)->str:"""Get a value from a dictionary or an environment variable. Args: key: The key to look up in the dictionary. env_key: The environment variable to look up if the key is not in the dictionary. default: The default value to return if the key is not in the dictionary or the environment. Defaults to None. Returns: str: The value of the key. Raises: ValueError: If the key is not in the dictionary and no default value is provided or if the environment variable is not set. """ifenv_keyinos.environandos.environ[env_key]:returnos.environ[env_key]elifdefaultisnotNone:returndefaultelse:msg=(f"Did not find {key}, please add an environment variable"f" `{env_key}` which contains it, or pass"f" `{key}` as a named parameter.")raiseValueError(msg)