Skip to main content

VariConfig

VariConfig is a flexible and powerful Python package for managing configuration files. It supports both YAML and JSON formats and allows for the use of variables within the configuration files, using template placeholders like {{ var }}.

Features

  • Read Configurations: Load configurations from YAML or JSON files.
  • Template Resolution: Supports variables inside configuration files using template placeholders (e.g., {{ var }}).
  • Nested Configs: Handles nested dictionaries as attribute-based objects for easy access.
  • Dynamic Updates: Update configurations dynamically in the code.
  • Conversion: Convert configurations back to standard Python dictionaries.

Installation

To install VariConfig, use pip:

pip install variconfig

Usage

1. Loading a Configuration

You can load configuration files in YAML or JSON format:

from variconfig import ConfigDict

# Load a YAML file
config = ConfigDict.from_yaml('config.yml')

# Load a JSON file
config = ConfigDict.from_json('config.json')

2. Accessing Configuration Values

Once the configuration is loaded, values can be accessed as object attributes:

# Access configuration values
print(config.data_dir)  # Output: path from config.yml
print(config.logging_config.loggers.matgraphdb.level)  # Output: INFO

3. Template Resolution

Variables can be defined using {{ var }} placeholders in the configuration file. These will be resolved when the configuration is loaded:

root_dir: "."
data_dir: "{{ root_dir }}/data"
log_dir: "{{ root_dir }}/logs"

In the example above, data_dir will be resolved to ./data, and log_dir will be resolved to ./logs.

4. Updating Configurations

You can modify configuration values at runtime:

# Update a configuration value
config.logging_config.loggers.matgraphdb.level = 'DEBUG'

# Print updated value
print(config.logging_config.loggers.matgraphdb.level)  # Output: DEBUG

5. Converting to Dictionary

If you need to convert the configuration back to a standard Python dictionary:

config_dict = config.to_dict()
print(config_dict)

Example Configuration (YAML)

root_dir: "."
data_dir: "{{ root_dir }}/data"
log_dir: "{{ root_dir }}/logs"
db_name: 'VariConfig'

logging_config:
  version: 1
  disable_existing_loggers: False
  formatters:
    simple:
      format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
      datefmt: '%Y-%m-%d %H:%M:%S'
  handlers:
    console:
      class: logging.StreamHandler
      formatter: simple
      stream: ext://sys.stdout
    file:
      class: logging.FileHandler
      formatter: simple
      filename: "{{ log_dir }}/variconfig.log"
      mode: a
  loggers:
    variconfig:
      level: INFO
      handlers: [console]
      propagate: no

License

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

Metadata

Release files for variconfig 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for variconfig 0.0.3
File Size Uploaded
variconfig-0.0.3.tar.gz 20.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for variconfig 0.0.3
File Interpreter ABI Platform
variconfig-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 31.9 kB

Release files / variconfig-0.0.3.tar.gz

Download URL variconfig-0.0.3.tar.gz
Size 20.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8da228d37e76bdcb8bbfe5acecedab078f456005596616349bbe5d6fcdad5789
BLAKE2b-256 checksum
How to use checksums
9840123fba47a3d1b4253c42cabfd298113e924a7b2864d186eaae6f3b5d8046
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release files / variconfig-0.0.3-py3-none-any.whl

Download URL variconfig-0.0.3-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa2014b05f3623ff46eb13d9905b255ce546de1f0c64def43960136e2c52fb79
BLAKE2b-256 checksum
How to use checksums
46c34c781403262a8159dfd415919cd6609713f6db7b55c8343b9afebd8a8555
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.9.20

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page