A simple solution that allows dot notation for YAML file.
Project description
yaml2object is a python library that allows dot notation access for YAML file.
Status
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 OR python dictionary
- namespace: param key in YAML file OR python dictionary
When source is a YAML file
from yaml2object import YAMLObject
class Config(metaclass=YAMLObject):
source = 'config.yml'
> WarningLog: Missing namespace attribute. Converting 'config.yml' to object.
from yaml2object import YAMLObject
class Config(metaclass=YAMLObject):
source = 'config.yml'
namespace = 'invalid'
> WarningLog: 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'
DefaultConfig = YAMLObject('DefaultConfig', (object,), {'source': 'config.yml', 'namespace': 'defaults'})
Source can also be a python dictionary
from yaml2object import YAMLObject
config = {'defaults': {'database':
{'adapter': 'postgresql', 'database': 'development'},
'port': 8000,
'nested_param':
{'param1': {'sub_param1': 'sub_param1 value', 'sub_param2': 'sub_param2 value'}}}}
DefaultConfig = YAMLObject('DefaultConfig', (object,), {'source': config, 'namespace': 'defaults'})
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
>>> DefaultConfig.to_dict()
>>> DefaultConfig.database.to_dict()
>>> DefaultConfig.database.adapter
>>> DefaultConfig.nested_param.param1.sub_param1
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for yaml2object-1.0.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | acced64bd8017208d15dc6780d660d34fbea771a5571e79f8e08014062fa056f |
|
MD5 | bbbde250a8a3c124ee096a7e0654d2a7 |
|
BLAKE2b-256 | e0ad88cc20d28e3699436b67fea4de53752a84b3cb911ea7fce5d1ca694de173 |