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_dirsdict

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_valuesdict, optional

A dictionary of child values (may be other TreeNode objects).

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

Load a TreeNode from the data rooted at dirname.

Parameters
dirnamestr or Path

The directory path to load from.

parentTreeNode, optional

This node’s parent node, if it’s already loaded.

nameimmutable, 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).

keys()

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

items()

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

underlying_tree_paths()

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(keys_to_keep)

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

Parameters
keys_to_keepiterable

A sequence of key names to keep.

Returns

TreeNode

prune_tree(paths, paths_are_sorted=False)

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

Parameters
pathslist

A list of tuples specifying the paths to keep.

paths_are_sortedbool, 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(dirname, parent=None)

Write this tree node to a directory.

Parameters
dirnamestr or Path

Directory to write to.

parentTreeNode, optional

This node’s parent.

Returns

None