943,708 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2716
  • Python RSS
May 22nd, 2008
0

python network simulator

Expand Post »
Hi all,

This is my first thread on Daniweb!. I was wondering if someone would help nudge me in the right direction on this one. I'm new to python, been using it for about 6 months or so trying to develop a network simulator for school. This is not an assignment for a class, there is no grade tied to it, it's a bit of research I'm involved in.

I was tasked with writing a network simulator that would eventually be used to simulate peer-to-peer networks for traffic research. Basically the script reads in a network topology file generated by BRITE (a network topology generator written in java/c++) and creates nodes and edges based on this information. The script does this just fine.

The next step sets up the routing table so all the nodes in the network can start talking to each other. Whenever I initialize my routing tables, I add routing table entries to the node's incoming routes list. The node then update's it's list as it would normally, using whatever metric to judge which routes to change. Because the list, the node's routing table, should be empty, it should add the contents of the incoming routes list to the node's routing table.

Each node has three lists of routing table entries: the one that represents it's actual routing table, the one that represents outgoing route changes, and the one that represents incoming route changes. Each routing table entry is a tuple representing where it came from, the route, the next_hop (via), and the cost. When I initialize the lists, though, they not only share the same values (all 3 lists per node), but every node that I initialize shares those values, so when I change one list it changes everywhere. This smacks of a shared memory address, but I fear I don't know enough python to figure out whats up. I'll post the code in the thread, as well as the graph input file.

If anyone could point me in the right direction, even insofar as a text to read to find the answer myself, I'd be very grateful.

Thanks for reading.

Here's the code!

