Skip to main content

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)

Release files for ez-yaml 2.2.0

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

Source distribution (sdist)

Source distribution for ez-yaml 2.2.0
File Size Uploaded
ez_yaml-2.2.0.tar.gz 381.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ez-yaml 2.2.0
File Interpreter ABI Platform
ez_yaml-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 728.8 kB

Release files / ez_yaml-2.2.0.tar.gz

Download URL ez_yaml-2.2.0.tar.gz
Size 381.9 kB
Tags Source
SHA-256 checksum
How to use checksums
aa02f14683cd4a32a1be621c5acabe587ddf0e647fef7e075b3c942db722d331
BLAKE2b-256 checksum
How to use checksums
2f5cbc0e52d9ea94a90d63645dc4df2724ad212ef918f3c89101dff78a4f8ed1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.8.3 pkginfo/1.9.6 requests/2.27.1 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.8.13

Release files / ez_yaml-2.2.0-py3-none-any.whl

Download URL ez_yaml-2.2.0-py3-none-any.whl
Size 347.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb1fa5734c7ce948ba146d9efab0d5017a94f1e0eeb2a112d40b62df30468de2
BLAKE2b-256 checksum
How to use checksums
a6f8c7b927a739a658b6d574deabcd0eb293454c5218a3c0208595c98453f12e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.8.3 pkginfo/1.9.6 requests/2.27.1 requests-toolbelt/0.10.1 tqdm/4.64.1 CPython/3.8.13

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

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