Skip to main content

yaml2object is a python library that allows dot notation access for YAML file.

Status

PyPI pyversions PyPI Build Status Coverage Status last commit License PyPI Package monthly downloads PyPI download week

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

  1. Set yaml2object.YAMLObject as meta-class of your config class.
  2. 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

NOTE

An underscore is added before any key matching python keyword lists https://docs.python.org/3/library/keyword.html#keyword.kwlist

Example:

from yaml2object import YAMLObject

config_dict = {'from': 'value1', 'None': 'value2'}
Config = YAMLObject('Config', (object,), {'source': config_dict})
    Missing namespace attribute. Converting source to object.
    Param from is a python keyword. Adding _ (underscore) before the param and can be accessed as _from
    Param None is a python keyword. Adding _ (underscore) before the param and can be accessed as _None
>>> Config._from
value1
>>> Config._None
value2

Metadata

Release files for yaml2object 1.0.13

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

Source distribution (sdist)

Source distribution for yaml2object 1.0.13
File Size Uploaded
yaml2object-1.0.13.zip 14.5 kB Details

Built distribution (wheel)

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

Total release size: 24.3 kB

Release files / yaml2object-1.0.13.zip

Download URL yaml2object-1.0.13.zip
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
663ec364cc953caef9b9634131e7a0b53ff097a6bfe77f88da19cc767e6d19da
BLAKE2b-256 checksum
How to use checksums
cf651012c6d466f726c30f8783f4cf5659827ba462cf0a42d906b7fedd07e6dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

Release files / yaml2object-1.0.13-py3-none-any.whl

Download URL yaml2object-1.0.13-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5d7c8dc8ee3b27cfd598b68bac8a63801ddb7d9970b46893abbf4f273cb8832
BLAKE2b-256 checksum
How to use checksums
c1a8f7005721dc079cde1f527ad665c6a57b41157a4272739af3a00bac5d95ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

1.0.13 This release

2 release files

1.0.12

2 release files

1.0.11

1 release file

1.0.10

2 release files

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

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