Package: serializable-trees
Serializable TreesEasy-to-use serializable tree datatypes for PythonInstallationYou can install thepackage from PyPI:pipinstall--upgradeserializable-treesUsing avirtual environmentis strongly recommended.Notable changesv0.5.0ListNodeis a subclass oflist,
andMapNodeis a subclass ofdict.v0.4.0⚠ Possible infinite loops with releases prior to 0.4.0In early releases,ListNodeand MapNode contents were not
checked for circular references, which could lead to infinite loops.Starting with release 0.4.0, allNodesubclass instances
implement additional checks that prevent the creation of
circular node references.Package contents and basic usageTheserializable_treespackage contains the modulesserializable_trees.basicsimplementing a low-level API
with theListNodeandMapNodeclasses and some helper functionsserializable_trees.treesimplementing a high-level API
with theTraversalPathandTreeclasses.Both high-level API classes from thetreesmodule
are aliased in the base module so they can simply be imported using>>> from serializable_trees import TraversalPath, TreeLoading a data structure from a file is easy,
but its representation is slightly hard to read.Using theto_yaml()orto_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

>>>Treecontents can be accessed directly under therootattribute:>>> 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'
>>>UsingTraversalPathobjects, 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

>>>ListNodeandMapNodeobjects 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 theNodeinstances directly –
in this case, using the.append()method of theListNodeinstance:>>> 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