Node.py
python Syntax (Toggle Plain Text)
  1. class node(dict):
  2.  
  3. """ Node class, contains edge list, node name, and other node info
  4.  
  5. """
  6.  
  7.  
  8.  
  9. def __init__(self, iD, xPos, yPos, InDeg, OutDeg, ASId, Type, EdgeList = None):
  10.  
  11. """ Initialize everything, likely read in from a file
  12.  
  13. """
  14.  
  15.  
  16.  
  17. self["nodeData"] = {
  18.  
  19. "id" : iD,
  20.  
  21. "xpos" : xPos,
  22.  
  23. "ypos" : yPos,
  24.  
  25. "inDeg" : InDeg,
  26.  
  27. "outDeg" : OutDeg,
  28.  
  29. "ASid" : ASId,
  30.  
  31. "type" : Type
  32.  
  33. }
  34.  
  35. if EdgeList is None:
  36.  
  37. self["edgeList"] = []
  38.  
  39. else:
  40.  
  41. self["edgeList"] = EdgeList
  42.  
  43. self["incRTE"] = None#RTable()
  44.  
  45. self["outRTE"] = None#RTable()
  46.  
  47. self["myRTable"] = None#RTable()
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. def tick(self, number = 1):
  56.  
  57. """ Either tick once, or tick number times
  58.  
  59. """
  60.  
  61. #thisTickDo = self.msgQ.pop(0)
  62.  
  63. #testext = thisTickDo.getMessage()
  64.  
  65. #print testext
  66.  
  67.  
  68.  
  69. #while number > 0:
  70.  
  71. #Do tick things here
  72.  
  73.  
  74.  
  75. # number = number - 1
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. def addEdge(self, target):
  84.  
  85. """ Appends the edge to the end of edgeList. This may be extended to order the edges
  86.  
  87. on insert.
  88.  
  89. """
  90.  
  91. li = target
  92.  
  93. self["edgeList"].append(li)
  94.  
  95.  
  96.  
  97. def initRTable(self):
  98.  
  99. if self["incRTE"] is None:
  100.  
  101. self["incRTE"] = RTable()
  102.  
  103. for curr in self["edgeList"]:
  104.  
  105. foo = rtEntry(int(curr["to"]), int(self["nodeData"]["id"]), int(curr["length"]), int(curr["from"]))
  106.  
  107. self["incRTE"].addRTE(foo)
  108.  
  109.  
  110.  
  111.  
  112.  
  113. def updateRTable(self):
  114.  
  115. if self["outRTE"] is None:
  116.  
  117. self["outRTE"] = RTable()
  118.  
  119. if self["incRTE"] is None:
  120.  
  121. self["incRTE"] = RTable()
  122.  
  123. for curr1 in self["incRTE"]:
  124.  
  125. if len(self["myRTable"]) is 0:
  126.  
  127. self["myRTable"].addRTE(curr1)
  128.  
  129. else:
  130.  
  131. for curr2 in self["myRTable"]:
  132.  
  133. if curr2[0] == curr1[0]:
  134.  
  135. if curr1.getCost() < curr2.getCost():
  136.  
  137. del curr2
  138.  
  139. self["myRTable"].addRTE(curr1)
  140.  
  141. self["incRTE"] = None
  142.  
  143. self["outRTE"] = None # Might be a problem later!!
  144.  
  145.  
  146.  
  147. def isNeighborRoute(self, targetRTE):
  148.  
  149. if targetRTE[1] == targetRTE[3]:
  150.  
  151. return True
  152.  
  153. else:
  154.  
  155. return False
  156.  
  157.  
  158.  
  159.  
  160.  
  161. class rtEntry(list):
  162.  
  163. def __init__(self, dest, origin, cost, via):
  164.  
  165. self.entry = [dest, origin, cost, via]
  166.  
  167.  
  168.  
  169. def getDest(self): return self.entry[0]
  170.  
  171. def getOrigin(self): return self.entry[1]
  172.  
  173. def getCost(self): return self.entry[2]
  174.  
  175. def getVia(self): return self.entry[3]
  176.  
  177. def __repr__(self):
  178.  
  179. foo = str(self.getDest())+", "+str(self.getOrigin())+", "+str(self.getCost())+", "+str(self.getVia())
  180.  
  181. return foo
  182.  
  183.  
  184.  
  185.  
  186.  
  187. class RTable(list):
  188.  
  189. internalRTable = None
  190.  
  191. def __init__(self, rtbl=None):
  192.  
  193. if self.internalRTable == None:
  194.  
  195. self.internalRTable = []
  196.  
  197. if rtbl is not None:
  198.  
  199. for rtentry in rtbl:
  200.  
  201. self.addRTE([rtentry])
  202.  
  203.  
  204.  
  205. def addRTE(self, rteToAdd):
  206.  
  207. if self.internalRTable is None:
  208.  
  209. self.internalRTable = []
  210.  
  211. self.internalRTable.append([rteToAdd])
  212.  
  213.  
  214.  
  215. def __repr__(self):
  216.  
  217. ret = ""
  218.  
  219. if self.internalRTable is None:
  220.  
  221. return ret
  222.  
  223. else:
  224.  
  225. for curr in self.internalRTable:
  226.  
  227. if curr is not None:
  228.  
  229. ret = ret + str(curr)+"\n"
  230.  
  231. return ret
  232.  
  233.  
  234.  
  235. def clearRTList(self):
  236.  
  237. self.internalRTable = None
  238.  
  239. #for rte in self.internalRTable:
  240.  
  241. #del rte
  242.  
  243.  
  244.  
  245.  
  246.  
  247. class edge(dict):
  248.  
  249. """An edge object. Each node has a set of edge objects.
  250.  
  251. Each graph has a set of all the edges that will supply
  252.  
  253. each node with edges to populate the edge lists over time
  254.  
  255. """
  256.  
  257. def __init__(self, edgeId, From, To, Length, Delay, Bandwidth, ASFrom, ASTo, Type):
  258.  
  259. self.edgeData = {
  260.  
  261. "edgeId" : edgeId,
  262.  
  263. "from" : From,
  264.  
  265. "to" : To,
  266.  
  267. "length" : Length,
  268.  
  269. "delay" : Delay,
  270.  
  271. "bandwidth" : Bandwidth,
  272.  
  273. "ASfrom" : ASFrom,
  274.  
  275. "ASto" : ASTo,
  276.  
  277. "type" : Type
  278.  
  279. }
  280.  
  281.  
  282.  
  283. self.load = 0
  284.  
  285.  
  286.  
  287.  
  288.  
  289. def __getitem__(self, key): return self.edgeData[key]
  290.  
  291.  
  292.  
  293. def __setitem__(self, key, item): self.edgeData[key] = item
  294.  
  295.  
  296.  
  297. def __repr__(self):
  298.  
  299. ret = ""
  300.  
  301. for k,v in self.edgeData.iteritems():
  302.  
  303. ret = ret + str(k) + ": " + str(v) + "; "
  304.  
  305. return ret
  306.  
  307.  
  308.  
  309. def addLoad(self, Load = 5):
  310.  
  311. """ If called without any parameters, adds 5 units to the edge's current load.
  312.  
  313. Otherwise, add Load units to the current load. If an add would increase
  314.  
  315. the current load over the edge's bandwidth, set the edge's current load to it's
  316.  
  317. max load (bandwidth). Then, return the value of the current load.
  318.  
  319. """
  320.  
  321.  
  322.  
  323. if ((self.load + Load) > self.edgeData["bandwidth"]):
  324.  
  325. self.load = self.edgeData["bandwidth"]
  326.  
  327. else:
  328.  
  329. self.load = self.load + Load
  330.  
  331. return self.load
  332.  
  333.  
  334.  
  335. def remLoad(self, Load = 5):
  336.  
  337. """ If called without any parameters, removes 5 units from the edge's
  338.  
  339. current load. Otherwise, remove Load units from the current load.
  340.  
  341. If a remove would make the current load zero or less, set the
  342.  
  343. edge's current load to zero. Then, return the value of the current load.
  344.  
  345. """
  346.  
  347.  
  348.  
  349. if ((self.load - Load) < 0):
  350.  
  351. self.load = 0
  352.  
  353. else:
  354.  
  355. self.load = self.load - Load
  356.  
  357. return self.load
  358.  
  359.  
  360.  
  361. def isFull(self):
  362.  
  363. if self.load == self.edgeData["bandwidth"]:
  364.  
  365. return True
  366.  
  367. else:
  368.  
  369. return False

