Obtaining a list of nodes for a network
Similarly to what explored for the links() method, the nodes() method allows to retrieve the nodes of the network in a CubeNetworkEntityResultSet. This is an iterable object where the cursor is set to a record initialized before the first record.
net_nodes = db.nodes(network_name)
The cursor next will return a Boolean, which is True if the next record exists and False otherwise, and will set the pointer to the next record. The node number (n) can be access directly using the n() function.
Therefore, a list of the nodes in the network can be
obtained as in the example below.
net_nodes_list = [] while net_nodes.next(): net_nodes_list.append(net_nodes.n())
Alternatively, a Lua expression can be used, to retrieve
the node numbers using the valueForNodes() method. An example is provided
below, where string concatenation operator is done in Lua with ".." (two dots):
lua_expr = """ if __entityIndex == 0 then __userDataString1 = tostring(node.n) else __userDataString1 = __userDataString1 .. ", " .. node.n end """ node_value = db.valueForNodes(network_name, lua_expr) # <class 'cubeapi.NodeValueEntryVector'> nodes_list = list(map(int, db.lastExpressionUserData().string1.split(",")))
This will return a Python list with the node numbers integer values.