2

我正在编写一个脚本,可以帮助我记录我们的网络室。

该脚本背后的想法是房间是一个列表,其中包含多个机架列表。机架列表包含称为模块的列表,带有服务器/交换机/等。在模块列表中是带有电缆编号的实际端口。

例如:

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

04/02= 第一个机架

MM02= 该机架中的第一个模块

1= 端口号

#1992= 电缆编号

我希望你能明白。

我编写的脚本比较了房间列表中的电缆编号,并查看是否有重复。现在它变得棘手:然后它应该用另一个端口的机架和模块替换电缆编号。这应该很容易,因为模块和机架是包含端口的列表中的第一个元素,但我不知道如何访问这些信息。(我是编程菜鸟)

4

2 回答 2

1

正如推荐中提到的,这里使用的更好的数据结构是嵌套dict的 s

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

然后您只需data["04/02"]["MM02"] = {1: "#1992", 2: "#1993", 3: "#1567", ...}替换值,但是,这意味着您需要手动创建子字典的缺点 - 但是,有解决此问题的方法,例如:

from functools import partial
from collections import defaultdict

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

这些不仅在可读性和可用性方面具有优势,而且在访问速度方面也具有优势。

于 2012-04-20T12:58:11.117 回答
0

这是您正在寻找的 Python 类。这很简单,所以如果你是你所说的菜鸟并且你想学习:阅读并理解代码。
底部给出了一些示例行以显示功能。对于多个机架,只需创建一个 Rack() 列表。祝你好运。

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()
于 2012-04-20T14:14:13.640 回答