4.12. foundations.nodes

nodes.py

Platform:
Windows, Linux, Mac Os X.
Description:
This module 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.12.1. Module Attributes

foundations.nodes.LOGGER

4.12.2. Classes

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

Bases: foundations.dataStructures.Structure

This class represents a storage object for the AbstractNode class attributes.

Usage:

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

This method is the property for self.__name attribute.

Returns:Value. ( String )
value[source]

This method is the property for self.__value attribute.

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

Bases: foundations.dataStructures.Structure

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

Usage:

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

This method is the property for self.__family attribute.

Returns:self.__family. ( String )
nodesInstances[source]

This method is the property for self.__nodesInstances attribute.

Returns:self.__nodesInstances. ( WeakValueDictionary )
identity[source]

This method is the property for self.__identity attribute.

Returns:self.__identity. ( String )
name[source]

This method is the property for self.__name attribute.

Returns:self.__name. ( String )
classmethod getNodeByIdentity(identity)[source]

This method returns the Node with given identity.

Usage:

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

This method returns the Node attributes names.

Usage:

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

This method returns the Node attributes.

Usage:

>>>     nodeA = AbstractNode("MyNodeA", attributeA=Attribute(value="A"), attributeB=Attribute(value="B"))
>>> nodeA.getAttributes()
[{'value': 'B'}, {'value': 'A'}]
Returns:Attributes. ( List )
attributeExists(name)[source]

This method 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 – Attribute name. ( String )
Returns:Attribute exists. ( Boolean )
addAttribute(*args, **kwargs)[source]

This decorator is used for exceptions handling.

Parameters:
  • *args – Arguments. ( * )
  • **kwargs – Keywords arguments. ( ** )
removeAttribute(*args, **kwargs)[source]

This decorator is used for exceptions handling.

Parameters:
  • *args – Arguments. ( * )
  • **kwargs – Keywords arguments. ( ** )
class foundations.nodes.AbstractCompositeNode(name=None, parent=None, children=None, **kwargs)[source]

Bases: foundations.nodes.AbstractNode

This class defines the base composite Node class.
It provides compositing capabilities allowing the assembly of graphs and various trees structures.
Parameters:
  • name – Node name. ( String )
  • parent – Node parent. ( AbstractNode / AbstractCompositeNode )
  • children – Children. ( List )
  • **kwargs – Keywords arguments. ( ** )
Note :

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

parent[source]

This method is the property for self.__parent attribute.

Returns:self.__parent. ( AbstractNode / AbstractCompositeNode )
children[source]

This method is the property for self.__children attribute.

Returns:self.__children. ( List )
child(index)[source]

This method 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
'MyNodeB'
Parameters:index – Child index. ( Integer )
Returns:Child node. ( AbstractNode / AbstractCompositeNode / Object )
indexOf(child)[source]

This method 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 – Child node. ( AbstractNode / AbstractCompositeNode / Object )
Returns:Child index. ( Integer )
row()[source]

This method 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. ( Integer )
addChild(child)[source]

This method 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 – Child node. ( AbstractNode / AbstractCompositeNode / Object )
Returns:Method success. ( Boolean )
removeChild(index)[source]

This method 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]
['MyNodeB']
Parameters:index – Node index. ( Integer )
Returns:Removed child. ( AbstractNode / AbstractCompositeNode / Object )
insertChild(child, index)[source]

This method 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]
['MyNodeB', 'MyNodeD', 'MyNodeC']
Parameters:
  • child – Child node. ( AbstractNode / AbstractCompositeNode / Object )
  • index – Insertion index. ( Integer )
Returns:

Method success. ( Boolean )

hasChildren()[source]

This method returns if the Node has children.

Usage:

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

This method returns the children count.

Usage:

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

This method sorts the children using either the given attribute or the Node name.

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

Method success. ( Boolean )

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

This method 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 – Matching pattern. ( String )
  • flags – Matching regex flags. ( Integer )
  • candidates – Matching candidates. ( List )
Returns:

Matching children. ( List )

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

This method returns the Nodes from given family.

Parameters:
  • pattern – Matching pattern. ( String )
  • flags – Matching regex flags. ( Integer )
  • node – Node to start walking from. ( AbstractNode / AbstractCompositeNode / Object )
Returns:

Family nodes. ( List )

listNode(tabLevel=-1)[source]

This method 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 – Tab level. ( Integer )
Returns:Node listing. ( String )

Table Of Contents

Previous topic

4.11. foundations.namespace

Next topic

4.13. foundations.parsers

This Page