Skip to main content

Map a python configuration from environment variables

Project description

Ecological combines PEP526 and environment variables to make the configuration of 12 factor apps easy.

Getting Started

Ecological automatically gets and converts environment variables according to the configuration class definition.

For example, imagine your application has a configurable (integer) Port and (boolean) Debug flag and a (string) Log Level, that is INFO by default, you could simply declare your configuration as:

class Configuration(ecological.AutoConfig):
    port: int
    debug: bool
    log_level: str = "INFO"

And then set the environment variables PORT, DEBUG and LOG_LEVEL. Ecological will automatically set the class properties from the environment variables with the same (but upper cased) name.

The values are set at the class definition type and assigned to the class itself (i.e. the class doesn’t need to be instantiated).

Typing Support

Ecological also supports some of the types defined in PEP484, for example:

class Configuration(ecological.AutoConfig):
    list_of_values: List[str]

Will automatically parse the environment variable value as a list.

Prefixed Configuration

You can also decide to prefix your application configuration, for example, to avoid collisions:

class Configuration(ecological.AutoConfig, prefix='myapp'):
    home: str

In this case the home property will be fetched from the MYAPP_HOME environment property.

Caveats and Known Limitations

  • Ecological doesn’t support (public) methods in AutoConfig classes

Advanced Usage

Fine-grained control

You can control how the configuration properties are set by providing a ecological.Variable instance as the default value.

ecological.Variable receives the following parameters:

  • variable_name (mandatory) - exact name of the environment variable that will be used.

  • default (optional) - default value for the property if it isn’t set.

  • transform (optional) - function that converts the string in the environment to the value and type you expect in your application. The default transform function will try to cast the string to the annotation type of the property.

Transformation function

The transformation function receive two parameters, a string representation with the raw value, and a wanted_type with the value of the annotation (usually, but not mandatorily a type).

Nested Configuration

Ecological.AutoConfig also supports nested configurations, for example:

class Configuration(ecological.AutoConfig):
    integer: int

    class Nested(ecological.AutoConfig, prefix='nested'):
        boolean: bool

This way you can group related configuration properties hierarchically.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

ecological-1.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file ecological-1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for ecological-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c599cf480b415763f75e2576dc2dc0c0ffc0203d69bf88258ebefaae04361a4c
MD5 98a503ecb7c415ca8cf86aafd790b5d7
BLAKE2b-256 015de4fcbab30eebecf3d70f1ef8b3f707e161dbceb8a87b866d228e53702db7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page