python中包含列表的访问信息

2024-03-28 14:42:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

例如:

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

04/02=第一个机架

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

1=端口号

#1992=电缆编号

我希望你明白我的意思。你知道吗

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


Tags: 模块端口网络服务器脚本列表记录编号
2条回答

这是您要查找的Python类。这很简单,所以如果你是你所说的noob并且你想学习:阅读并理解代码。
下面给出了几个示例行来展示功能。对于多个机架,只需创建一个机架列表()。祝你好运。你知道吗

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()

如前所述,这里使用的更好的数据结构是嵌套的^{}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", ...}来替换值,但是,这有一个缺点,即您需要手动创建子字典-但是有solutions to this problem例如:

from functools import partial
from collections import defaultdict

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

这些不仅在可读性和可用性方面有优势,而且访问速度也很快。你知道吗

相关问题 更多 >