PlasmaCalcs.tools.trees.Tree
- class PlasmaCalcs.tools.trees.Tree(obj, parent=None)
Bases:
objectnode in a Tree. Might have any number of children, might have one parent.- obj: any object.
- the object stored here, in this node.
- __init__(obj, parent=None)
Methods
__init__(obj[, parent])add_child(child)display([show_depth, max_depth, shorthand])enumerate_flat(*[, include_self])flat(*[, include_self])flat_branches_until(branches_until, *[, ...])html([show_depth, max_depth, shorthand])make_child(obj)make_children(objs)set_parent(parent, *[, _internal])Attributes
DEFAULT_TREE_SHORTHANDDEFAULT_TREE_SHOW_DEPTHDEFAULT_TREE_SHOW_MAX_DEPTH- add_child(child)
- adds this child (a Tree) to self.children. Also, child.set_parent(self).returns the added child.
- property depth
- number of layers above self. (parent, parent’s parents, etc.)depth = 0 for the node with parent = None.
- display(show_depth=None, max_depth=None, *, shorthand=None)
- display self in html. Includes self.html() and DEFAULTS.TREE_CSS.
- show_depth: None or int
- max number of layers of tree to show by default (i.e. “not hidden” by default)None –> use self.DEFAULT_TREE_SHOW_DEPTH if defined else DEFAULTS.TREE_SHOW_DEPTH
- max_depth: None or int
- max number of layers of tree to render (even if all layers are “not hidden”).Anything deeper will not be converted to html string.None –> use self.DEFAULT_TREE_SHOW_MAX_DEPTH if defined else DEFAULTS.TREE_SHOW_MAX_DEPTH
- shorthand: None or bool
- whether to use shorthand for the “Tree([depth=N, height=N, size=N], obj=…)” part of the repr.True –> use shorthand; replace that^ with: “((N, N, N)) …”None –> use self.DEFAULT_TREE_SHORTHAND if defined else DEFAULTS.TREE_SHORTHAND
- enumerate_flat(*, include_self=False)
- returns a generator which iterates over all of self’s descendants, in depth-first order,yielding (index, node) pairs, such that self[index] == node.Note that index will be a tuple with length == node.depth.if include_self, yield self first, as: ((), self)).
- flat(*, include_self=False)
- returns a generator which iterates over all of self’s descendants, in depth-first order.if include_self, yield self first.
- flat_branches_until(branches_until, *, include_self=False)
- returns a generator which iterates over all of self’s descendants, in depth-first order,but stop looking at descendants on a branch as soon as branches_until(node).E.g. self.flat_branches_until(lambda node: node.obj==7) will be similar to flat,but won’t go to any descendants for any node with obj==7.if include self, yield self first, and check branches_until(self) before continuing.otherwise, never check branches_until(self).
- property height
- number of layers below self. (children, children’s children, etc.)height = 0 for a node with no children.
- html(show_depth=None, max_depth=None, *, shorthand=None)
- returns html for displaying self and all of self’s children.
- show_depth: None or int
- max number of layers of tree to show by default (i.e. “not hidden” by default)None –> use self.DEFAULT_TREE_SHOW_DEPTH if defined else DEFAULTS.TREE_SHOW_DEPTH
- max_depth: None or int
- max number of layers of tree to render (even if all layers are “not hidden”).Anything deeper will not be converted to html string.None –> use self.DEFAULT_TREE_SHOW_MAX_DEPTH if defined else DEFAULTS.TREE_SHOW_MAX_DEPTH
- shorthand: None or bool
- whether to use shorthand for the “Tree([depth=N, height=N, size=N], obj=…)” part of the repr.True –> use shorthand; replace that^ with: “((N, N, N)) …”None –> use self.DEFAULT_TREE_SHORTHAND if defined else DEFAULTS.TREE_SHORTHAND
- make_child(obj)
- makes a child of self, with obj as its stored object, and returns the child.
- make_children(objs)
- make_child(obj) for obj in objs; returns the list of newly made children.
- property parent
- parent node of self. None if self is root.When set to a value, calls self.set_parent(value),which also updates tracking info appropriately, and updates parent’s children.
- property parent_ref
- stores parent value, but internally uses weakref to avoid circular references.Users should always use self.parent instead.
- set_parent(parent, *, _internal=False)
- sets self.parent = parent. Also, parent.add_child(self), unless _internal=True.Users should use self.parent = parent instead of calling set_parent directly.
- property size
- number of nodes in this tree. (here and below)size = 1 for a node with no children.