The TreeNode class

Module Contents



A base class for representing an object that lives "at" a filesystem directory.

class pygsti.protocols.treenode.TreeNode(possible_child_name_dirs, child_values=None)

Bases: object

A base class for representing an object that lives “at” a filesystem directory.

  • possible_child_name_dirs (dict) – A dictionary with string keys and values that maps possible child names (keys of this TreeNode) to directory names (where those keys are stored).

  • child_values (dict, optional) – A dictionary of child values (may be other TreeNode objects).

abstract classmethod from_dir(cls, dirname, parent=None, name=None)

Load a TreeNode from the data rooted at dirname.

  • dirname (str or Path) – The directory path to load from.

  • parent (TreeNode, optional) – This node’s parent node, if it’s already loaded.

  • name (immutable, optional) – The name of this node, usually a string or tuple. Almost always the key within parent that refers to the loaded node (this can be different from the directory name).

_init_children(self, dirname, meta_subdir=None, **kwargs)

An iterator over the keys (child names) of this tree node.

__contains__(self, key)

An iterator over the (child_name, child_node) pairs of this node.

__getitem__(self, key)
abstract _create_childval(self, key)

Dictionary paths leading to data objects/nodes beneath this one.


list – A list of tuples, each specifying the tree traversal to a child node. The first tuple is the empty tuple, referring to this (root) node.

view(self, keys_to_keep)

Get a “view” of this tree node that only has a subset of this node’s children.


keys_to_keep (iterable) – A sequence of key names to keep.



prune_tree(self, paths, paths_are_sorted=False)

Prune the tree rooted here to include only the given paths, discarding all other leaves & branches.

  • paths (list) – A list of tuples specifying the paths to keep.

  • paths_are_sorted (bool, optional) – Whether paths is sorted (lexographically). Setting this to True will save a little time.


TreeNode – A view of this node and its descendants where unwanted children have been removed.

abstract write(self, dirname, parent=None)

Write this tree node to a directory.

  • dirname (str or Path) – Directory to write to.

  • parent (TreeNode, optional) – This node’s parent.



_write_children(self, dirname, write_subdir_json=True)

Writes this node’s children to directories beneath dirname.

Each child node is written to a sub-directory named according to the sub-directory names associated with the child names (keys) of this node.

  • dirname (str or Path) – The root directory to write to.

  • write_subdir_json (bool, optional) – If True, a dirname/edesign/subdirs.json file is written that contains child name information, i.e. the map between directory names and child names (it is useful to not requires these be the same, and sometimes it’s useful to name children with a tuple rather than just a string).