Skip to main content

Yaml 1.2 support for Python with a nicer api

Project description

safer-yaml

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.8+
  • 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.tar.gz (6.1 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-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: safer_yaml-0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for safer_yaml-0.1.tar.gz
Algorithm Hash digest
SHA256 dace44e5e585a0de29fec0968c9596dc80d922c0101e846a21793f826d395439
MD5 113987c547ebbcf79ed02af5ae165105
BLAKE2b-256 96d6e600130ff2780bb8800d09ee0b2d09e951d247144b02105a3e0cb4b191ea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: safer_yaml-0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for safer_yaml-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef278359d0de5d8baeabcde8d736a4d74d5182d30c2f0d4440f82916587b2fd1
MD5 750a6eb5109d6da55caa4518b4c7900d
BLAKE2b-256 f0a4f76ee8f73bcc7abe2e4b34bad5bb508f20c4fe1b1a3e3d12ccbd96116000

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