Skip to main content

Unified config management for Python: YAML, JSON, TOML, dotenv, INI—one API.

Project description

Uniconfig

A flexible Python library for loading, editing, and saving configuration files in multiple formats—including YAML, JSON, TOML, dotenv (.env), and INI. Uniconfig provides a unified interface for working with configuration files, making it easy to manage your application's settings regardless of the underlying file format.

Features

  • Multiple Format Support: Works with YAML, JSON, TOML, dotenv (.env), and INI files
  • Unified API: Read, write, and modify configuration with a single consistent API
  • Intuitive Access: Access nested configuration values using simple dot notation
  • Type Preservation: Maintains data types (strings, numbers, booleans, lists, dictionaries)
  • Hot Reload: Reload configuration changes at runtime
  • Format Conversion: Easily convert between different configuration formats
  • Environment Variable Override: Override configuration values with environment variables
  • Advanced Features: Compare configurations, convert to namespaces, flatten nested structures

Installation

pip install pz-uniconfig

Quick Start

from pz_uniconfig import Uniconfig

# Load a configuration file
config = Uniconfig("config.yaml")

# Get values (with optional default)
database_host = config.get("database.host", "localhost")
is_debug = config.get("app.debug", False)

# Check if a key exists
if config.has("logging.level"):
    print(f"Log level: {config.get('logging.level')}")

# Set values (creates nested structures as needed)
config.set("app.version", "1.2.3")
config.set("database.credentials.username", "admin")

# Save changes
config.save()

Detailed Usage

Loading Configuration Files

Uniconfig automatically detects the file format based on the file extension:

# Load different file formats
yaml_config = Uniconfig("config.yaml")
json_config = Uniconfig("config.json")
toml_config = Uniconfig("config.toml")
ini_config = Uniconfig("config.ini")
env_config = Uniconfig("config.env")

# Specify a different directory
config = Uniconfig(config_filename="config.yaml", config_dir="/path/to/config/dir")

Accessing Configuration Values

# Basic key access with dot notation
project_title = config.get("project.title")

# Accessing nested values
smtp_server = config.get("notifications.email.smtp_server")

# Accessing array elements
first_owner = config.get("project.owners[0].username")
first_role = config.get("project.owners[0].roles[0]")

# Using default values for non-existent keys
value = config.get("does.not.exist", "Default Value")

# Checking if keys exist
if config.has("project.title"):
    print("Project title is defined")

# Using try_get for safe access
found, value = config.try_get("project.title")
if found:
    print(f"Found value: {value}")

Modifying Configuration

# Setting values
config.set("maintenance_mode", True)

# Setting nested values
config.set("notifications.email.port", 587)

# Creating new nested values
config.set("new.nested.value", "This is a new nested value")

# Setting array values
config.set("metrics.providers[0]", "newrelic")

# Delete a key
config.delete("temporary.setting")

Saving Changes

# Save changes to the original file
config.save()

# Clone to a different file or format
json_config = config.clone("/path/to/new/config.json")

Advanced Features

Updating from Dictionary

update_dict = {
    "project": {
        "title": "Updated Project",
        "version": "2.0.0"
    },
    "api": {
        "url": "https://api.example.com/v2/"
    }
}
config.update_from_dict(update_dict)

Converting to Namespace

# Convert to namespace for attribute-style access
ns = config.as_namespace()
print(ns.project.title)
print(ns.api.url)

Flattening Nested Structure

# Convert nested structure to flat dictionary
flat_dict = config.as_flat_dict()
# Result: {"project.title": "My Project", "project.version": "1.0", ...}

Environment Variable Override

# Override config values with environment variables
# For example, PROJECT_TITLE env var will override project.title
config.override_with_env(prefix="APP_")

Comparing Configurations

# Compare two configurations
other_config = Uniconfig("other_config.yaml")
differences = config.diff(other_config)

Reloading Configuration

# Reload configuration from file
config.reload()

Method Reference

Method Description
__init__(config_filename, config_dir=None) Initialize with a configuration file
get(key, default=None) Get a value by key with optional default
set(key, value) Set a value by key (creates nested structure if needed)
has(key) Check if a key exists
try_get(key) Safely try to get a value, returns (found, value)
delete(key) Delete a key from the configuration
save() Save changes to the configuration file
reload() Reload the configuration from the file
clone(new_path) Clone the configuration to a new file
as_namespace() Convert configuration to a namespace for attribute access
diff(other) Compare with another configuration
update_from_dict(d) Update configuration from a dictionary
as_flat_dict(separator=".") Convert to a flat dictionary with keys separated by dots
override_with_env(prefix=None) Override configuration with environment variables
print() Print the configuration as formatted JSON

Supported File Formats

Format File Extensions Description
YAML .yaml, .yml Human-readable format with support for complex data structures
JSON .json JavaScript Object Notation, widely used for data interchange
TOML .toml Tom's Obvious, Minimal Language, designed to be easy to read
INI .ini Simple configuration format with sections and key-value pairs
dotenv .env Environment variable file format with KEY=VALUE pairs

Why Use Uniconfig?

Uniconfig simplifies configuration management in Python applications by providing:

  1. Format Flexibility: Switch between configuration formats without changing your code
  2. Simplified Access: No need to remember different APIs for different formats
  3. Nested Configuration: Easily work with complex, nested configuration structures
  4. Type Safety: Preserve data types across different formats
  5. Developer Experience: Intuitive API with helpful methods for common tasks
  6. Extensibility: Easy to add support for additional formats if needed

Whether you're building a small script or a large application, Uniconfig helps you manage your configuration in a clean, consistent way.

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

pz_uniconfig-1.0.0.tar.gz (31.0 kB view details)

Uploaded Source

Built Distribution

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

pz_uniconfig-1.0.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file pz_uniconfig-1.0.0.tar.gz.

File metadata

  • Download URL: pz_uniconfig-1.0.0.tar.gz
  • Upload date:
  • Size: 31.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pz_uniconfig-1.0.0.tar.gz
Algorithm Hash digest
SHA256 70fb82ebce18d942b8e50e66a3ccb919e05f4fdd1e1c612c7c4f6db24331f863
MD5 5bed0ae552581d9a9ba841edf39d0489
BLAKE2b-256 fb3aeb5b3506da0f82672e4794b86ff119e2224822dc7d019a7a6063546d7b69

See more details on using hashes here.

File details

Details for the file pz_uniconfig-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pz_uniconfig-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pz_uniconfig-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec4c8d40d2415c2933e33f53844e8e4afe8dd880288280e0d407fec4ab8bd3d0
MD5 0d04444126e827464c1054965c482f77
BLAKE2b-256 5c9b8f8e241af56e9bb23b978e6f0bed497cb0f8c4b7d7057be69b2f3d4ce236

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