A simple solution that allows dot notation for YAML file.
Project description
Status
yaml2object is a python library that allows dot notation access for YAML file.
Install
pip install yaml2object
Usage
1. Create your YAML settings
# config.yml
defaults: &defaults
database:
adapter: postgresql
database: development
port: 8000
nested_param:
param1:
sub_param1: 'sub_param1 value'
sub_param2: 'sub_param2 value'
development:
<<: *defaults
test:
<<: *defaults
port: 8001
2. Define your class
- Set yaml2object.YAMLObject as meta-class of your config class.
- Provide source, namespace as class fields.
- scource: YAML file path
- namespace: param key in YAML file
from yaml2object import YAMLObject
class Config(metaclass=YAMLObject):
source = 'config.yml'
Warning Log: Missing namespace attribute. Converting 'config.yml' to object.
from yaml2object import YAMLObject
class Config(metaclass=YAMLObject):
source = 'config.yml'
namespace = 'invalid'
Warning Log: Missing 'invalid' param in 'config.yml'. Converting 'config.yml' to object.
from yaml2object import YAMLObject
class DevelopmentConfig(metaclass=YAMLObject):
source = 'config.yml'
namespace = 'development'
class TestConfig(metaclass=YAMLObject):
source = 'config.yml'
namespace = 'test'
3. Access your YAML as python object
>>> Config.to_dict()
>>> Config.development.to_dict()
>>> Config.development.database.to_dict()
>>> Config.development.database.adapter
>>> Config.development.nested_param.param1.sub_param1
>>> DevelopmentConfig.to_dict()
>>> DevelopmentConfig.database.to_dict()
>>> DevelopmentConfig.database.adapter
>>> DevelopmentConfig.database.database
>>> TestConfig.to_dict()
>>> TestConfig.port
>>> TestConfig.database.to_dict()
>>> TestConfig.database.adapter
>>> TestConfig.database.database
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
yaml2object-1.0.4.zip
(8.9 kB
view hashes)
Built Distribution
Close
Hashes for yaml2object-1.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9767e965e6f3708578de7914d40b409a4724960a800869498e1f835c334335f6 |
|
MD5 | b8473c86020175e0cd2643425ffaa317 |
|
BLAKE2b-256 | 0c7eae4b86d6c21aefa6861b56332b97c6d6880736fd03cfbec10aa0adba1508 |