Package ClusterShell :: Module MsgTree :: Class MsgTree
[hide private]
[frames] | no frames]

Class MsgTree

source code


MsgTree maps key objects to multi-lines messages.

MsgTree is a mutable object. Keys are almost arbitrary values (must be hashable). Message lines are organized as a tree internally. MsgTree provides low memory consumption especially on a cluster when all nodes return similar messages. Also, the gathering of messages is done automatically.

Instance Methods [hide private]
 
__init__(self, mode=0)
MsgTree initializer
source code
 
clear(self)
Remove all items from the MsgTree.
source code
 
__len__(self)
Return the number of keys contained in the MsgTree.
source code
 
__getitem__(self, key)
Return the message of MsgTree with specified key.
source code
 
get(self, key, default=None)
Return the message for key if key is in the MsgTree, else default.
source code
 
add(self, key, msgline)
Add a message line associated with the given key to the MsgTree.
source code
 
_update_keys(self)
Update keys associated to tree elements (MODE_DEFER).
source code
 
keys(self)
Return an iterator over MsgTree's keys.
source code
 
__iter__(self)
Return an iterator over MsgTree's keys.
source code
 
messages(self, match=None)
Return an iterator over MsgTree's messages.
source code
 
items(self, match=None, mapper=None)
Return (key, message) for each key of the MsgTree.
source code
 
_depth(self)
Return the depth of the MsgTree, ie.
source code
 
walk(self, match=None, mapper=None)
Walk the tree.
source code
 
walk_trace(self, match=None, mapper=None)
Walk the tree in trace mode.
source code
 
remove(self, match=None)
Modify the tree by removing any matching key references from the messages tree.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, mode=0)
(Constructor)

source code 

MsgTree initializer

The `mode' parameter should be set to one of the following constant:

MODE_DEFER: all messages are processed immediately, saving memory from duplicate message lines, but keys are associated to tree elements usually later when tree is first "walked", saving useless state updates and CPU time. Once the tree is "walked" for the first time, its mode changes to MODE_SHIFT to keep track of further tree updates. This is the default mode.

MODE_SHIFT: all keys and messages are processed immediately, it is more CPU time consuming as MsgTree full state is updated at each add() call.

MODE_TRACE: all keys and messages and processed immediately, and keys are kept for each message element of the tree. The special method walk_trace() is then available to walk all elements of the tree.

Overrides: object.__init__

__getitem__(self, key)
(Indexing operator)

source code 

Return the message of MsgTree with specified key. Raises a KeyError if key is not in the MsgTree.

get(self, key, default=None)

source code 

Return the message for key if key is in the MsgTree, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

_depth(self)

source code 

Return the depth of the MsgTree, ie. the max number of lines per message. Added for debugging.

walk(self, match=None, mapper=None)

source code 

Walk the tree. Optionally filter keys on match parameter, and optionally map resulting keys with mapper function. Return an iterator over (message, keys) tuples for each different message in the tree.

walk_trace(self, match=None, mapper=None)

source code 

Walk the tree in trace mode. Optionally filter keys on match parameter, and optionally map resulting keys with mapper function. Return an iterator over 4-length tuples (msgline, keys, depth, num_children).

remove(self, match=None)

source code 

Modify the tree by removing any matching key references from the messages tree.

Example of use:

>>> msgtree.remove(lambda k: k > 3)