And the second file, Network.py:
python Syntax (Toggle Plain Text)
  1. from Node import *
  2.  
  3.  
  4.  
  5. class Network:
  6.  
  7. """ This is a class that will hold a network's hardware description.
  8.  
  9. Eventually, many network objects will be able to be instantiated
  10.  
  11. and communicate with each other. These will be controlled in
  12.  
  13. another class, NetManager. The interaction between each network
  14.  
  15. will be handled by another configuration file suited for EGPs.
  16.  
  17. """
  18.  
  19. def __init__(self, filename = "", NodeList = [], ModelInfo = [""], EdgeList = []):
  20.  
  21. """ This function prepares the network. It reads all the
  22.  
  23. configuration info from the infile and initializes all the main
  24.  
  25. lists and node lists. The edge distribution will be taken care
  26.  
  27. of by the driver program, as this may vary from simulation to
  28.  
  29. simulation.
  30.  
  31. """
  32.  
  33. self.nodeList = NodeList
  34.  
  35. self.modelInfo = ModelInfo
  36.  
  37. self.sourcefile = filename
  38.  
  39. self.fullEdgeList = EdgeList
  40.  
  41. self.routingProtocol = ""
  42.  
  43.  
  44.  
  45. if self.sourcefile == "":
  46.  
  47. self.sourcefile = \
  48.  
  49. raw_input("Please enter the path and filename \
  50.  
  51. of the configuration file: ")
  52.  
  53.  
  54.  
  55. infile = open(self.sourcefile, 'r')
  56.  
  57.  
  58.  
  59. buffer = infile.readline()
  60.  
  61. helper = buffer.split(" ")
  62.  
  63.  
  64.  
  65. self.numNodes = int(helper[2])
  66.  
  67. self.numEdges = int(helper[4])
  68.  
  69. buffer = infile.readline()
  70.  
  71. self.modelInfo = buffer.split(" ") # No functionality yet
  72.  
  73.  
  74.  
  75. # Read in all the node info, a line at a time.
  76.  
  77.  
  78.  
  79. buffer = infile.readline() #Nodes:(blah)\n
  80.  
  81.  
  82.  
  83. i = 0
  84.  
  85. while i < self.numNodes:
  86.  
  87. buffer = infile.readline()
  88.  
  89. helper = buffer.split("\t")
  90.  
  91. self.nodeList.append(node(int(helper[0]), int(helper[1]), \
  92.  
  93. int(helper[2]), int(helper[3]), int(helper[4]), \
  94.  
  95. int(helper[5]), helper[6], []))
  96.  
  97. i+=1
  98.  
  99.  
  100.  
  101. buffer = infile.readline() #"\n"
  102.  
  103. buffer = infile.readline() #"\n"
  104.  
  105.  
  106.  
  107. buffer = infile.readline() #Edges:(blah)\n
  108.  
  109. #helper = buffer.split(" ")
  110.  
  111.  
  112.  
  113.  
  114.  
  115. i = 0
  116.  
  117. while i < self.numEdges:
  118. buffer = infile.readline()
  119.  
  120. helper = buffer.split("\t")
  121.  
  122. self.fullEdgeList.append(edge(int(helper[0]), int(helper[1]),\
  123.  
  124. int(helper[2]), float(helper[3]), float(helper[4]), \
  125.  
  126. float(helper[5]), int(helper[6]), int(helper[7]), \
  127.  
  128. helper[8]))
  129.  
  130. i+=1
  131.  
  132. infile.close()
  133.  
  134.  
  135.  
  136. # End __init__()
  137.  
  138.  
  139.  
  140. def setRoutingType(self, lsProto = "OSPF"):
  141.  
  142. if (lsProto == "OSPF"): # Add other protocols later
  143.  
  144. self.routingProtocol = "OSPF"
  145.  
  146. """ Here is where I would call an object seperate from the
  147.  
  148. Network object to preserve the
  149.  
  150. physical network configuration. This is an attempt to
  151.  
  152. isolate the hardware from the protocol.
  153.  
  154. # #############################################################
  155.  
  156. # #####################NOT FINISHED!!!!########################
  157.  
  158. """
  159.  
  160. pass
  161.  
  162.  
  163.  
  164. def addAllEdges(self):
  165.  
  166. """ addAllEdges() adds every edge in the edge list to the node
  167.  
  168. who's id is in it's from field
  169.  
  170. """
  171.  
  172. for edge in self.fullEdgeList:
  173.  
  174. for node in self.nodeList:
  175.  
  176. if node["nodeData"]["id"] == edge["from"]:
  177.  
  178. node.addEdge(edge)
  179.  
  180.  
  181.  
  182. def addNode(self, targetNode):
  183.  
  184. self.nodeList.append(targetNode)
  185.  
  186. self.numNodes = self.numNodes + 1
  187.  
  188.  
  189.  
  190. def initAllRTables(self):
  191.  
  192. for rtable in self.nodeList:
  193.  
  194. rtable.initRTable()
  195.  
  196. rtable.updateRTable()
  197.  
  198. #print rtable

And here's the .brite file, you don't have to save it as a .brite, but that's the program that generated it, BRITE:
Python Syntax (Toggle Plain Text)
  1. Topology: ( 50 Nodes, 100 Edges )
  2. Model (7 - Imported From BRITE format file /home/ich1/Desktop/BRITE/router50wax.brite ): Model (1 - RTWaxman): 50 1000 100 1 2 0.15000000596046448 0.20000000298023224 1 3 10.0 1024.0
  3. Nodes: ( 50 )
  4. 0 604 820 2 2 -1 RT_NODE
  5. 1 161 651 5 5 -1 RT_NODE
  6. 2 175 671 2 2 -1 RT_NODE
  7. 3 723 140 5 5 -1 RT_NODE
  8. 4 542 626 2 2 -1 RT_NODE
  9. 5 768 945 3 3 -1 RT_NODE
  10. 6 429 108 2 2 -1 RT_NODE
  11. 7 649 828 6 6 -1 RT_NODE
  12. 8 123 390 3 3 -1 RT_NODE
  13. 9 835 523 3 3 -1 RT_NODE
  14. 10 360 95 2 2 -1 RT_NODE
  15. 11 698 906 3 3 -1 RT_NODE
  16. 12 903 147 4 4 -1 RT_NODE
  17. 13 975 184 2 2 -1 RT_NODE
  18. 14 541 487 6 6 -1 RT_NODE
  19. 15 738 897 7 7 -1 RT_NODE
  20. 16 893 867 5 5 -1 RT_NODE
  21. 17 886 214 4 4 -1 RT_NODE
  22. 18 394 681 4 4 -1 RT_NODE
  23. 19 261 895 2 2 -1 RT_NODE
  24. 20 286 113 2 2 -1 RT_NODE
  25. 21 525 249 5 5 -1 RT_NODE
  26. 22 250 837 3 3 -1 RT_NODE
  27. 23 803 655 4 4 -1 RT_NODE
  28. 24 580 135 3 3 -1 RT_NODE
  29. 25 957 474 4 4 -1 RT_NODE
  30. 26 419 562 2 2 -1 RT_NODE
  31. 27 56 699 4 4 -1 RT_NODE
  32. 28 647 250 5 5 -1 RT_NODE
  33. 29 229 325 3 3 -1 RT_NODE
  34. 30 461 498 8 8 -1 RT_NODE
  35. 31 662 200 6 6 -1 RT_NODE
  36. 32 554 574 4 4 -1 RT_NODE
  37. 33 888 73 2 2 -1 RT_NODE
  38. 34 68 533 5 5 -1 RT_NODE
  39. 35 924 679 4 4 -1 RT_NODE
  40. 36 178 442 2 2 -1 RT_NODE
  41. 37 11 935 3 3 -1 RT_NODE
  42. 38 240 582 8 8 -1 RT_NODE
  43. 39 925 762 2 2 -1 RT_NODE
  44. 40 926 110 5 5 -1 RT_NODE
  45. 41 849 616 7 7 -1 RT_NODE
  46. 42 681 157 4 4 -1 RT_NODE
  47. 43 796 538 12 12 -1 RT_NODE
  48. 44 102 550 2 2 -1 RT_NODE
  49. 45 458 424 4 4 -1 RT_NODE
  50. 46 593 168 2 2 -1 RT_NODE
  51. 47 111 273 5 5 -1 RT_NODE
  52. 48 726 442 5 5 -1 RT_NODE
  53. 49 931 338 3 3 -1 RT_NODE
  54.  
  55.  
  56. Edges: ( 100 )
  57. 0 43 30 337.3796081542969 1.1253772373229511 10.0 -1 -1 E_RT U
  58. 1 43 15 363.6550598144531 1.2130227099123791 10.0 -1 -1 E_RT U
  59. 2 16 15 157.8765411376953 0.5266194559760917 10.0 -1 -1 E_RT U
  60. 3 16 43 343.00146484375 1.1441297327224622 10.0 -1 -1 E_RT U
  61. 4 31 43 363.5931701660156 1.2128162682665473 10.0 -1 -1 E_RT U
  62. 5 31 30 359.4509582519531 1.1989993365742146 10.0 -1 -1 E_RT U
  63. 6 48 43 118.81077575683594 0.3963100891511952 10.0 -1 -1 E_RT U
  64. 7 48 31 250.31979370117188 0.8349769549611947 10.0 -1 -1 E_RT U
  65. 8 32 48 216.8132781982422 0.7232112496914188 10.0 -1 -1 E_RT U
  66. 9 32 30 120.10411834716797 0.4006242156604486 10.0 -1 -1 E_RT U
  67. 10 34 31 680.9735717773438 2.271483333237635 10.0 -1 -1 E_RT U
  68. 11 34 32 487.7263488769531 1.6268799826743912 10.0 -1 -1 E_RT U
  69. 12 41 15 302.1291198730469 1.007794265034669 10.0 -1 -1 E_RT U
  70. 13 41 16 254.827392578125 0.8500126863702655 10.0 -1 -1 E_RT U
  71. 14 1 34 150.24313354492188 0.5011571490064699 10.0 -1 -1 E_RT U
  72. 15 1 30 336.7625427246094 1.1233189286056335 10.0 -1 -1 E_RT U
  73. 16 29 48 510.5859375 1.703131362630877 10.0 -1 -1 E_RT U
  74. 17 29 15 765.679443359375 2.5540317073599463 10.0 -1 -1 E_RT U
  75. 18 14 41 333.92364501953125 1.1138493851620885 10.0 -1 -1 E_RT U
  76. 19 14 32 87.96590423583984 0.2934226725471521 10.0 -1 -1 E_RT U
  77. 20 47 41 813.8138427734375 2.7145907812445285 10.0 -1 -1 E_RT U
  78. 21 47 30 416.0829162597656 1.3879032148959718 10.0 -1 -1 E_RT U
  79. 22 18 34 358.0223388671875 1.1942339752495958 10.0 -1 -1 E_RT U
  80. 23 18 29 392.3786315917969 1.3088342322200677 10.0 -1 -1 E_RT U
  81. 24 3 14 391.8328857421875 1.3070138200147368 10.0 -1 -1 E_RT U
  82. 25 3 43 404.63934326171875 1.3497315641666967 10.0 -1 -1 E_RT U
  83. 26 12 14 496.6326599121094 1.6565882384943433 10.0 -1 -1 E_RT U
  84. 27 12 41 472.0985107421875 1.5747511258011284 10.0 -1 -1 E_RT U
  85. 28 27 47 429.5357971191406 1.4327771952126316 10.0 -1 -1 E_RT U
  86. 29 27 15 710.1605224609375 2.3688405212012955 10.0 -1 -1 E_RT U
  87. 30 44 47 277.14617919921875 0.9244601450221231 10.0 -1 -1 E_RT U
  88. 31 44 14 443.4974670410156 1.4793483131620864 10.0 -1 -1 E_RT U
  89. 32 17 12 69.12307739257812 0.23056976767767162 10.0 -1 -1 E_RT U
  90. 33 17 43 336.26776123046875 1.1216685151914954 10.0 -1 -1 E_RT U
  91. 34 2 30 334.2528991699219 1.1149476587897413 10.0 -1 -1 E_RT U
  92. 35 2 27 122.24974060058594 0.40778124111643244 10.0 -1 -1 E_RT U
  93. 36 13 43 396.6824951171875 1.3231903756471002 10.0 -1 -1 E_RT U
  94. 37 13 48 358.55963134765625 1.196026190050639 10.0 -1 -1 E_RT U
  95. 38 28 3 133.70115661621094 0.4459790533363282 10.0 -1 -1 E_RT U
  96. 39 28 43 324.2607116699219 1.0816173089648635 10.0 -1 -1 E_RT U
  97. 40 38 34 178.843505859375 0.5965577221404783 10.0 -1 -1 E_RT U
  98. 41 38 47 334.8462219238281 1.1169267704654136 10.0 -1 -1 E_RT U
  99. 42 37 41 896.6632690429688 2.9909467203573503 10.0 -1 -1 E_RT U
  100. 43 37 27 240.251953125 0.8013942536372947 10.0 -1 -1 E_RT U
  101. 44 8 38 224.83995056152344 0.749985346734518 10.0 -1 -1 E_RT U
  102. 45 8 18 397.64556884765625 1.3264028438222293 10.0 -1 -1 E_RT U
  103. 46 23 17 448.74267578125 1.4968444462377035 10.0 -1 -1 E_RT U
  104. 47 23 1 642.012451171875 2.1415230238109424 10.0 -1 -1 E_RT U
  105. 48 40 3 205.20477294921875 0.6844894441914838 10.0 -1 -1 E_RT U
  106. 49 40 28 312.1553955078125 1.0412383206378477 10.0 -1 -1 E_RT U
  107. 50 35 23 123.35720825195312 0.4114753555673276 10.0 -1 -1 E_RT U
  108. 51 35 16 190.53871154785156 0.635568729176808 10.0 -1 -1 E_RT U
  109. 52 7 35 312.771484375 1.0432933718932982 10.0 -1 -1 E_RT U
  110. 53 7 14 357.6940002441406 1.1931387554924435 10.0 -1 -1 E_RT U
  111. 54 49 28 297.3213806152344 0.9917573730798603 10.0 -1 -1 E_RT U
  112. 55 49 16 530.3630981445312 1.769100869590693 10.0 -1 -1 E_RT U
  113. 56 22 17 890.294921875 2.9697042007474383 10.0 -1 -1 E_RT U
  114. 57 22 8 464.6912841796875 1.5500432775386481 10.0 -1 -1 E_RT U
  115. 58 9 12 382.0994567871094 1.2745465957889754 10.0 -1 -1 E_RT U
  116. 59 9 41 94.04785919189453 0.3137098905666751 10.0 -1 -1 E_RT U
  117. 60 21 22 649.12939453125 2.165262591533407 10.0 -1 -1 E_RT U
  118. 61 21 1 542.3098754882812 1.8089510293427102 10.0 -1 -1 E_RT U
  119. 62 33 40 53.037723541259766 0.17691480264410042 10.0 -1 -1 E_RT U
  120. 63 33 43 474.0137023925781 1.5811395175010645 10.0 -1 -1 E_RT U
  121. 64 6 38 510.2911071777344 1.702147914534042 10.0 -1 -1 E_RT U
  122. 65 6 7 752.8612060546875 2.5112746700742137 10.0 -1 -1 E_RT U
  123. 66 24 7 696.4265747070312 2.3230290026409914 10.0 -1 -1 E_RT U
  124. 67 24 40 346.9020080566406 1.1571405443983538 10.0 -1 -1 E_RT U
  125. 68 42 3 45.31004333496094 0.15113803608415305 10.0 -1 -1 E_RT U
  126. 69 42 1 717.241943359375 2.3924615987483415 10.0 -1 -1 E_RT U
  127. 70 4 38 305.1884765625 1.0179991804947275 10.0 -1 -1 E_RT U
  128. 71 4 21 377.3830871582031 1.2588144801101138 10.0 -1 -1 E_RT U
  129. 72 19 38 313.70367431640625 1.0464028228368782 10.0 -1 -1 E_RT U
  130. 73 19 43 643.1749267578125 2.1454006249810744 10.0 -1 -1 E_RT U
  131. 74 36 40 818.3690795898438 2.729785415715307 10.0 -1 -1 E_RT U
  132. 75 36 49 760.1480102539062 2.5355808325701985 10.0 -1 -1 E_RT U
  133. 76 26 42 482.3577575683594 1.6089722896509937 10.0 -1 -1 E_RT U
  134. 77 26 7 351.6475524902344 1.172969976750497 10.0 -1 -1 E_RT U
  135. 78 11 37 687.61181640625 2.2936261338710864 10.0 -1 -1 E_RT U
  136. 79 11 23 272.07720947265625 0.9075518820178466 10.0 -1 -1 E_RT U
  137. 80 39 9 255.38401794433594 0.8518693887367105 10.0 -1 -1 E_RT U
  138. 81 39 7 283.7816162109375 0.946593580452706 10.0 -1 -1 E_RT U
  139. 82 45 38 269.2359619140625 0.8980745003066838 10.0 -1 -1 E_RT U
  140. 83 45 24 313.6957092285156 1.0463762541635242 10.0 -1 -1 E_RT U
  141. 84 20 31 385.9339294433594 1.2873370198104161 10.0 -1 -1 E_RT U
  142. 85 20 21 274.9854431152344 0.9172527052539606 10.0 -1 -1 E_RT U
  143. 86 46 38 544.0634155273438 1.8148002093079465 10.0 -1 -1 E_RT U
  144. 87 46 31 76.05918884277344 0.253706145078451 10.0 -1 -1 E_RT U
  145. 88 25 28 382.46044921875 1.2757507369273113 10.0 -1 -1 E_RT U
  146. 89 25 45 501.4987487792969 1.6728197637957152 10.0 -1 -1 E_RT U
  147. 90 10 42 326.9327087402344 1.0905301318161726 10.0 -1 -1 E_RT U
  148. 91 10 45 343.28558349609375 1.1450774505344419 10.0 -1 -1 E_RT U
  149. 92 5 25 507.50567626953125 1.6928567171277247 10.0 -1 -1 E_RT U
  150. 93 5 35 308.3699035644531 1.028611278688182 10.0 -1 -1 E_RT U
  151. 94 0 18 251.83526611328125 0.840032026800625 10.0 -1 -1 E_RT U
  152. 95 0 43 341.1568603515625 1.1379767944381125 10.0 -1 -1 E_RT U
  153. 96 15 11 41.0 0.13676127903124233 10.0 -1 -1 E_RT U
  154. 97 15 25 476.3297119140625 1.588864893706107 10.0 -1 -1 E_RT U
  155. 98 30 5 542.2711791992188 1.8088219524162237 10.0 -1 -1 E_RT U
  156. 99 30 21 257.0933532714844 0.8575711176546155 10.0 -1 -1 E_RT U
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ich1 is offline Offline
7 posts
since May 2008
May 24th, 2008
0

Re: python network simulator

Nice code!

Line 19 in the second file is (one of?) your troubles:

def __init__(self, filename = "", NodeList = [], ModelInfo = [""], EdgeList = [])

This is tricky, and it gets even experienced Python users several times until they learn by banging their heads against walls.

Here's what happens:

On compilation, NodeList's default value is set to a reference (pointer) to an empty list []. Likewise, ModeInfo is set to a reference to [""], and EdgeList is set to a reference to [].

NOW.

Your code executes and creates a single Network object. It adds items to, say, NodeList. As a result, that empty list in memory changes. But the default reference doesn't change! ACK!! As a result, the NEXT Network object you create will have as its default value the list of all the items added to NodeList the last time.

It's the peril of mutable objects: changing the object "automagically" changes it for all shared references (which are shared memory locations, as you pointed out in your post).

So...

Two options.

One is to use immutable tuples as your default values. Then convert each to a list straight away in the __init__ code. :

Python Syntax (Toggle Plain Text)
  1. def __init__(self, filename = "", NodeList = (), ModelInfo = ("",), EdgeList = ()):
  2.  
  3. NodeList = list(NodeList)
  4. ModeInfo = list(ModeInfo)
  5. EdgeList = list(EdgeList)

I like this option.

OR, set the default values to be None. Then, check for None in the __init__ code and if None, then assign to an empty list. Because references within the __init__ are local scope, a new list will be created each time.

Python Syntax (Toggle Plain Text)
  1. def __init__(..., NodeList = None, ...):
  2.  
  3. if NodeList == None:
  4. NodeList = []

This is one of the (relatively few) annoying quirks of Python, and it is an unavoidable feature of its otherwise very useful distinction between mutable and immutable objects.

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
May 27th, 2008
0

Re: python network simulator

Thanks for the compliment!

Hrmmm, interesting, I think I understand. You're saying that instead of initializing the lists every time I call a Network object, python uses the list that already exists with the same name (lists being mutable), so I should pass the parameters as immutable objects because these are recreated every time I call a new Network object. I'll try you idea with the tuples, thanks a ton!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ich1 is offline Offline
7 posts
since May 2008
May 27th, 2008
0

Re: python network simulator

Another question, do you think inheriting object would make this any easier or more clear?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ich1 is offline Offline
7 posts
since May 2008
May 27th, 2008
0

Re: python network simulator

No, I think it's clear enough. class Network should inherit from object, though. Only old-style classes get to be parent-less.

class Network(object):
...

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Nov 13th, 2008
0

Re: python network simulator

When I was searching python network simulator I came across this thread. I found it very helpful for those who want to start writing network simulation in python. When you complete your simulator, it would be nice if you could put it on the web. If you do, could you post the link here. I have seen network simulators in C++, Java but not in python.
Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gaunle is offline Offline
1 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: help with lists and dictionaries
Next Thread in Python Forum Timeline: pywxrc.file , XCred.file , etc.. What & Why?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC