Skip to main content

lzconfig — Lazy Configuration without instantiation

lz: Too lazy to type lazy

中文文档

Import and go. No init, no loader calls, no boilerplate.

import lzconfig as lz

# Attribute-style access
print(lz.database.host)                # → "localhost"
print(lz.database.credentials.user)    # → "admin"

# Dict-style access
print(lz['database']['host'])          # → "localhost"

# Mixed access
print(lz.database['port'])             # → 5432

# Safe access with defaults
print(lz.get('debug', False))          # → True

How It Works

At import time, lzconfig discovers your config file, parses it, interpolates environment variables, and replaces itself in sys.modules with the loaded configuration object. From then on, lz is your config.

Command-line --key value arguments are merged over the file, so the command line always wins. And if command-line arguments are present, no config file is needed at all.

Config File Resolution

  1. If LZCONFIG_FILE is set (and non-empty), that file is used. Relative paths are resolved against the current working directory.

  2. Otherwise, the current directory is scanned for the first match:

    Priority Filename
    1 lzconfig.yaml
    2 lzconfig.yml
    3 lzconfig.json
    4 lzconfig.toml
    5 lzconfig.ini
    6 lzconfig.cfg
    7 lzconfig.env
    8 lzconfig.xml
  3. If nothing is found and there are no --key value command-line arguments, ConfigNotFoundError is raised at import time.

Supported Formats

Format Extensions
YAML .yaml .yml
JSON .json
TOML .toml
INI .ini .cfg
.env .env
XML .xml

All formats work out of the box — no extras needed.

Environment Variable Interpolation

$VAR and ${VAR} references in config values are expanded at load time using os.path.expandvars:

# lzconfig.yaml
database:
  host: $DB_HOST
  password: ${DB_PASSWORD}

Undefined variables are left as literal text — no error is raised.

Command-Line Arguments

--key value and --key=value arguments are merged over the file-based configuration — the command line is the highest-priority source:

python app.py --database.host example.com --port 8080 --debug true
import lzconfig as lz

lz.database.host  # "example.com" — overrides the config file
lz.port           # 8080 — an int, not a string
lz.debug          # True — a bool

Rules:

  • Dotted keys build nested structure: --database.host x sets lz.database.host.
  • Values are parsed with JSON when possible (--port 5432 → int, --debug true → bool, --tags '["a","b"]' → list); otherwise they stay strings (--zip 01234"01234").
  • A bare --key (no value) is treated as True.
  • Only -- options are consumed; everything else is left in sys.argv untouched, and a bare -- ends option parsing.
  • If command-line arguments are present, no config file is necessary, the config will be built from the command line alone.
  • If neither a config file nor command-line arguments exist, ConfigNotFoundError is raised.

Access Patterns

Pattern Example
Attribute chain lz.a.b.c
Dict chain lz['a']['b']['c']
Mixed lz.a['b'].c
List index lz['items'][0].name
Membership 'key' in lz
Safe get lz.get('key', default)
Keys lz.keys()
Length len(lz)
Iteration for k in lz: ...
Attribute write lz.debug = True
Dict write lz['database']['port'] = 8080
Delete key del lz.debug / del lz['debug']

The config object is mutable — attribute and dict-style writes update the underlying data in place, and nested values share the same storage (lz.database.host = '...' is visible through lz['database']['host']).

Key / Method Collision

If a config key happens to have the same name as a ConfigObject method (e.g., get, items, keys):

  • Attribute access (lz.items) returns the method.
  • Dict access (lz['items']) always returns the config value.

This is a deliberate trade-off. When in doubt, use [] — it always works. The same rule applies to writes: lz.items = [...] still stores the config value, but reading lz.items returns the method.

Error Handling

Exception When
ConfigNotFoundError No config file found and no --key value arguments present
ConfigParseError File found but cannot be parsed
ConfigKeyError Accessing a non-existent key

All exceptions inherit from ConfigError and can be imported directly:

from lzconfig import ConfigError, ConfigNotFoundError, ConfigKeyError

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lzconfig-0.1.3.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

lzconfig-0.1.3-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file lzconfig-0.1.3.tar.gz.

File metadata

  • Download URL: lzconfig-0.1.3.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for lzconfig-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0e09e442721fe2c1500add0bbfc73db4bb4937c4b33750821295a741ba62569e
MD5 f73fa2d94d72fbab7039b8eb4ea63c92
BLAKE2b-256 1e9f447ed2dfdf98cff4552f6a63f1d94c31d6d16174475afed814966e011f11

See more details on using hashes here.

Provenance

The following attestation bundles were made for lzconfig-0.1.3.tar.gz:

Publisher: publish.yml on SihanLv/lzconfig

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

File details

Details for the file lzconfig-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: lzconfig-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for lzconfig-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c1dbf3a6a8ff135ca7de9655bf082b0968d00ee6642d8149c3ec39874ebe346b
MD5 1c6c85f1cd28e474ee426461ca778ac0
BLAKE2b-256 c3bcf3fb6fb18d7d59a290662bfa8dddc3e045f3015907639de7f4e7554fe829

See more details on using hashes here.

Provenance

The following attestation bundles were made for lzconfig-0.1.3-py3-none-any.whl:

Publisher: publish.yml on SihanLv/lzconfig

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