Skip to main content

Creates a class used to query environmental variables with typehinting a conversion to basic Python types.

Project description

EnvProxy

EnvProxy is a Python package that provides a convenient proxy for accessing environment variables with type hints, type conversion, and customizable options for key formatting. It alos includes EnvConfig, which lets you define configuration classes that map directly to environment variables. With EnvConfig, you can declaratively describe your environment-based configuration, including defaults, type hints, and optional sample .env file generation.

Installation

To install EnvProxy, use standard package management tools for Python:

# Using pip
pip install env-proxy

# Using poetry
poetry add env-proxy

Usage

Basic Usage with EnvProxy

Start by creating an EnvProxy instance with optional configuration for environment variable key transformations:

from env_proxy import EnvProxy

proxy = EnvProxy(prefix="MYAPP")

The prefix option adds a prefix to all keys, allowing you to group related variables under a common namespace. For example, with prefix="MYAPP", proxy.get_any("var") will look for the environment variable MYAPP_VAR. See the Configuration Options for EnvProxy section for more options.

Retrieving Environment Variables

Each method returns the value of an environment variable, converting it to the specified type. If the variable is missing, it either raises an error or returns the provided default.

Methods

get_any

Retrieve the raw value of a variable as Any. If the key does not exist, ValueError is raised unless a default is provided.

# export MYAPP_VAR="value"

value = proxy.get_any("var")  # returns "value"

get_bool

Retrieve a boolean variable. The following values are considered truthy (case-insensitive):

yes, true, 1, on, enable, enabled, allow

Similarly, common falsy values are handled:

no, false, 0, off, disable, disabled, disallow, deny

# export MYAPP_ENABLED="true"

value = proxy.get_bool("enabled")  # returns True

get_str

Retrieve a string variable.

# export MYAPP_NAME="example"

name = proxy.get_str("name")  # returns "example"

get_int

Retrieve an integer variable.

# export MYAPP_COUNT="42"

count = proxy.get_int("count")  # returns 42

get_float

Retrieve a floating-point variable.

# export MYAPP_RATIO="3.14"

ratio = proxy.get_float("ratio")  # returns 3.14

get_list

Retrieve a list of strings by splitting the variable’s value based on a separator (default is ,).

# export MYAPP_ITEMS="a,b,c ,d"

items = proxy.get_list("items")  # returns ["a", "b", "c", "d"]

get_json

Parse a JSON string from the environment.

# export MYAPP_CONFIG='{"key": "value"}'

config = proxy.get_json("config")  # returns {"key": "value"}

EnvConfig – Declarative Configuration with Fields

The new EnvConfig class allows you to define environment-based configuration with type hints, descriptions, and defaults. It automatically connects fields to environment variables using a declarative approach, and can even generate a sample .env file for easy setup.

Defining Configuration Classes with EnvConfig

Define your configuration by subclassing EnvConfig and using Field factory to describe each variable. The Field function supports attributes like description, default, and type_hint (see Field Options).

from env_proxy import EnvConfig, Field, EnvProxy

class MyConfig(EnvConfig):
    env_proxy = EnvProxy(prefix="MYAPP")  # common prefix for all fields
    debug: bool = Field(description="Enable debug mode", default=False)
    database_url: str = Field(description="Database connection URL")
    max_connections: int = Field(description="Maximum DB connections", default=10)
    cache_backends: list[str] = Field(description="Cache backends", type_hint="list")

Accessing Config Values

Once defined, MyConfig provides easy access to each environment variable with the specified type conversions.

config = MyConfig()

# Access configuration values

debug = config.debug  # Looks for MYAPP_DEBUG in the environment
database_url = config.database_url  # Raises ValueError if not found

Generating a Sample .env File

You can export a sample .env file from your EnvConfig class, which documents all fields with their descriptions, types, and default values.

MyConfig.export_env("sample.env", include_defaults=True)

This would produce a file like:

# debug (bool) [optional]
# Enable debug mode
MYAPP_DEBUG=False

# database_url (str) [required]
# Database connection URL
MYAPP_DATABASE_URL=

# max_connections (int) [optional]
# Maximum DB connections
MYAPP_MAX_CONNECTIONS=10

# cache_backends (list) [required]
# Cache backends
MYAPP_CACHE_BACKENDS=

Field Options

Each Field can be customized with the following options:

  • alias: Custom name in the environment. Defaults to the field name.
  • description: Description of the variable.
  • default: Default value if the variable is missing. If UNSET, the variable is required.
  • type_hint: Specify the type explicitly (e.g., json for JSON objects).
  • env_prefix: Override the env_prefix set on the EnvConfig class for a specific field.
  • allow_set: Allow modification of the environment variable value at runtime.

Field Type Hints

The following type_hint values are supported:

  • any
  • bool
  • float
  • int
  • str
  • list
  • json

Example of using type_hint:

class AdvancedConfig(EnvConfig):
    settings: dict[str, Any] = Field(type_hint="json", description="Complex JSON settings")

Example Usage with EnvConfig

import os
from env_proxy import EnvConfig, Field

# Set environment variables
os.environ["MYAPP_DEBUG"] = "true"
os.environ["MYAPP_DATABASE_URL"] = "sqlite:///data.db"
os.environ["MYAPP_CACHE_BACKENDS"] = "redis,memcached"

class MyConfig(EnvConfig):
    env_prefix: str = "MYAPP"
    debug: bool = Field(description="Enable debug mode", default=False)
    database_url: str = Field(description="Database connection URL")
    cache_backends: list[str] = Field(description="Cache backends", type_hint="list")

config = MyConfig()

# Access configuration values

print(config.debug)  # True
print(config.database_url)  # "sqlite:///data.db"
print(config.cache_backends)  # ["redis", "memcached"]

# Export a sample .env file

MyConfig.export_env("sample.env", include_defaults=True)

Configuration Options for EnvProxy

You can control how keys are transformed when retrieving variables in EnvProxy:

  • prefix: Adds a prefix to all keys.
  • uppercase: Converts keys to uppercase.
  • underscored: Replaces hyphens with underscores.
proxy = EnvProxy(prefix="myapp", uppercase=True, underscored=False)
proxy.get_any("var")  # Looks for "MYAPP_VAR"

Error Handling

If a variable is not found, and no default value is provided, a ValueError will be raised. Each method also raises a ValueError for invalid conversions.

try:
    missing_value = proxy.get_int("missing_key")
except ValueError as e:
    print(e)  # Output: No value found for key 'missing_key' in the environment.

License

EnvProxy is open-source and distributed under the MIT License. See LICENSE.md for more information.

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

env_proxy-1.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

env_proxy-1.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file env_proxy-1.1.0.tar.gz.

File metadata

  • Download URL: env_proxy-1.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for env_proxy-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e7d1fd68713afbbdb43a6a5d4bbba219503850fb520021a4f28ce9c625895ad3
MD5 9bf160966afa2eb28674f0b24b035c3e
BLAKE2b-256 a9eb99fe64c5f8a8d9bea723b934a1d85bf2f6fe83705d2cf78f3699064059fd

See more details on using hashes here.

File details

Details for the file env_proxy-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: env_proxy-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for env_proxy-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 365919b36ba25e8e623d65f4f31f456186fca98e9d1e03a9e12bed947e86ead1
MD5 a555e12d30e8f3bee4ab52f0f2875dda
BLAKE2b-256 0a71d9f7550d9423ec44da2ddb3612b274786adf98bc8e877d296ab2ad21b174

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