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
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
Built Distribution
File details
Details for the file serializable-trees-0.5.1.tar.gz
.
File metadata
- Download URL: serializable-trees-0.5.1.tar.gz
- Upload date:
- Size: 18.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff8789c116fcd700b85a2cbfde218d07ef3368f8d0fc1ddf77cbe2c4722e4e57 |
|
MD5 | 97be616834938b393c098e384cb83521 |
|
BLAKE2b-256 | 503191771ac075524a89c708286fbc0e943861bee38b1a29062a5db0e0a99197 |
Provenance
File details
Details for the file serializable_trees-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: serializable_trees-0.5.1-py3-none-any.whl
- Upload date:
- Size: 12.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd4d9b1d2ae941ae486624942827de02e872c7025736504aef2d3f86a87008ad |
|
MD5 | eea3e69a86c0dac3b682c58f5cef4089 |
|
BLAKE2b-256 | 5c46f61d17bdb7b66827cb94e87dd2c6d18abae27c716ded46290013af9780b0 |