4.13. foundations.nodes

nodes.py

Platform:
Windows, Linux, Mac Os X.
Description:
Defines various nodes and dag related classes.
Others:
Portions of the code from DAG by Simon Wittber: http://pypi.python.org/pypi/DAG/ and PyQt4 Model View Programming Tutorials by Yasin Uludag: http://www.yasinuludag.com/blog/?p=98

4.13.1. Module Attributes

foundations.nodes.LOGGER

4.13.2. Classes

class foundations.nodes.Attribute(name=None, value=None, **kwargs)[source]

Bases: foundations.dataStructures.Structure

Defines a storage object for the AbstractNode class attributes.

Initializes the class.

Usage:

>>> attribute = Attribute(name="My Attribute", value="My Value")
>>> attribute.name
u'My Attribute'
>>> attribute["name"]
u'My Attribute'
>>> attribute.value
u'My Value'
>>> attribute["value"]
u'My Value'
Parameters:
  • name (unicode) – Attribute name.
  • value (object) – Attribute value.
  • **kwargs (**) – Keywords arguments.
name[source]

Property for self.__name attribute.

Returns:Value.
Return type:unicode
value[source]

Property for self.__value attribute.

Returns:Value.
Return type:object
class foundations.nodes.AbstractNode(name=None, **kwargs)[source]

Bases: foundations.dataStructures.Structure

Defines the base Node class.
Although it can be instancied directly that class is meant to be subclassed.
Note:Doesn’t provide compositing capabilities, AbstractCompositeNode class must be used for that purpose.

Initializes the class.

Usage:

>>> nodeA = AbstractNode("MyNodeA")
>>> nodeA.identity
1
>>> nodeB = AbstractNode()
>>> nodeB.name
u'Abstract2'
>>> nodeB.identity
2
Parameters:
  • name (unicode) – Node name.
  • **kwargs (**) – Keywords arguments.
family[source]

Property for self.__family attribute.

Returns:self.__family.
Return type:unicode
nodesInstances[source]

Property for self.__nodesInstances attribute.

Returns:self.__nodesInstances.
Return type:WeakValueDictionary
identity[source]

Property for self.__identity attribute.

Returns:self.__identity.
Return type:unicode
name[source]

Property for self.__name attribute.

Returns:self.__name.
Return type:unicode
getNodeByIdentity(identity)[source]

Returns the Node with given identity.

Usage:

>>> nodeA = AbstractNode("MyNodeA")
>>> AbstractNode.getNodeByIdentity(1)
<AbstractNode object at 0x101043a80>
Parameters:identity (int) – Node identity.
Returns:Node.
Return type:AbstractNode
Note:Nodes identities are starting from ‘1’ to nodes instances count.
listAttributes()[source]

Returns the Node attributes names.

Usage:

>>>     nodeA = AbstractNode("MyNodeA", attributeA=Attribute(), attributeB=Attribute())
>>> nodeA.listAttributes()
['attributeB', 'attributeA']
Returns:Attributes names.
Return type:list
getAttributes()[source]

Returns the Node attributes.

Usage:

>>>     nodeA = AbstractNode("MyNodeA", attributeA=Attribute(value="A"), attributeB=Attribute(value="B"))
>>> nodeA.getAttributes()
[<Attribute object at 0x7fa471d3b5e0>, <Attribute object at 0x101e6c4a0>]
Returns:Attributes.
Return type:list
attributeExists(name)[source]

Returns if given attribute exists in the node.

Usage:

>>>     nodeA = AbstractNode("MyNodeA", attributeA=Attribute(), attributeB=Attribute())
>>> nodeA.attributeExists("attributeA")
True
>>> nodeA.attributeExists("attributeC")
False
Parameters:name (unicode) – Attribute name.
Returns:Attribute exists.
Return type:bool
addAttribute(name, value)[source]

Adds given attribute to the node.

Usage:

>>>     nodeA = AbstractNode()
>>> nodeA.addAttribute("attributeA", Attribute())
True
>>> nodeA.listAttributes()
[u'attributeA']
Parameters:
  • name (unicode) – Attribute name.
  • value (Attribute) – Attribute value.
Returns:

Method success.

Return type:

bool

removeAttribute(name)[source]

Removes given attribute from the node.

Usage:

>>>     nodeA = AbstractNode("MyNodeA", attributeA=Attribute(), attributeB=Attribute())
>>> nodeA.removeAttribute("attributeA")
True
>>> nodeA.listAttributes()
['attributeB']
Parameters:name (unicode) – Attribute name.
Returns:Method success.
Return type:bool
class foundations.nodes.AbstractCompositeNode(name=None, parent=None, children=None, **kwargs)[source]

