Skip to main content

Python application configuration via the environment

Project description

Envier

Python application configuration from the environment

Synopsis

Envier is a Python library for extracting configuration from environment variables in a declarative and (eventually) 12-factor-app-compliant way.

Usage

The following example shows how to declare the configuration for an application that uses the MYAPP_DEBUG, MYAPP_SERVICE_HOST and MYAPP_SERVICE_PORT variables from the environment.

>>> from envier import Env
>>> 
>>> class GlobalConfig(Env):
>>>     __prefix__ = "myapp"
>>>     
>>>     debug_mode = Env.var(bool, "debug", default=False)
>>> 
>>>     service_host = Env.var(str, "service.host", default="localhost")
>>>     service_port = Env.var(int, "service.port", default=3000)
>>> 
>>>     _is_default_port = Env.der(bool, lambda c: c.service_port == c.spec.service_port.default)
>>> 
>>> config = GlobalConfig()
>>> config.service_port
3000
>>> config._is_default_port
True

Configurations can also be nested to create namespaces:

>>> from envier import Env
>>> 
>>> class ServiceConfig(Env):
>>>     __prefix__ = "service"
>>> 
>>>     host = Env.var(str, "host", default="localhost")
>>>     port = Env.var(int, "port", default=3000)
>>> 
>>> class GlobalConfig(Env):
>>>     __prefix__ = "myapp"
>>>     
>>>     debug_mode = Env.var(bool, "debug", default=False)
>>> 
>>>     service = ServiceConfig
>>> 
>>> config = GlobalConfig()
>>> config.service.port
3000

The same configuration can be obtained with implicit nesting by declaring the ServiceConfig subclass inside GlobalConfig, and setting the class attribute __item__ to the name of the item the sub-configuration should be assigned to, viz.

>>> from envier import Env
>>> 
>>> class GlobalConfig(Env):
>>>     __prefix__ = "myapp"
>>>     
>>>     debug_mode = Env.var(bool, "debug", default=False)
>>> 
>>>     class ServiceConfig(Env):
>>>         __item__ = __prefix__ = "service"
>>>         
>>>         host = Env.var(str, "host", default="localhost")
>>>         port = Env.var(int, "port", default=3000)
>>> 
>>> config = GlobalConfig()
>>> config.service.port
3000

Type Checking

The library ships with a mypy plugin to allow for type checking. If you want to use it, either install the library with the mypy extra or ensure that mypy is installed, and then add envier.mypy to the list of extra plugins in the mypy configuration.

Roadmap

  • Add support for environment files.
  • Rely on type hints as support for older versions of Python is dropped.
  • Derivations might require an evaluation order.

Project details


Download files

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

Source Distribution

envier-0.5.2.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

envier-0.5.2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file envier-0.5.2.tar.gz.

File metadata

  • Download URL: envier-0.5.2.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for envier-0.5.2.tar.gz
Algorithm Hash digest
SHA256 4e7e398cb09a8dd360508ef7e12511a152355426d2544b8487a34dad27cc20ad
MD5 3bcc6e91f97d2af0f2408a5ae19d291b
BLAKE2b-256 1a3e49a0e2111173a259c96027e17a50361c474336aba04de6f834d816d18ec3

See more details on using hashes here.

Provenance

File details

Details for the file envier-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: envier-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for envier-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 65099cf3aa9b3b3b4b92db2f7d29e2910672e085b76f7e587d2167561a834add
MD5 75ef1f34f5c00f8ed7f2ee4931e74369
BLAKE2b-256 397499f5d33b4ec2eed9d19d975ab61e3eeddc71c8780a3e46ec9e0f17095451

See more details on using hashes here.

Provenance

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