Package gnue :: Package common :: Package utils :: Module tree :: Class Node
[show private | hide private]

Type Node

object --+
         |
        Node

Known Subclasses:
NamedNode

A node in a n-ary tree.

Instances of this class represent nodes that make up a tree structure. Each node can have one parent (a node with a parent of None is a root node) and an arbitary number of children.

The parent of a node is defined on creation of the node, but can be changed later. Nodes keep track of their children automatically, so changing the parent of a node from A to B will automatically remove the node from A's list of children and add it to B's list of children.

Children of a node are sorted in the order in which they were attached to the parent.
Method Summary
  __init__(self, parent)
Initialize a new node.
  _set_parent_(self, parent)
Hook for subclasses to register a parent change.
  walk(self, function, *args, **kwargs)
Apply a function to all nodes of the (sub)tree rooted at this node.
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
Return hash(x)...
  __new__(T, S, ...)
Return a new object with type S, a subtype of T...
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __repr__(x)
Return repr(x)...
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
Return str(x)...

Property Summary
[Node] children: List of child nodes.
Node or None parent: Parent node, or None if this is the root of the tree.
Node root: Root node of the tree this node is a member of.

Method Details

__init__(self, parent)
(Constructor)

Initialize a new node.
Parameters:
parent - Parent node.
           (type=Node)
Overrides:
__builtin__.object.__init__

_set_parent_(self, parent)

Hook for subclasses to register a parent change.

Subclasses can implement this method to execute stuff that has to be done whenever a node gets a new parent.

walk(self, function, *args, **kwargs)

Apply a function to all nodes of the (sub)tree rooted at this node.

The function is first applied to this node, then to the first child node, then to all children of the first child node recursively, then to the next child node, etc.

The function is called with the repective node as the first argument, followed by the positional arguments and keyword arguments provided in the call to walk.
Parameters:
function - Function to call on every node.
           (type=callable)
args - Positional arguments
           (type=tuple)
kwargs - Keyword arguments
           (type=dict)

Property Details

children

List of child nodes. (readonly)

parent

Parent node, or None if this is the root of the tree.

root

Root node of the tree this node is a member of. (readonly)


GNUe Home

Public API

Developer's Corner