# Install
`pip install ez_yaml`
# Usage
```python
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
```python
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
```py
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)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jeff-hykin/ez_yaml",
"name": "ez-yaml",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "",
"author": "Jeff Hykin",
"author_email": "jeff.hykin@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/2f/5c/bc0e52d9ea94a90d63645dc4df2724ad212ef918f3c89101dff78a4f8ed1/ez_yaml-2.2.0.tar.gz",
"platform": null,
"description": "# Install\n\n`pip install ez_yaml`\n\n# Usage\n\n```python\nimport ez_yaml\n\n# to_string(obj, settings={})\nez_yaml.to_string({\"thing\": 1, \"abc\": [ 1,2,3 ]})\n\n# to_object(file_path, settings={})\n# to_object(string , settings={})\nez_yaml.to_object(string='''\n\nthing: 1\nabc:\n - 1\n - 2\n - 3\n\n''')\n\n# to_file(obj, file_path, settings={})\nez_yaml.to_file(\n {\"thing\": 1, \"abc\": [ 1,2,3 ]},\n file_path=\"./my_file.yaml\",\n)\n\n```\n\n# Settings\n\n```python\nimport ez_yaml\n\n# to_string(obj, settings={})\nez_yaml.to_string(\n {\"thing\": 1, \"abc\": [ 1,2,3 ]},\n settings=dict(\n # these are the default values\n safe=False,\n width=None,\n allow_duplicate_keys=True,\n explicit_start=False,\n explicit_end=False,\n explict_null=True,\n indent_mapping=3,\n indent_sequence=2,\n offset=0,\n )\n)\n\n# to_file(obj, file_path, settings={})\nez_yaml.to_file(\n {\"thing\": 1, \"abc\": [ 1,2,3 ]},\n file_path=\"./my_file.yaml\",\n settings=dict(\n width=9999999999999,\n explicit_start=True,\n explicit_end=True,\n )\n)\n```\n\n# Custom Yaml Tags Example\n\n```py\nfrom ez_yaml import yaml\n\n@yaml.register_class\nclass YourCustomClass:\n yaml_tag = \"!python/YourCustomClass\"\n \n def __init__(self, something):\n self.something = something\n \n @classmethod\n def from_yaml(cls, constructor, node):\n # will print true\n print(node.value.startswith(\"blah blah YourCustomClass(something:\"))\n # node.value is the python-value\n return YourCustomClass(something=node.value[len(\"blah blah YourCustomClass(something:\")-1:-1])\n \n @classmethod\n def to_yaml(cls, representer, object_of_this_class):\n representation = f\"blah blah YourCustomClass(something:{object_of_this_class.something})\"\n # ^ needs to be a string (or some other yaml-primitive)\n return representer.represent_scalar(\n tag=cls.yaml_tag,\n value=representation,\n style=None,\n anchor=None\n )\n\n\ndata = [\n YourCustomClass(['blah blah blah']),\n YourCustomClass({\"thing\": \"lorem ipsum\"}),\n]\n\n# will get generated with a tag\noutput = ez_yaml.to_string(data)\n# will detect tag and convert it back to a YourCustomClass\nyaml.load(output)\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Straighforward wrapper around Ruamel Yaml",
"version": "2.2.0",
"project_urls": {
"Homepage": "https://github.com/jeff-hykin/ez_yaml"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "a6f8c7b927a739a658b6d574deabcd0eb293454c5218a3c0208595c98453f12e",
"md5": "6cc679afb3c597936589573afc7f55cf",
"sha256": "fb1fa5734c7ce948ba146d9efab0d5017a94f1e0eeb2a112d40b62df30468de2"
},
"downloads": -1,
"filename": "ez_yaml-2.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6cc679afb3c597936589573afc7f55cf",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 346960,
"upload_time": "2024-02-02T16:37:32",
"upload_time_iso_8601": "2024-02-02T16:37:32.937711Z",
"url": "https://files.pythonhosted.org/packages/a6/f8/c7b927a739a658b6d574deabcd0eb293454c5218a3c0208595c98453f12e/ez_yaml-2.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2f5cbc0e52d9ea94a90d63645dc4df2724ad212ef918f3c89101dff78a4f8ed1",
"md5": "61673b59eccf6b07dd4e775e1b384af5",
"sha256": "aa02f14683cd4a32a1be621c5acabe587ddf0e647fef7e075b3c942db722d331"
},
"downloads": -1,
"filename": "ez_yaml-2.2.0.tar.gz",
"has_sig": false,
"md5_digest": "61673b59eccf6b07dd4e775e1b384af5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 381857,
"upload_time": "2024-02-02T16:37:34",
"upload_time_iso_8601": "2024-02-02T16:37:34.999174Z",
"url": "https://files.pythonhosted.org/packages/2f/5c/bc0e52d9ea94a90d63645dc4df2724ad212ef918f3c89101dff78a4f8ed1/ez_yaml-2.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-02 16:37:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jeff-hykin",
"github_project": "ez_yaml",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "ez-yaml"
}