Bases: foundations.nodes.AbstractNode

Defines the base composite Node class.
It provides compositing capabilities allowing the assembly of graphs and various trees structures.

Initializes the class.

Parameters:
  • name (unicode) – Node name.
  • parent (AbstractNode or AbstractCompositeNode) – Node parent.
  • children (list) – Children.
  • **kwargs (**) – Keywords arguments.
Note:

pickle.HIGHEST_PROTOCOL must be used to pickle foundations.nodes.AbstractCompositeNode class.

parent[source]

Property for self.__parent attribute.

Returns:self.__parent.
Return type:AbstractNode or AbstractCompositeNode
children[source]

Property for self.__children attribute.

Returns:self.__children.
Return type:list
child(index)[source]

Returns the child associated with given index.

Usage:

>>> nodeB = AbstractCompositeNode("MyNodeB")
>>> nodeC = AbstractCompositeNode("MyNodeC")
>>> nodeA = AbstractCompositeNode("MyNodeA", children=[nodeB, nodeC])
>>> nodeA.child(0)
<AbstractCompositeNode object at 0x10107b6f0>
>>> nodeA.child(0).name
u'MyNodeB'
Parameters:index (int) – Child index.
Returns:Child node.
Return type:AbstractNode or AbstractCompositeNode or Object
indexOf(child)[source]

Returns the given child index.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeA.indexOf(nodeB)
0
>>> nodeA.indexOf(nodeC)
1
Parameters:child (AbstractNode or AbstractCompositeNode or Object) – Child node.
Returns:Child index.
Return type:int
row()[source]

Returns the Node row.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeB.row()
0
>>> nodeC.row()
1       
Returns:Node row.
Return type:int
addChild(child)[source]

Adds given child to the node.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB")
>>> nodeA.addChild(nodeB)
True
>>> nodeA.children
[<AbstractCompositeNode object at 0x10107afe0>]
Parameters:child (AbstractNode or AbstractCompositeNode or Object) – Child node.
Returns:Method success.
Return type:bool
removeChild(index)[source]

Removes child at given index from the Node children.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeA.removeChild(1)
True
>>> [child.name for child in nodeA.children]
[u'MyNodeB']
Parameters:index (int) – Node index.
Returns:Removed child.
Return type:AbstractNode or AbstractCompositeNode or Object
insertChild(child, index)[source]

Inserts given child at given index.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeD = AbstractCompositeNode("MyNodeD")
>>> nodeA.insertChild(nodeD, 1)
True
>>> [child.name for child in nodeA.children]
[u'MyNodeB', u'MyNodeD', u'MyNodeC']
Parameters:
  • child (AbstractNode or AbstractCompositeNode or Object) – Child node.
  • index (int) – Insertion index.
Returns:

Method success.

Return type:

bool

hasChildren()[source]

Returns if the Node has children.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeA.hasChildren()
False
Returns:Children count.
Return type:int
childrenCount()[source]

Returns the children count.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeA.childrenCount()
2
Returns:Children count.
Return type:int
sortChildren(attribute=None, reverseOrder=False)[source]

Sorts the children using either the given attribute or the Node name.

Parameters:
  • attribute (unicode) – Attribute name used for sorting.
  • reverseOrder (bool) – Sort in reverse order.
Returns:

Method success.

Return type:

bool

findChildren(pattern=u'.*', flags=0, candidates=None)[source]

Finds the children matching the given patten.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> nodeA.findChildren("c", re.IGNORECASE)
[<AbstractCompositeNode object at 0x101078040>]
Parameters:
  • pattern (unicode) – Matching pattern.
  • flags (int) – Matching regex flags.
  • candidates (list) – Matching candidates.
Returns:

Matching children.

Return type:

list

findFamily(pattern=u'.*', flags=0, node=None)[source]

Returns the Nodes from given family.

Parameters:
  • pattern (unicode) – Matching pattern.
  • flags (int) – Matching regex flags.
  • node (AbstractNode or AbstractCompositeNode or Object) – Node to start walking from.
Returns:

Family nodes.

Return type:

list

listNode(tabLevel=-1)[source]

Lists the current Node and its children.

Usage:

>>> nodeA = AbstractCompositeNode("MyNodeA")
>>> nodeB = AbstractCompositeNode("MyNodeB", nodeA)
>>> nodeC = AbstractCompositeNode("MyNodeC", nodeA)
>>> print nodeA.listNode()
|----'MyNodeA'
                |----'MyNodeB'
                |----'MyNodeC'
Parameters:tabLevel (int) – Tab level.
Returns:Node listing.
Return type:unicode