added support for module level__dir__
- __dir__is called when the built-in dir function is called on the module
- __getattr__is called when an attribute is not found via the regular attribute lookup
Let's use this to build an environment based configuration module.
- Conviruation values has a value, environment key and a function to convert from str to right type
- I'm going to use dataclasses and populate values from environment in __post_init__
- Complex data types (such as list) should be JSON encoded in the environment variables
- All configuration values with start with thec_prefix
- __dir__will return a list configuration variables without thec_prefix
- __getattr__will add thec_prefix and will look for the varialbes in globals
We're adding c_ prefix and removing it to bypass the regular attribute lookup mechanism. If we'll call a variablehttp_portand user will writeconfig.http_port, our__dir__function won't be called.
Here's the code