Skip to main content
https://sourceforge.net/p/ruamel-yaml-base/code/ci/default/tree/_doc/_static/license.svg?format=raw https://sourceforge.net/p/ruamel-yaml-base/code/ci/default/tree/_doc/_static/pypi.svg?format=raw https://sourceforge.net/p/oitnb/code/ci/default/tree/_doc/_static/oitnb.svg?format=raw https://sourceforge.net/p/ryd/code/ci/default/tree/_doc/_static/ryd.svg?format=raw

YAMLBase class with saving, delayed loading and optional version support

The YAMLBase class is initiated with a pathlib.Path or string based filename of a document containing a single YAML file. This file needs to exists unless the create_ok parameter is supplied, in which case an instance of the argument to that parameter is created (typically a list or dict like object) as the root of the data structure. If the argument is True an empty ruamel.yaml.CommentedMap() instance is created as the root.

Access to the root object is made via the .data attribute, if an existing file was specified, it is read in on the first access to .data.

from ruamel.yaml.base import YAMLBase

file_name = '/data0/talks/meta.yaml'

base = YAMLBase(file_name, create_ok=True)
base.data['PyCon 2006'] = dict(location='Dallas, TX, USA', title='Extending the life of CVS with Python')
base.data['EuroPython 2016'] = dict(location='Bilbao, Spain', title='Beyond scraping')
base.changed = True
base.save()
print(open(file_name).read())

Which prints:

PyCon 2006:
  location: Dallas, TX, USA
  title: Extending the life of CVS with Python
EuroPython 2016:
  location: Bilbao, Spain
  title: Beyond scraping

The document is normally loaded using ruamel.yaml’s default round-trip mode. If the initial access to an existing document is done via .fast_data, the loading is done via the faster C loader (which is not fully YAML 1.2) compatible. In that case you should not save the resulting file.

The save will not actually dump out the YAML document file, unless .changed has been set to True. Saving to file sets .changed back to False. Using the force=True will write to file independent of changed. You can also add an extra out argument, e.g. instead of the last two lines in the example above, you could do:

import sys
base.save(out=sys.stdout)  # not changing `.changed`
base.save()

automatic saving

If YAMLBase is initialised with auto_save=True, and .changed has been set the YAML file will be dumped when the object goes out of scope.

internal progress

The initialisation has an optional verbose argument which can be set to a positive integer to get some information on what is going on internally.

alternative initialisation

When you subclass YAMLBase, you can pre set the create_ok and auto_save values by setting attributes before calling super():

class X(YAMLBase):
    def __init__(self, path):
        self._create_ok = True
        self._auto_save = True
        super().__init__(path=path)

Release files for ruamel.yaml.base 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ruamel.yaml.base 0.3.2
File Size Uploaded
ruamel.yaml.base-0.3.2.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ruamel.yaml.base 0.3.2
File Interpreter ABI Platform
ruamel.yaml.base-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 19.3 kB

Release files / ruamel.yaml.base-0.3.2.tar.gz

Download URL ruamel.yaml.base-0.3.2.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
88b6edc8ace60c12d98f05fda22e5d9d69ba9a4b531cf54783142151145b0372
BLAKE2b-256 checksum
How to use checksums
4348a9857909c0c2982c79e7ace20b9e63455b457f62aae0b18f94d5f6ccdbaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / ruamel.yaml.base-0.3.2-py3-none-any.whl

Download URL ruamel.yaml.base-0.3.2-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c3dbd29a507579863a492e58f556e2d922b13dee1846b93a0b8fb6b76d98f440
BLAKE2b-256 checksum
How to use checksums
161b0c255d7e70b4c0178719f6f74457c54a3b120483c9041a8bbf331617da64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.4

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page