Skip to main content

Wrapper for python dataclasses to read from the environment

Project description

MIT License PyPI version Tests

Envclasses

Envclasses are a thin wrapper around dataclasses which allows for the values to be defined via environment variables rather than explicitly in code. Values are typed and are able to be defaulted.

Motivation

I got tired of writing code that was configured through environment variables, referencing the environment variable when I needed to instantiate something. This made it difficult to keep up with how I could configure that software that I was writing as I would have to comb through the code and make sure that the documentation was up to date.

Envclasses are an attempt to reduce the sprawl of configuration through environment variables and centralize configuration into a single, document-able class. They are both inspired by, and built on top of dataclasses, which is why their structure is so similar.

Usage

Defining an environment class is simple:

from envclasses import EnvClassMeta

class ApplicationConfig(metaclass=EnvClassMeta):

    db_url: str
    db_username: str
    db_password: str
    port: int = 5050
    mode: str = 'development'


config = ApplicationConfig()

The provided metaclass will turn the ApplicationConfig into a dataclass with fields defined from os.environ. The metaclass will prioritize upper-case versions of fields before lower-case, that is to say DB_URL would be prioritized over db_url. Mixed-case variants are not considered.

If values are not defined, the metaclass will wait until all fields have been tested to report which are missing. In the event that we should ignore missing fields, the environment variable env_ignore_missing should be defined as true or yes.

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

pyenvclasses-0.1.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyenvclasses-0.1.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file pyenvclasses-0.1.0.tar.gz.

File metadata

  • Download URL: pyenvclasses-0.1.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Darwin/19.3.0

File hashes

Hashes for pyenvclasses-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e86bac268126768ecbb9a47b11c53bd0e365376641f5de5d36baf13d10cd11ad
MD5 075578a80919ea227d1d8292ec561f58
BLAKE2b-256 41e1394d9c24faf7a37fe0f31d408a71431f9f4c3b20f2d481875543fce906db

See more details on using hashes here.

File details

Details for the file pyenvclasses-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyenvclasses-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Darwin/19.3.0

File hashes

Hashes for pyenvclasses-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 258e35c7dd62475131fc9c5eb10b8abf43af95571935bdc94a1b1f97f70950f9
MD5 5361537aa75e9e4829fdf1dbbbd29834
BLAKE2b-256 f3b03b85560819b4f11d601d863c7b1a5382ce72ac3c7e4ce07d39f0cc20d2ed

See more details on using hashes here.

Supported by

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