Skip to main content

Yaml 1.2 support for Python with a nicer api

Project description

safer-yaml

PyPI Python Build Coverage License: MIT

A tiny wrapper around PyYAML and yamlcore that provides a consistent, JSON-like API for YAML 1.2.

Why

  • Consistent API: mirrors Python’s json module: load, loads, dump, dumps.
  • YAML 1.2 semantics: avoids the classic “Norway problem” and treats on/off/yes/no as plain strings unless explicitly boolean.
  • Simple file/stream handling: accepts path-like objects and text streams, with UTF-8 file I/O by default.
  • Minimal quoting and stable ordering: outputs human-friendly YAML with minimal necessary quotes and preserves insertion order.

Install

pip install safer-yaml

Quick start

from safer_yaml import load, loads, dump, dumps

# YAML 1.2 semantics (not YAML 1.1)
assert loads("no\n") == "no"        # not False
assert loads("on\n") == "on"        # not True

# Dump to a string
s = dumps({"k": "no"})
print(s)  # k: no

Dump and load using a string filename

from safer_yaml import load, dump

dump({"a": "1:2", "b": "a: b"}, "myfile.yaml")
data = load("myfile.yaml")
assert data == {"a": "1:2", "b": "a: b"}

Use pathlib.Path too

from pathlib import Path
from safer_yaml import load, dump

p = Path("data.yaml")
dump({"a": "1:2", "b": "a: b"}, p)
data = load(p)
assert data == {"a": "1:2", "b": "a: b"}

Work with text streams

import io
from safer_yaml import dump

buf = io.StringIO()
dump({"outer": {"inner": "v"}}, buf)
print(buf.getvalue())

Control formatting (width and indent)

from safer_yaml import dumps

# Use defaults (no wrapping, indent=4)
print(dumps({"k": ["a", "b", "c"]}))

# Set your own
yaml_text = dumps({"nested": {"x": 1}}, width=80, indent=2)
print(yaml_text)

API by example

  • loads
loads("k: v\n")            # -> {"k": "v"}
  • load
load("config.yaml")        # path-like (opened UTF-8)
  • dumps
dumps({"k": "v"})          # -> "k: v\n"
dumps({"k": "v"}, width=80, indent=2)
  • dump
dump({"k": "v"}, "out.yaml")
with open("out.yaml", "r", encoding="utf-8") as f:
    assert f.read().strip() == "k: v"

Defaults and constants

  • DEFAULT_WIDTH = 1_000_000 (effectively disables wrapping)
  • DEFAULT_INDENT = 4

If you need the constants:

from safer_yaml.safer_yaml import DEFAULT_WIDTH, DEFAULT_INDENT

Behavior and notes

  • YAML 1.2 booleans: true/false are booleans; yes/no/on/off are strings by default.
  • Minimal quoting: values are quoted only when required (e.g., "a: b", "#tag").
  • Insertion order is preserved; keys are not sorted (sort_keys=False).
  • Paths are handled cross-platform using os.fspath and PathLike.
  • Uses yamlcore’s CCoreLoader/CCoreDumper for correctness and performance with YAML 1.2.

Compatibility

  • Python: 3.9+
  • Platforms: cross-platform (Windows, macOS, Linux)

License MIT — see LICENSE.

Links

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

safer_yaml-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safer_yaml-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file safer_yaml-0.1.1.tar.gz.

File metadata

  • Download URL: safer_yaml-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for safer_yaml-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bd930bf908a6ed65477427ea49a2598654ed57044e78961675ef9302481f8222
MD5 18ade97f551234303cc06192adf95512
BLAKE2b-256 2ec8b67cc437c270f0ea44bb5e6564d071028f594dbb2e0748ede34a061cd577

See more details on using hashes here.

File details

Details for the file safer_yaml-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: safer_yaml-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for safer_yaml-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e7934d1204283695ca1e74f9d59c52898648792af49cebe609cd7e6c3bf28d7
MD5 3a478c5530927ab3d70b70780465f423
BLAKE2b-256 cb4a0f7e1f6f199894be48962319288948c15be5630e2adc1bb419946b66d95d

See more details on using hashes here.

Supported by

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