pygsti.protocols.treenode

The TreeNode class

Module Contents

Classes

TreeNode

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.

Parameters
  • 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.

Parameters
  • 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)
keys(self)

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

__contains__(self, key)
__len__(self)
items(self)

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

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

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

Returns

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.

Parameters

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

Returns

TreeNode

prune_tree(self, paths, paths_are_sorted=False)

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

Parameters
  • 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.

Returns

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.

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

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

Returns

None

_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.

Parameters
  • 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).

Returns

None