Skip to main content

Serializable tree datatypes for Python

Project description

Serializable Trees

Easy-to-use serializable tree datatypes for Python

Installation

You can install the package from PyPI:

pip install --upgrade serializable-trees

Using a virtual environment is strongly recommended.

Notable changes

v0.5.0

ListNode is a subclass of list, and MapNode is a subclass of dict.

v0.4.0


⚠ Possible infinite loops with releases prior to 0.4.0

In early releases, ListNode and MapNode contents were not checked for circular references, which could lead to infinite loops.

Starting with release 0.4.0, all Node subclass instances implement additional checks that prevent the creation of circular node references.


Package contents and basic usage

The serializable_trees package contains the modules

  • serializable_trees.basics implementing a low-level API with the ListNode and MapNode classes and some helper functions
  • serializable_trees.trees implementing a high-level API with the TraversalPath and Tree classes.

Both high-level API classes from the trees module are aliased in the base module so they can simply be imported using

>>> from serializable_trees import TraversalPath, Tree

Loading a data structure from a file is easy, but its representation is slightly hard to read.

Using the to_yaml() or to_json() methods and printing the results improves readability:

>>> gitlab_ci = Tree.from_file(".gitlab-ci.yml")
>>> gitlab_ci
Tree({'include': {'project': 'blackstream-x/building-blocks', 'file': '/Pipelines/python/pip/package+docs.gitlab-ci.yml'}, 'variables': {'MODULE_NAME': 'serializable_trees'}, 'python:mypy': {'variables': {'EXTRA_REQUIREMENTS': 'types-pyyaml'}}, 'python:pylint': {'variables': {'CODESTYLE_TARGET': 'src'}}})
>>> 
>>> print(gitlab_ci.to_yaml())
include:
  project: blackstream-x/building-blocks
  file: /Pipelines/python/pip/package+docs.gitlab-ci.yml
variables:
  MODULE_NAME: serializable_trees
python:mypy:
  variables:
    EXTRA_REQUIREMENTS: types-pyyaml
python:pylint:
  variables:
    CODESTYLE_TARGET: src

>>> 

Tree contents can be accessed directly under the root attribute:

>>> gitlab_ci.root.include
{'project': 'blackstream-x/building-blocks', 'file': '/Pipelines/python/pip/package+docs.gitlab-ci.yml'}
>>> gitlab_ci.root.include.project
'blackstream-x/building-blocks'
>>> gitlab_ci.root.include.file
'/Pipelines/python/pip/package+docs.gitlab-ci.yml'
>>>

Using TraversalPath objects, you can manipulate the tree. In this case, remove a portion from the tree and return it using the .crop() method:

>>> include_path = TraversalPath("include")
>>> old_include_contents = gitlab_ci.crop(include_path)
>>> old_include_contents
{'project': 'blackstream-x/building-blocks', 'file': '/Pipelines/python/pip/package+docs.gitlab-ci.yml'}
>>> 
>>> print(gitlab_ci.to_yaml())
variables:
  MODULE_NAME: serializable_trees
python:mypy:
  variables:
    EXTRA_REQUIREMENTS: types-pyyaml
python:pylint:
  variables:
    CODESTYLE_TARGET: src

>>> 

ListNode and MapNode objects can be used to define new items. In this case, the Tree object’s .graft() method is used:

>>> from serializable_trees.basics import ListNode, MapNode
>>> gitlab_ci.graft(include_path, ListNode([MapNode(template="Security/Secret-Detection.gitlab-ci.yml")]))
>>> 
>>> print(gitlab_ci.to_yaml())
variables:
  MODULE_NAME: serializable_trees
python:mypy:
  variables:
    EXTRA_REQUIREMENTS: types-pyyaml
python:pylint:
  variables:
    CODESTYLE_TARGET: src
include:
- template: Security/Secret-Detection.gitlab-ci.yml

>>> 

You can also manipulate the Node instances directly – in this case, using the .append() method of the ListNode instance:

>>> gitlab_ci.root.include.append(old_include_contents)
>>> 
>>> print(gitlab_ci.to_yaml())
variables:
  MODULE_NAME: serializable_trees
python:mypy:
  variables:
    EXTRA_REQUIREMENTS: types-pyyaml
python:pylint:
  variables:
    CODESTYLE_TARGET: src
include:
- template: Security/Secret-Detection.gitlab-ci.yml
- project: blackstream-x/building-blocks
  file: /Pipelines/python/pip/package+docs.gitlab-ci.yml

>>> 
>>> gitlab_ci.root.include
[{'template': 'Security/Secret-Detection.gitlab-ci.yml'}, {'project': 'blackstream-x/building-blocks', 'file': '/Pipelines/python/pip/package+docs.gitlab-ci.yml'}]
>>> 

Further reading

→ Full documentation

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

serializable-trees-0.5.0.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

serializable_trees-0.5.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file serializable-trees-0.5.0.tar.gz.

File metadata

  • Download URL: serializable-trees-0.5.0.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for serializable-trees-0.5.0.tar.gz
Algorithm Hash digest
SHA256 5a0d46a769325b670ab64620274cd9948e32a18b9bca697295185d0226542df8
MD5 dea306f22d8dd2926cbd6b8ce29e58dd
BLAKE2b-256 c4af863e86d486ac7e33996175f55317d211d282010bbff0e749a825a983852c

See more details on using hashes here.

File details

Details for the file serializable_trees-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for serializable_trees-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6acb2343f472a0d4e8265a4d785bd105fbed229d36b32fbe5dd1adeb2374e85
MD5 073f0d8628943d2a224e8fa71e857a6b
BLAKE2b-256 8d62fe78ca08cd3234f4339bf4371b2cbf1951af786d121f95db4fe73c2ac76a

See more details on using hashes here.

Supported by

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