- Very
lightweight
andsimple
API (currently it contains only one function) Easy
to use- Based on popular and well-tested libraries (like
camel-converter
,PyYAML
andmunch
) - Automatically
merge
default and production configuration files - Convert keys in configuration files to
snake_case
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
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
# 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
config = init_config(
'config.yaml', 'default.config.yaml',
merge_configs = True,
convert_keys_to_snake_case = False,
add_underscore_prefix_to_keywords = False
raise_error_non_identifiers = False)
print(json.dumps(config.database))
# Output:
# {"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.
# merge default and production configuration files
# setting to `False` disables other flags and makes default config optional
# `False` means "open config file and apply `ymal.safe_load` and `munchify` with no formatting"
merge_configs=True
# convert `camelCase` or `PascalCase` keys to `snake_case`
convert_keys_to_snake_case=True
# add underscore prefix to keys that are Python keywords
add_underscore_prefix_to_keywords=True
# raise error if key name is not valid Python identifier
raise_error_non_identifiers=True
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