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
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
ez_yaml-2.2.0.tar.gz
(381.9 kB
view details)
Built Distribution
ez_yaml-2.2.0-py3-none-any.whl
(347.0 kB
view details)
File details
Details for the file ez_yaml-2.2.0.tar.gz
.
File metadata
- Download URL: ez_yaml-2.2.0.tar.gz
- Upload date:
- Size: 381.9 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
aa02f14683cd4a32a1be621c5acabe587ddf0e647fef7e075b3c942db722d331
|
|
MD5 |
61673b59eccf6b07dd4e775e1b384af5
|
|
BLAKE2b-256 |
2f5cbc0e52d9ea94a90d63645dc4df2724ad212ef918f3c89101dff78a4f8ed1
|
File details
Details for the file ez_yaml-2.2.0-py3-none-any.whl
.
File metadata
- Download URL: ez_yaml-2.2.0-py3-none-any.whl
- Upload date:
- Size: 347.0 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
fb1fa5734c7ce948ba146d9efab0d5017a94f1e0eeb2a112d40b62df30468de2
|
|
MD5 |
6cc679afb3c597936589573afc7f55cf
|
|
BLAKE2b-256 |
a6f8c7b927a739a658b6d574deabcd0eb293454c5218a3c0208595c98453f12e
|