7.4.5. Class NodeSet¶
Note on memory management: Set objects are automatically destroyed when they are no longer in use.
7.4.5.1. Obtaining a NodeSet¶
You can create a new, empty node set by calling the NodeSet constructor with an empty parameter list. Passing an existing NodeSet object to the constructor creates an independent copy of the set. Any iterable yielding Node objects can be passed to the constructor to populate the new set.
All methods of the RFG interface that return sets of nodes return values of class NodeSet.
Caution
Nodes from different graphs can be inserted into the same NodeSet instance. Exercise caution when distinguishing types and attribute descriptors when querying nodes from a mixed-graph node set. It is recommended to avoid using mixed-graph node sets.
7.4.5.2. Iterating the Nodes Contained in a Node Set¶
__iter__()
The __iter__ method returns a generator for all nodes contained in the node set. See the example code in Listing Iterating a set of nodes.
from bauhaus import rfg
#...fill some_node_set
for n in some_node_set:
... # perform your actions on the nodes
Caution
Avoid modifying the set while iterating over it, as this may result in unpredictable behavior. If modification is required during iteration, create a copy of the set and modify the copy while iterating over the original.
7.4.5.3. General Operations¶
__len__()
The __len__ method returns the size of the set. Typically, this is used by calling the len function on the set.
__str__()
The __str__ method returns a textual representation of the node set (for debugging purposes only). For user-facing output, use iteration and the attribute handling methods for individual nodes.
The __eq__ and __ne__ methods check for equality and inequality, respectively, between two node sets. Equality is defined by the contents of the sets; if two sets contain the same elements, they are equal. Equality does not imply that inserting an element into one set also alters the other.
Typically, both methods are called using the == and != operators. The operators
<= and >= check whether the left set is a subset or a superset of the right set,
respectively.
Note
These methods check whether the instance is a subset or a superset of other_set, respectively.
clear()
Removes all elements from the set.
7.4.5.4. Membership in the View¶
The __contains__ method checks whether the node passed as a parameter is contained in the set. It can also be used with the in operator.
The insert and add methods add a node to the set. The remove method removes a node from the set; it has no effect if the node is not contained in the set.
To add or remove all nodes of a set from a view, pass the set to the add or remove methods of the View class.
7.4.5.5. Set Operations¶
These operations return new sets containing the intersection, union, difference, or symmetric difference of two sets. The update method adds the nodes of a NodeSet or any iterable yielding Node objects to the instance and returns the instance.
The following operator versions are also available:
operator |
function |
operation |
|---|---|---|
s1 & s2 |
__and__ |
s1.intersection(s2) |
s1 | s2 |
__or__, __add__ |
s1.union(s2) |
s1 - s2 |
__sub__ |
s1.difference(s2) |
s1 ^ s2 |
__xor__ |
s1.symmetric_difference(s2) |
copy()
The copy method returns a copy of the instance.
pop()
The pop method removes and returns an arbitrary node from the set. It raises a KeyError if the set is empty.
7.4.5.6. Querying the Neighborhood of Nodes in the Set¶
These methods perform the operations described in Section Querying the Neighborhood of Nodes and Edges.
The incomings, outgoings, attached, and connectings methods return edge sets.
The successors, predecessors, and neighbors methods return node sets.
Note
Since edges may be present in one view but not in another, the view argument is crucial for determining neighborhood relations.
7.4.5.7. Filtering a Node Set¶
filter(node_matcher)
The filter method applies the node_matcher predicate to each node in the set and returns a new set containing all nodes that match the predicate.