I’m making a python script to pull data from an sqlite db for a network path.
I’ve got two classes: node, link:
class node:
nodeName = str()
nodeType = str()
node1 = str()
node2 = str()
active = 0
def __init__(self, nodeID):
self.nodeID = nodeID
cursor = network.execute("SELECT linkID FROM link WHERE nodeA = ? OR nodeB = ?", (nodeID, nodeID,))
row1 = cursor.fetchone()
row2 = cursor.fetchone()
if row1:
self.link1 = link(row1[0])
if row2:
self.link2 = link(row2[0])
self.nodeName, selfsnodeType, self.active = network.execute("SELECT nodeName, nodeType, active FROM node WHERE nodeID = ?", (nodeID,)).fetchall()[0]
Link is exactly the same, except it tries to get two node children. As you can see, this creates a circular reference, but it seems that three levels deep, the link children do not get node children of their own, and the node children do not get link children. Trying to read the values returns None type:
NODE object 67 getting link children:
(19,)
(48,)
link object 19 getting node childs:
(67,)
(112,)
NODE object 67 getting link children:
None
None
- Is this behavior by design?
- Is this a bad idea?
- Is is possible to iterate through the children?
Yes, this is bad design.
You only need to store edges to build a graph. Accordingly, you should build the graph by pulling edges from your database.
Afterwards, or simultaneously, you should pull the information you need to decorate the nodes (and edges if you decorate your edges). This should not trigger any attempt to pull information about edges (other than decorative information) from the database.