Skip to main content

Classy Env

Tests License: MIT

About

Classy Env is a lightweight Python package for managing environment variables in an OOP way.

Requirements

This package requires Python 3.11 or higher.

Installation

Classy Env is available on PyPI and can be installed by running the following command:

pip install classy-env

Usage

Create a ClassyEnv subclass and declare its attributes using the EnvVar function:

from classyenv import ClassyEnv, EnvVar

class Settings(ClassyEnv):
    database_user = EnvVar("DB_USER")
    database_password = EnvVar("DB_PASSWORD")
    api_secret_key = EnvVar("SECRET_KEY")

The EnvVar function takes the name of the environment variable as an argument. The Value of that variable will be assigned to the corresponding attribute.

Then, create an object of your subclass and access the environment variables through the object's attributes:

settings = Settings()

database_connection = connect(
    user=settings.database_user,
    password=settings.database_password,
)

At the object's creation, Classy Env will check if the environment variables provided to the EnvVar functions are defined. If not, an exception will be raised.

Runtime validation

added in version 1.1.0

The validation mentioned above can also be triggered at class creation using the runtime_check class argument:

from classyenv import ClassyEnv, EnvVar

class Settings(ClassyEnv, runtime_check=True):
    database_user = EnvVar("DB_USER")
    database_password = EnvVar("DB_PASSWORD")
    api_secret_key = EnvVar("SECRET_KEY")

Converters

added in version 1.2.0

The EnvVar function accepts an optional converter argument. Converter must be a callable that accepts a string value.

If provided, converter will be called with the environment variable value when the attribute is being accessed, and the returned by converter value will be returned as attribute value:

from classyenv import ClassyEnv, EnvVar

class Settings(ClassyEnv):
    database_port: int = EnvVar("DB_PORT", converter=int)


settings = Settings()
assert isinstance(settings.database_port, int)

Defaults

added in version 1.3.0

The EnvVar function accepts an optional default argument, which allows for specifying a default value, when corresponding environment variable is not defined:

import os
from classyenv import ClassyEnv, EnvVar

if "DB_PORT" in os.environ:
    os.environ.pop("DB_PORT")

class Settings(ClassyEnv):
    database_port = EnvVar("DB_PORT", default=3306)


settings = Settings()
assert settings.database_port == 3306

Mutating the ClassyEnv instances and subclasses

At this moment, mutating instances of the ClassyEnv class is not supported:

settings.database_user = "Roe_Jogan123" # this will raise an exception

Similarly, mutating the class attributes of the ClassyEnv subclasses is not supported:

Settings.database_user = "Roe_Jogan123" # this will raise an exception

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for classy-env 1.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for classy-env 1.3.1
File Size Uploaded
classy_env-1.3.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for classy-env 1.3.1
File Interpreter ABI Platform
classy_env-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / classy_env-1.3.1.tar.gz

Download URL classy_env-1.3.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1d4afa8227c915b7acad2de0851c00d4cee466c72342927b67236bca5977cd29
BLAKE2b-256 checksum
How to use checksums
6ccd1aa0020ff8c85108c505bfb0522402e91d67c03398b1349465295a6d1121
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.2

Release files / classy_env-1.3.1-py3-none-any.whl

Download URL classy_env-1.3.1-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d517be7e6932f9f334879522a69f64329274783d193dab43947771658d5a66d
BLAKE2b-256 checksum
How to use checksums
d73f0d2acfd4a588b1ff8094fb50f0c0b5b459c62df119e3c5737134cf5c8686
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page