Tinytree - API

def constructFromList(lst)
lst

a nested list of Tree objects

Returns a list consisting of the nodes at the base of each tree. Trees are constructed "bottom-up", so all parent nodes for a particular node are guaranteed to exist when "addChild" is run.

class Tree(object)

A simple implementation of an ordered tree

def __init__(self, children=None)
children
A nested list specifying a tree of children
def addChildrenFromList(self, children)

Add children to this node.

children
A nested list specifying a tree of children
def addChild(self, node)

Add a child to this node.

child
A Tree object
def register(self, parent)

Called after a node has been added to a parent.

child
A Tree object
def index(self)

Return the index of this node in the parent child list, based on object identity.

def remove(self)

Remove this node from its parent. Returns the index this node had in the parent child list.

def clear(self)

Clear all the children of this node. Return a list of the removed children.

def replace(self, *nodes)

Replace this node with a sequence of other nodes. This is equivalent to deleting this node from the child list, and then inserting the specified sequence in its place.

nodes
A sequence of Tree objects
def reparent(self, node)

Inserts a node between the current node and its parent. Returns the specified parent node.

node
A Tree object
def isDescendantOf(self, node)

Returns true if the specified node lies on the path to the root from this node.

node
A Tree object
def siblings(self)

Generator yielding all siblings of this node, including this node itself.

def pathToRoot(self)

Generator yielding all objects on the path from this node to the root of the tree, including this node itself.

def pathFromRoot(self)

Generator yielding all nodes on the path to this node from the root of the tree, including this node itself.

def getRoot(self)

Return the topmost node in the tree.

def preOrder(self)

Return a list of subnodes in PreOrder.

def postOrder(self)

Return a list of the subnodes in PostOrder.

def findChild(self, *func, **kwargs)

Find the first child matching all specified selectors in a pre-order traversal of this node's subnodes. Return None if no matching object is found.

func
A list of selector functions, that accept a node, and return a boolean.
kwargs
A dictionary of attribute selectors. Checks that matching attributes exist, and that their values are equal to the specified values.
def findParent(self, *func, **kwargs)

Find the first node matching func in a traversal to the root of the tree. Return None if no matching object is found.

func
A list of selector functions, that accept a node, and return a boolean.
kwargs
A dictionary of attribute selectors. Checks that matching attributes exist, and that their values are equal to the specified values.
def findForwards(self, *func, **kwargs)

Search forwards in a preOrder traversal of the whole tree (not this node's subnodes). Return None if object not found.

func
A list of selector functions, that accept a node, and return a boolean.
kwargs
A dictionary of attribute selectors. Checks that matching attributes exist, and that their values are equal to the specified values.
def findBackwards(self, *func, **kwargs)

Search backwards in a preOrder traversal of the whole tree (not this node's subnodes). Return None if object not found.

func
A list of selector functions, that accept a node, and return a boolean.
kwargs
A dictionary of attribute selectors. Checks that matching attributes exist, and that their values are equal to the specified values.
def getPrevious(self)

Find the previous node in the preOrder traversal of the tree.

def getNext(self)

Find the next node in the preOrder traversal of the tree.

def getDepth(self)

Return the depth of this node, i.e. the number of nodes on the path to the root.

def findAttr(self, attr, default=None)

Traverses the path to the root of the tree, looking for the specified attribute. If it is found, return it, else return default.

attr
A string attribute name
default
Arbitrary default return value
def attrsToRoot(self, attr)

Traverses the path from this node to the root of the tree, and yields a value for each attribute. Nodes that do not have the attribute and attribute values that test false are ignored.

attr
A string attribute name
def treeProp(name)

Define a property whose value should be looked up on nodes between this node and the root, inclusive. Returns the first matching attribute. Raises ValueError if no matching attribute is found.

name
Property name
def dump(self, outf=sys.stdout)

Dump a formatted representation of this tree to the specified file descriptor.

outf
Output file descriptor.
def count(self)

Number of nodes in this tree, including the root.

Copyright Nullcube 2008