Skip to main content

Convert YAML configuration files to Python objects

Project description

pyya - Simple tool that converts YAML configuration files to Python objects

PyPI - Downloads ClickPy Dashboard GitHub Downloads (all assets, all releases) PyPI - Python Version PyPI - Link PyPI - Version PyPI - Wheel License: MIT

Features

  • Very lightweight and simple API (currently it contains only one function)
  • Easy to use
  • Based on popular and well-tested libraries (like pydantic, camel-converter, PyYAML and munch)
  • Automatically merge default and production configuration files
  • Convert keys in configuration files to snake_case
  • YAML validation with Pydantic models

Installation

pip install pyya

Or download a specific version from Releases page and install it with:

pip install /path/to/pyya-[version]-py3-none-any.whl

Usage

Example

Create YAML configuration files for your project:

# default.config.yaml - this file usually goes to version control system
database:
  host: localhost
  port: 5432
  username: postgres
  password: postgres

redis:
  host: localhost
  port: 6379
# config.yaml - this file for production usage
database:
  username: username
  password: password

Import configuration files in your Python code with pyya:

import json

from pyya import init_config, logger

logger.setLevel(logging.INFO)

config = init_config(
    'config.yaml', 'default.config.yaml',
    merge_configs = True,
    sections_ignored_on_merge = ['redis'], # do not include redis in your config
    convert_keys_to_snake_case = False,
    add_underscore_prefix_to_keywords = False,
    raise_error_non_identifiers = False,
    validate_data_types = True,
    allow_extra_sections = True,
    warn_extra_sections = True,
    )
print(json.dumps(config))

# Output:
# {database: {"host": "localhost", "port": 5432, "username": "username", "password": "password"}}

As you can see, pyya automatically merges default config file with production config file.

Under the hood pyya uses PyYAML to parse YAML files and munch library to create attribute-stylish dictionaries.

Flags

# merge default and production configuration files
# setting to `False` disables other flags and makes default config optional
# `False` means "open config file and apply `yaml.safe_load` and `munchify` with no formatting"
merge_configs=True
# list of sections to ignore when merging configs
# it is useful when you have examples in your default config but do not want to have in the main one
sections_ignored_on_merge=None
# convert `camelCase` or `PascalCase` keys to `snake_case`
convert_keys_to_snake_case=False
# add underscore prefix to keys that are Python keywords
add_underscore_prefix_to_keywords=False
# raise error if key name is not valid Python identifier
raise_error_non_identifiers=False
# raise error if data types in config are not the same as default (makes sense only if merge is enabled)
# validation based on data types inferred from default config
validate_data_types=True
# raise error if there are extra sections in config (may break if section name formatting is enabled)
allow_extra_sections=True
# warn about extra keys and values on the first level
warn_extra_sections=True

Contributing

Are you a developer?

  • Fork the repository https://github.com/shadowy-pycoder/pyya/fork
  • Clone the repository: git clone https://github.com/<your-username>/pyya.git && cd pyya
  • Create your feature branch: git switch -c my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request

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

pyya-0.1.7.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

pyya-0.1.7-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file pyya-0.1.7.tar.gz.

File metadata

  • Download URL: pyya-0.1.7.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for pyya-0.1.7.tar.gz
Algorithm Hash digest
SHA256 1447f8aa2336fd0cc0ebd94b21f456512cfd8cccf5cb98b1bd134b60886dca6c
MD5 51bcb80ee803c07db56ac515666caac0
BLAKE2b-256 473617e170c27702d5d9ef8c444b12984d77a15454ceb6215a8267c1f0122f74

See more details on using hashes here.

File details

Details for the file pyya-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pyya-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for pyya-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 69bc44aee06b89ab76001186040026d070e83feec2e23e19ebbe9dec423fe5ad
MD5 5e2871feda92ab436d84ba491f25abb6
BLAKE2b-256 d7ad9770c4bc24f2dcb55afa4781059bce7af27b4f1518ad593399d196f97b36

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