Wrapper for python dataclasses to read from the environment
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.
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.
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
The metaclass will prioritize upper-case versions of fields before lower-case, that is to say
DB_URL would be
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
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size pyenvclasses-0.1.0-py3-none-any.whl (3.3 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size pyenvclasses-0.1.0.tar.gz (3.4 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for pyenvclasses-0.1.0-py3-none-any.whl