Making the configuration aware of the None values

This three-stage process to set the environment variables includes many common sources of parameters and configuration settings. We don't always need environment variables, configuration files, and command-line options; some applications may only need a subset of these techniques.

We often need type conversions that will preserve None values. Keeping the None values will ensure that we can tell when an environment variable was not set. Here's a more sophisticated type conversion that can be called None-aware:

from typing import Optional

def
nint(x: Optional[str]) -> Optional[int]:
if x is None:
return x
return int(x)

We use this when converting environment variable values to integers. If an environment variable is not set, a default of None will be used. If the environment variable is set, then the value will be converted to an integer. In later processing steps, we can depend on the None value to build a dictionary from only the proper values that are not None.

We can use similar None-aware conversions to handle float values. We don't need to do any conversion for strings, and os.environ.get("SIM_NAME") will provide the environment variable value or None.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset