Commits

Santiago Payà Miralta committed d1c3425

Clean code and first NetworkX steps (can create graphs from db).

  • Participants
  • Parent commits 9734463

Comments (0)

Files changed (6)

File class_edge.py

 """
 
 class edge:
-    """Simple class with basic attributes."""
+    """
+    Simple class with basic attributes.
+    
+    The the edge itself, 'idnA' and 'idnB' should be from the same level, 
+    this is resolved at execution time from an intelligent interface.
+
+    The 'iddown' object should be from the 'downlevel' level,
+    this is resolved at execution time from an intelligent interface.
+    """
     def __init__(self, level, idnA, idnB, iddown, downlevel, idtype):
         self.id = None
         self.level = level
         self.idtype = idtype
     
     def store(self, db):
-        # TODO idnA and idnB should be from the same level. INTERFACE ??
-        # TODO iddown should be from downlevel level. INTERFACE ??
+        """
+        Stores data in db. 
+
+        Param: db is the database object.
+        Return: the SQL statement and node id.
+        """
         if self.id:
             st = "UPDATE gn_" + str(self.level) + "_edges SET " \
                 "idnA = " + str(self.idnA) + ", " \
                 "idtype = " + str(self.idtype) + " " \
                 "WHERE id = " + str(self.id) + ";"
             db.query(st)
-            return st
         else:
             st = "INSERT INTO gn_" + str(self.level) + "_edges(idna, idnb, iddown, downlevel, idtype) " \
                 "VALUES(" \
                 + str(self.idtype) + ") " \
                 "RETURNING id;"
             self.id = db.query(st).getresult()[0][0]
-            return self.id
+        return st, self.id
         

File class_node.py

 """
 
 class node:
-    """Simple class with basic attributes."""
+    """
+    Simple class with basic attributes.
+
+    The 'iddown' object should be from the 'downlevel' level,
+    this is resolved at execution time from an intelligent interface.
+    """
     def __init__(self, level, iddown, downlevel, idtype):
         self.id = None
         self.level = level
         self.idtype = idtype
     
     def store(self, db):
-        # TODO iddown should be from downlevel level. INTERFACE ??
+        """
+        Stores data in db. 
+
+        Param: db is the database object.
+        Return: the SQL statement and node id.
+        """
         if self.id:
             st = "UPDATE gn_" + str(self.level) + "_nodes SET " \
                 "iddown = " + str(self.iddown) + ", " \
                 "idtype = " + str(self.idtype) + " " \
                 "WHERE id = " + str(self.id) + ";"
             db.query(st)
-            return st
         else:
             st = "INSERT INTO gn_" + str(self.level) + "_nodes(iddown, downlevel, idtype) " \
                 "VALUES(" \
                 + str(self.idtype) + ") " \
                 "RETURNING id;"
             self.id = db.query(st).getresult()[0][0]
-            return self.id
+        return st, self.id

File db_schema.py

       NO MAXVALUE 
       NO CYCLE;
     """
-    print st
-    db.query(st)
+    return st, db.query(st)
     
     ##
     # Out from basic schema all tables are referenced by this ones.
         "level" varchar(32),
         "tablename" varchar(63), -- pg NAMEDATALEN is 63 currently.
         "description" varchar(126)
-    ) WITH OIDS;
+    ) WITH (OIDS = FALSE);
     ALTER TABLE "gn_xx_node_types" 
         ADD CONSTRAINT "gn_xx_node_types_id" 
         PRIMARY KEY("id");
         "level" varchar(32),
         "tablename" varchar(63), -- pg NAMEDATALEN is 63 currently.
         "description" varchar(126)
-    ) WITH OIDS;
+    ) WITH (OIDS = FALSE);
     ALTER TABLE "gn_xx_edge_types" 
         ADD CONSTRAINT "gn_xx_edge_types_id" 
         PRIMARY KEY("id");
     ALTER SEQUENCE "gn_xx_edge_types_seq" OWNED BY gn_xx_edge_types.id;
     """
-    print st
-    db.query(st)
+    return st, db.query(st)
 
 def createLevelTables(level):
     """
         "iddown" integer,
         "downlevel" varchar(32),
         "idtype" integer
-    ) WITH OIDS;
+    ) WITH (OIDS = FALSE);
     ALTER TABLE "gn_""" + level + """_nodes" 
         ADD CONSTRAINT "gn_""" + level + """_nodes_id" 
         PRIMARY KEY("id");
         "iddown" integer,
         "downlevel" varchar(32),
         "idtype" integer
-    ) WITH OIDS;
+    ) WITH (OIDS = FALSE);
     ALTER TABLE "gn_""" + level + """_edges" 
         ADD CONSTRAINT "gn_""" + level + """_edges_id" 
         PRIMARY KEY("id");
         REFERENCES "gn_xx_edge_types"("id") 
         ON UPDATE RESTRICT ON DELETE RESTRICT;
     """
-    print st
-    db.query(st)
+    return st, db.query(st)
 
 def dropLevelTables(level):
     """
     DROP TABLE IF EXISTS "gn_""" + level + """_nodes" CASCADE;
     DROP TABLE IF EXISTS "gn_""" + level + """_edges" CASCADE;
     """
-    print st
-    db.query(st)
+    return st, db.query(st)
 
 ################################################################################
 # Data procedures.
         "VALUES ('" \
         + level + "', '" \
         + tablename + "', '" \
-        + description + "');"
-    print st
-    db.query(st)
+        + description + "') " \
+        "RETURNING id;"
+    return st, db.query(st).getresult()[0][0]
 
 def insertEdgeType(level, tablename, description):
     st = "INSERT INTO gn_xx_edge_types(level, tablename, description) " \
         "VALUES ('" \
         + level + "', '" \
         + tablename + "', '" \
-        + description + "');"
-    print st
-    db.query(st)
+        + description + "') " \
+        "RETURNING id;"
+    return st, db.query(st).getresult()[0][0]
 
 import db_connection
 import db_schema
 
+GN_DEBUG = True
+
 ##
 # *** DANGER ***
 # startSchema() deletes db schema.
 #
 #db_schema.startSchema()
-
-

File test_data.py

 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ################################################################################
 
+from class_edge import edge
+from class_node import node
 from db_connection import db
 from db_schema import *
-from class_edge import edge
-from class_node import node
 
-##
+################################################################################
+# Insert tables.
+# 
+print insertNodeType('00', 'vaults', 'Vaults and manholes.')
+print insertNodeType('00', 'poles', 'Poles and towers.')
+print insertNodeType('01', 'closures', 'Splicing boxes.')
+print insertNodeType('02', 'splices', 'Splices.')
+
+print insertEdgeType('00', 'trails', 'Trenchs.')
+print insertEdgeType('01', 'cables', 'Cables.')
+print insertEdgeType('02', 'fibers', 'Fibers.')
+
+print dropLevelTables('00')
+print createLevelTables('00')
+
+print dropLevelTables('01')
+print createLevelTables('01')
+
+print dropLevelTables('02')
+print createLevelTables('02')
+
+print dropLevelTables('LL')
+print createLevelTables('LL')
+
+################################################################################
 # Insert data.
-# 
-
-insertNodeType('00', 'vaults', 'Vaults and manholes.')
-insertNodeType('00', 'poles', 'Poles and towers.')
-insertNodeType('01', 'closures', 'Splicing boxes.')
-insertNodeType('02', 'splices', 'Splices.')
-
-insertEdgeType('00', 'trails', 'Trenchs.')
-insertEdgeType('01', 'cables', 'Cables.')
-insertEdgeType('02', 'fibers', 'Fibers.')
-
-createLevelTables('00')
-createLevelTables('01')
-createLevelTables('02')
-createLevelTables('LL')
-
+#
 N001 = node('00', 0, 0, 1)
 print N001.store(db)
+
 N002 = node('00', 0, 0, 1)
 print N002.store(db)
+
 N003 = node('00', 0, 0, 2)
 print N003.store(db)
-
 N003.idtype = 1
-print N003.id
 print N003.store(db)
 
-E001 = edge('00', 100000000, 100000001, 0, 0, 2)
+E001 = edge('00', N001.id, N002.id, 0, 0, 2)
 print E001.store(db)
 
-#print db.query("SELECT * FROM nodes")
+for r in db.query("SELECT * FROM gn_00_nodes").dictresult():
+    print 'id: %(id)s - iddown: %(iddown)s - idtype: %(idtype)s' % r
 
-#print
-#for r in db.query("SELECT * FROM nodes").dictresult():
-#    print 'idn: %(idn)s - iddown: %(iddown)s - idtype: %(idtype)s' % r
+for r in db.query("SELECT * FROM gn_00_edges").dictresult():
+    print 'id: %(id)s - idnA: %(idna)s - idnB: %(idnb)s - iddown: %(iddown)s - idtype: %(idtype)s' % r

File test_networkx.py

 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ################################################################################
 
-import genet
 import pylab as P
 import networkx as NX
+from test_data import *
 
 ################################################################################
-# Level 0
+# Level 00
 #
-G0 = NX.XGraph(name='Geometric', multiedges=True, selfloops=True)
+G00 = NX.XGraph(name='Level 00', multiedges=True, selfloops=True)
 
-for e in genet.EL00:
-    G0.add_edges_from([(e.idnA.id, e.idnB.id, e.id)])
+for r in db.query("SELECT * FROM gn_00_edges").dictresult():
+    G00.add_edges_from([(r['idna'], r['idnb'], r['id'])])
+    
+# May be there are isolated nodes.
+for r in db.query("SELECT * FROM gn_00_nodes").dictresult():
+    G00.add_node(r['id'])
 
-# May be there are isolated nodes.
-for n in genet.NL00:
-    print n.id
-    G0.add_node(n.id)
+print 'G00 nodes:', G00.nodes()
+print 'G00 edges:', G00.edges()
 
-print 'G0 nodes:', G0.nodes()
-print 'G0 edges:', G0.edges()
-        
-pos = NX.spring_layout(G0)
-NX.draw_networkx(G0, pos)
+pos = NX.spring_layout(G00)
+NX.draw_networkx(G00, pos)
 P.show()
 
 P.savefig("genet_L0.png")