Skip to main content

Straighforward wrapper around Ruamel Yaml

Project description

Install

pip install ez_yaml

Usage

import ez_yaml

# to_string(obj, settings={})
ez_yaml.to_string({"thing": 1, "abc": [ 1,2,3 ]})

# to_object(file_path, settings={})
# to_object(string   , settings={})
ez_yaml.to_object(string='''

thing: 1
abc:
    - 1
    - 2
    - 3

''')

# to_file(obj, file_path, settings={})
ez_yaml.to_file(
    {"thing": 1, "abc": [ 1,2,3 ]},
    file_path="./my_file.yaml",
)

Settings

import ez_yaml

# to_string(obj, settings={})
ez_yaml.to_string(
    {"thing": 1, "abc": [ 1,2,3 ]},
    settings=dict(
        # these are the default values
        safe=False,
        width=None,
        allow_duplicate_keys=True,
        explicit_start=False,
        explicit_end=False,
        explict_null=True,
        indent_mapping=3,
        indent_sequence=2,
        offset=0,
    )
)

# to_file(obj, file_path, settings={})
ez_yaml.to_file(
    {"thing": 1, "abc": [ 1,2,3 ]},
    file_path="./my_file.yaml",
    settings=dict(
        width=9999999999999,
        explicit_start=True,
        explicit_end=True,
    )
)

Custom Yaml Tags Example

from ez_yaml import yaml

@yaml.register_class
class YourCustomClass:
    yaml_tag = "!python/YourCustomClass"
    
    def __init__(self, something):
        self.something = something
    
    @classmethod
    def from_yaml(cls, constructor, node):
        # will print true
        print(node.value.startswith("blah blah YourCustomClass(something:"))
        # node.value is the python-value
        return YourCustomClass(something=node.value[len("blah blah YourCustomClass(something:")-1:-1])
    
    @classmethod
    def to_yaml(cls, representer, object_of_this_class):
        representation = f"blah blah YourCustomClass(something:{object_of_this_class.something})"
        # ^ needs to be a string (or some other yaml-primitive)
        return representer.represent_scalar(
            tag=cls.yaml_tag,
            value=representation,
            style=None,
            anchor=None
        )


data = [
    YourCustomClass(['blah blah blah']),
    YourCustomClass({"thing": "lorem ipsum"}),
]

# will get generated with a tag
output = ez_yaml.to_string(data)
# will detect tag and convert it back to a YourCustomClass
yaml.load(output)

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

ez_yaml-2.2.0.tar.gz (381.9 kB view hashes)

Uploaded Source

Built Distribution

ez_yaml-2.2.0-py3-none-any.whl (347.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page