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.1.tar.gz (31.2 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.1-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pz_uniconfig-1.0.1.tar.gz
  • Upload date:
  • Size: 31.2 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.1.tar.gz
Algorithm Hash digest
SHA256 25f4ea1acbdc8d5476b2b7ffcb8dca4c6ea00e1109ae8cfbf563ba1ffeb90f82
MD5 f7bea2f9889920883c2c3bfe0c893eda
BLAKE2b-256 b5415ddcc3165d61e3eb58c5b9e054d41fb7218c5cadba9a7ba4ebb604d64178

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pz_uniconfig-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4cbc920034b7f0a83569cb9679bb03e01b098fa76af27fbd0c0f2c528e426512
MD5 bbce60dee9b11b1e9d8888cc90a57260
BLAKE2b-256 66285e03a7f1c876cd2da036d85b8f2e029e3ee6550789418cea6a7960a8100b

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