python network simulator

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.

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. :

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.

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!

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.

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...