1.2.5. BagNodeΒΆ
Last page update: September 22, 2015
We discovered in the previous section that we can associate a set of attributes to each item, and we know that each item is a BagNode.
We remember you that a
BagNode
is a class composed by:
- a single label.
- A single value (or item).
- One or more attributes.
If you need to work with nodes, you may get them with the following methods:
getNode()
: return a node.getNodes()
: return a list of nodes.getNodeByAttr()
: return the node who has the passed couple “value-attribute”>>> mybag = Bag({'paper':1,'scissors':2}) >>> papernode = mybag.getNode('paper') >>> mybag.setItem('rock',3,color='grey') >>> rocknode=mybag.getNodeByAttr('color','grey') >>> nodes=mybag.getNodes()The
getNodes()
method implements the Bag’s property nodes:>>> mybag.getNodes() == mybag.nodes TrueIf you have a node instance you may use one of the following methods:
hasAttr()
: check if a node has the given pair label-value in its attributes’ dictionary.setAttr()
: receive one or more key-value couple, passed as a dict or as named parameters, and sets them as attributes of the node.getAttr()
: return the value of an attribute. You have to specify the attribute’s label. If it doesn’t exist then it returns a default value.delAttr()
: delete the attribute with the passed names.getLabel()
: return the node’s label.setLabel()
: sets the node’s label.getValue()
: return the node’s value.setValue()
: set the node’s value.>>> print papernode.hasAttr('color') False >>> papernode.setAttr(color='white') >>> print papernode.getAttr('color') white >>> papernode.replaceAttr(color='yellow') >>> papernode.delAttr('color') >>> papernode.setLabel('sheet') >>> print papernode.getLabel() sheet >>> papernode.setValue(8) >>> papernode.getValue() 8For a complete list of the
BagNode
methods, check the BagNode section.