2

I'm writing a script that can help me documenting our network rooms.

The idea behind that script is that a room is a list, that contains several lists for the racks. The rack lists contain lists called module with the servers/switches/etc. in the module list are the actual ports with the cable numbers.

For example:

[04/02, [MM02, [1, #1992, 2, #1993, 3, #1567 ....], MM03, [1, #1234 .....]], 04/03, [MM01, .........]]]

04/02 = First Rack

MM02 = First module in that rack

1 = Port number

#1992 = Cable number

I hope you get the idea.

The script I wrote compares the cable numbers in the room list, and looks if there are duplicates. Now it gets tricky: It should then replace the cable number with the rack and module of the other port. That should be pretty easy, because module and rack are the first elements of those lists that contain the port, but I don't know how to get access to the information. (I'm a noob in programming)

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 5
    You would be better off using a dictionary instead of list. That would make data access and manipulation easier – DhruvPathak Apr 20 '12 at 12:46
  • What you ask could be done, but the best solution would be using a different data structure. – Rik Poggi Apr 20 '12 at 12:57
  • It seems to me that this would be much easier to implement as a dictionary where the keys are cable numbers and the other information is stored as a dictionary or custom class. Then the access would be something like `room[cable_number]["rack"]` or `room[cable_number]["module"]`... Of course, you'd probably need keys "rack1" and "rack2" in the interior dictionary since the cable has 2 ends... – mgilson Apr 20 '12 at 12:58
  • 1
    Did you think about storing the data in a database? – Andreas Florath Apr 20 '12 at 13:17

2 Answers2

1

As mentioned in the commends, the much better data structure to use here is nested dicts:

data = {
    "04/02": {
        "MM02": {1: "#1992", 2: "#1993", 3: "#1567", ...},
        "MM03": {1: "#1234", ...},
        ... 
    },
    "04/03": {
        "MM01": ...
        ...
    },
    ...
}

Then you simply do data["04/02"]["MM02"] = {1: "#1992", 2: "#1993", 3: "#1567", ...} to replace values, however, this has the disadvantage of meaning you need to manually create the sub-dictionaries - there are, however, solutions to this problem e.g:

from functools import partial
from collections import defaultdict

tripledict = partial(defaultdict, partial(defaultdict, dict))
mydict = tripledict()
mydict['foo']['bar']['foobar'] = 25

These not only have advantages in readability and usability, but also speed of access.

Community
  • 1
  • 1
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
0

Here's the Python class you're looking for. It's pretty straightforward, so if you are the noob you say you are and you want to learn: read and understand the code.
A few example rows are given at the bottom to show functionality. For multiple racks, just create a list of Rack(). Good luck.

class Rack():
    def __init__(self, name):
        self.name = name
        self.modules = dict() 

    # port_cable_list should be in the form:
    # [(1, #1992), (2, #1993), (3, #1567)] 
    def add_module(self, name, port_cable_list):
        self.modules[name] = dict()
        for port, cable in port_cable_list:
            self.modules[name][port] = cable

    def remove_module(self, name):
        if name in self.modules:
            del self.modules[name]

    def add_port(self, module_name, port, cable):
        if module_name not in self.modules:
            self.modules[module_name][port] = cable
            return True
        return False

    def remove_port(self, module_name, port):
        if module_name in self.modules:
            if port in self.modules[module_name]:
                del self.modules[module_name][port]
                return True
            else:
                return False
        return False

    def module_exists(self, module_name):
        return module_name in self.modules

    def port_exists_in_module(self, module_name, port):
        if self.modules[module_name]:
            return port in self.modules[module_name]
        return False

    def print_module(self, module_name):
        if self.module_exists(module_name):
            print "%s\nPort\tCable" % (module_name)
            for port, cable in self.modules[module_name].items():
                print port, "\t", cable
            print
            return True
        return False

    def print_rack(self):
        print self.name + ':'
        for module_name in self.modules.keys():
            self.print_module(module_name)

SomeRack = Rack('04/02')
SomeRack.add_module("MM02", [(1, '#1992'), (2, '#1993'), (3, '#1567')])
SomeRack.add_module("MM03", [(1, '#1234')])
SomeRack.print_module("MM03")
SomeRack.print_rack()
SomeRack.remove_module("MM03")
print SomeRack.module_exists("MM03")
SomeRack.print_rack()
gustafbstrom
  • 1,622
  • 4
  • 25
  • 44