Skip to main content

A Python package for loading, validating, configurations from various sources.

Project description

pyconfig-loader

pyconfig-loader is a simple Python library for loading configurations from different sources such as environment variables and configuration files (JSON/YAML). It supports flexible configurations with features like default values, required keys, and filtering based on key prefixes.

Installation

To install the package, you can use pip:

pip install pyconfig-loader

Alternatively, you can install it from the source using poetry:

poetry install

Usage

1. Loading Configuration from Environment Variables

You can load configuration directly from environment variables, optionally filtering by key prefix, providing default values, or marking certain variables as required.

from pyconfig_loader import ConfigLoader

# Example of loading configuration from environment variables
loader = ConfigLoader("env")

# Load with required keys and default values
config = loader.load_from_env(
    required_keys=["DB_USERNAME", "DB_PASSWORD"],
    default_values={"DB_PASSWORD": "default_pass"},
    key_prefix="DB_"
)

print(config)

This would output a dictionary like:

{
    'DB_USERNAME': 'admin',   # from env
    'DB_PASSWORD': 'default_pass'  # default value as it's missing in the environment
}

2. Loading Configuration from a File (JSON or YAML)

You can also load configuration from a file, supporting both JSON and YAML formats.

JSON Example:

{
    "DB_USERNAME": "admin",
    "DB_PASSWORD": "secret"
}

YAML Example:

DB_USERNAME: admin
DB_PASSWORD: secret
from pyconfig_loader import ConfigLoader

# Example of loading configuration from a JSON or YAML file
loader = ConfigLoader("path/to/config.json")  # Or "path/to/config.yaml"
config = loader.load()

print(config)

3. Error Handling

By default, if required environment variables are missing, a ValueError will be raised. You can modify this behavior with the raise_on_missing argument.

config = loader.load_from_env(
    required_keys=["DB_USERNAME", "DB_PASSWORD"],
    raise_on_missing=False  # Will not raise an error if keys are missing
)

4. Advanced Configuration Features

  • Prefix Filtering: You can filter environment variables based on a prefix (e.g., load only variables starting with DB_).
  • Default Values: Specify default values for missing environment variables.
  • Required Keys: Define which environment variables are required to exist in the configuration, and optionally set default values for them.
config = loader.load_from_env(
    required_keys=["API_KEY", "DB_PASSWORD"],
    default_values={"DB_PASSWORD": "default_pass"},
    key_prefix="APP_"
)

Development

To contribute to pyconfig-loader, you can clone the repository and use poetry to manage dependencies and run tests.

git clone https://github.com/yourusername/pyconfig-loader.git
cd pyconfig-loader
poetry install
poetry run pytest  # To run tests

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

pyconf_ezload-0.1.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

pyconf_ezload-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file pyconf_ezload-0.1.1.tar.gz.

File metadata

  • Download URL: pyconf_ezload-0.1.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyconf_ezload-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5c89c0fb514309bab7d5d242e94f16cf3baca9aca78413b32627e8d129ddb2a8
MD5 a5d30d08c1f78194d4df51a5bbb61be8
BLAKE2b-256 935e6c4df730f09910db4f5417fb37e57acaa9a7d6085a895d9fa7352205f694

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconf_ezload-0.1.1.tar.gz:

Publisher: python-publish.yml on KartikeyaMalimath/pyconf-ezload

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyconf_ezload-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyconf_ezload-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyconf_ezload-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 364c57afcf449240ca77adc58e1672f7279a5fb063a23f8ae34a04cebf500c3b
MD5 c919111c38f7d818d0a187d5d94b5894
BLAKE2b-256 8f0de6ff8cb62ff3ba2273baadb8c04b53a2b79fcf9547fbf8524075576b22a2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyconf_ezload-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on KartikeyaMalimath/pyconf-ezload

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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