将字符串列表解析为相应的对象

Resolve list of strings as respective objects

我有一个包含 [bool, "string of function name", ["parameters","to","pass"]] 的列表,但是当我传递它们时,除了它们的字符串值外,它们什么都没有解析。实际项目已经有 900+400 行左右,为了简洁和理智,我在下面包含了重要的部分:

def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath('.'), relative_path)

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi(resource_path('./hc-gui.ui'), self)
        # a bunch of QPushButton connected functions from a loaded .ui file made in QT Designer
        global dxt
        dxt = pydxt.laid(pydxt.fetch()) 
        # pydxt is a module I wrote specifically for this workplace and project 
        # laid() returns [[name, ip, user, pass, 6-digit id, laidrange_start, laidrange_end, associated_letter]] as end result, and is used below
        # the dxt list serves as basis for all such basic information
        
    def runButton(self):
        global runlist
        runlist = []
        for i in range(self.listLaid.count()):
            item = self.listLaid.item(i)
            runlist.append(str(item.text()))
            # there is now a list of each LAID to process
        global checklist
        checklist = []
        togglebuttons = [
            [self.toggleTTRX, "checkTTRX", ["host","usr","passwd","tbs"]],
            [self.toggleMCCH, "checkMCCH", ["host","usr","passwd","laid"]],
            [self.toggleRL, "checkRL", ["host","usr","passwd","tbs","sitedown"]],
            [self.toggleGPS, "checkGPS", ["host","usr","passwd","tbs","sitedown"]]
            ]
        for i in range(len(togglebuttons)):
            checklist.append([togglebuttons[i][0].isChecked(), togglebuttons[i][1], togglebuttons[i][2]])
        self.runLongTask()
        
    def runLongTask(self):
        self.worker = Worker()
        self.thread = QThread()
        self.worker.moveToThread(self.thread)
        self.worker.finished.connect(self.thread.quit)
        self.worker.finished.connect(self.worker.deleteLater)
        self.worker.finished.connect(self.presentResults)
        self.thread.finished.connect(self.thread.deleteLater)
        QApplication.processEvents()
        self.thread.started.connect(lambda: self.worker.connectAndParse(runlist, checklist))
        self.thread.start()
        
class Worker(QObject):
    finished = pyqtSignal()
    logit = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        
    def connectAndParse(self, runlist, checklist):
        global results
        results = []
        for x in range(len(runlist)):
            laid = str(runlist[x])
            hostlist = pydxt.finder(dxt,int(laid)) # just finds correct dxt based on provided laid, returns correct sublist from dxt list
            tbs = str(int(laid)-int(hostlist[5])+1)
            host = hostlist[1]
            usr = hostlist[2]
            passwd = hostlist[3]
            global sitedown
            sitedown = False
            runtask = Worker()
            for y in range(len(checklist)):
                if bool(checklist[y][0]):
                    func = getattr(runtask, checklist[y][1])(*checklist[y][2]
                    results.append(func())
        self.finished.emit()

    def checkTTRX(self, host, usr, passwd, tbs):
        with telnetlib.Telnet(host=host,timeout=2) as tn:
            tn.write(usr.encode('ascii' + b"\r\n")
            tn.write(passwd.encode('ascii' + b"\r\n")
            #etc etc
            number = ""
            return ["number of ttrx on site", number]
    
    def checkMCCH(self, host, usr, passwd, laid):
        with telnetlib.Telnet(host=host,timeout=2) as tn:
            #does more stuff, checks if sitedown should remain as False or be set to True
            
    def checkRL(self, host, usr, passwd, tbs, sitedown):
        #same story here, just keeps going

我确定我错过了一些显而易见的事情,因为我仍在学习如何使用多个 classes。以前所有在现在分离的 check 函数中的东西都在一个 huuuge 块中并且必须处理,我想让所有东西都可选。

我得到的错误是 Telnet 无法解析主机名,因为 host 不会解析为 ip 地址,而只是一串 "host".

问题(如我所见)根源于我需要解决的参数列表,但我没有解决,而且我不知道如何解决。如何将要使用的参数列表从 Ui class 传递到 Worker class ,在那里它们可以解析为具有相同名称的变量?

在我个人看来,您应该为所有检查功能建立一个标准接口,并让它们都使用完全相同的签名。让他们忽略他们不想要的参数。这消除了你的大部分麻烦。