json-style YAML 1.2 and comment-preserving editing for Python, in one
package. Edit a file or apply a JSON Patch to it and only what you touch
changes; a native backend and a pure-Python one sit behind the same API.
Full results, with memory and the method →
- Edits that keep your file. An unedited document writes back byte for byte. Setting a value changes only that value, in its own style, and comments stay where they were (documents and edits).
- JSON Patch and merge patch on YAML. RFC 6902 and RFC 7396 patches apply to a YAML file all or nothing, and keep its layout and comments (patches).
- YAML 1.2 by default.
yesis a string and0755is the integer 755;<<merge keys are there when you ask for them. Checked against the yaml-test-suite (conformance; what changes from YAML 1.1). - The
jsonAPI you know.loads,load,dumpsanddumpwith plaindict,listandstr,parse_float,defaultandsort_keys, and oneYAMLErrorthat is aValueError. - Safe on untrusted input. No tag constructs anything unless you hand it
a function. Depth
and alias expansion are bounded by default, and
Limits.untrusted()adds caps on sizes for network input (limits). - Drop-in modules for PyYAML and ruamel.yaml. Change one import and keep your calls; the guides for PyYAML and ruamel.yaml list what differs.
oneyaml isn't on PyPI. To try it, install from a clone:
pip install ./oneyaml-pythonRead a file into plain Python data, then edit it in place: a literal block gets a new line, a patch changes one value and adds a key, and the comments and every other byte stay.
import oneyaml
text = """\
# deploy settings
name: api
script: |
make build
replicas: 2 # raised by the release job
"""
config = oneyaml.loads(text) # plain Python data, as json.loads gives
print(config)
doc = oneyaml.parse(text) # a document that keeps every byte you don't change
doc["script"] = "make build\nmake test\n"
doc.patch([
{"op": "replace", "path": "/replicas", "value": 3},
{"op": "add", "path": "/region", "value": "eu-west-1"},
])
print(doc, end=""){'name': 'api', 'script': 'make build\n', 'replicas': 2}
# deploy settings
name: api
script: |
make build
make test
replicas: 3 # raised by the release job
region: eu-west-1
Coming from PyYAML? import oneyaml.compat.pyyaml as yaml keeps your code
working, and
YAML 1.2 for PyYAML users
shows what changes when you move to oneyaml.loads. For input you don't
control, pass limits=oneyaml.Limits.untrusted().
- Using oneyaml: the guide: reading and writing, limits and untrusted input, documents, edits and patches, backends, errors.
- Moving from another library:
PyYAML,
ruamel.yaml,
and YAML 1.2 for PyYAML users
(
yes, octal, base-60 numbers, dates, merge keys). - Choosing a library: how oneyaml compares, including where the others win.
- Evidence: performance, conformance.
- Releases: changelog.
- Contributing and security: CONTRIBUTING.md, SECURITY.md, CODE_OF_CONDUCT.md.
- The oneyaml family: oneyaml-kit, the shared conformance and benchmark kit, and oneyaml for Rust (the parsing engine behind the native backend), Go and C#.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.