python类在另一个类中进行管理

时间:2018-08-04 11:02:00

标签: python function class

我有一个名为martCrawler的类,该类可以从其他文件中导入其他3个搜寻器。

但是,随着爬网程序导入的增加,代码变得太长。

是否有更好的做法来管理这样的代码?

预先感谢〜

from Scripts_mart.wat_ver2 import crawler_watsons
from Scripts_mart.cosmed_ver1 import crawler_cosmed
from Scripts_mart.pxmart_ver1 import crawler_pxmart

import datetime

class martCrawler():

    def __init__(self):
        self.wat = crawler_watsons()
        self.cos = crawler_cosmed()
        self.pxm = crawler_pxmart()

    def crawler_testing(self):
        result_pack = {}
        wat_result = self.wat.run_before_insert()
        cos_result = self.cos.run_before_insert()
        pxm_result = self.pxm.run_before_insert()

        result_pack['wat'] = wat_result
        result_pack['cos'] = cos_result
        result_pack['pxm'] = pxm_result

        return result_pack
...

2 个答案:

答案 0 :(得分:1)

那么为什么不从一开始就将所有搜寻器存储在字典中呢?
例如:

from Scripts_mart.wat_ver2 import crawler_watsons
from Scripts_mart.cosmed_ver1 import crawler_cosmed
from Scripts_mart.pxmart_ver1 import crawler_pxmart

class MartCrawler():

    def __init__(self, *args):        
        self.crawlers = {}
        for crawler in args:
            # use some introspection to get the name of the classes. 
            # The names should be "crawler_watsons", etc
            self.crawlers[crawler.__name__] = crawler() 


    def crawler_testing(self):
        result_pack = {}
        for name, crawler in self.crawlers.items():
            result_back[name] = crawler.run_before_insert()
        return result_back


martCrawler = MartCrawler(crawler_watsons, crawler_cosmed, crawler_pxmart)

请记住,字典中的名称将是导入类的实际名称,而不是“ wat”,“ pos”和“ pxm”。但是,如果这是一个问题,则可以使用一些字符串操作和/或正则表达式对其进行修复。

例如,您可以将crawler.__name__替换为crawler.__name__[8:11]

答案 1 :(得分:1)

只需将它们放入字典中即可

from Scripts_mart.wat_ver2 import crawler_watsons
from Scripts_mart.cosmed_ver1 import crawler_cosmed
from Scripts_mart.pxmart_ver1 import crawler_pxmart

import datetime

class martCrawler():

    def __init__(self):
        self.crawlers = {
            "wat": crawler_watsons(), 
            "cos": crawler_cosmed(),
            "pxm": crawler_pxmart()
        }

    def crawler_testing(self):
        result_pack = {}
        for key in self.crawlers:
            result_pack[key] = self.crawlers[key].run_before_insert()

        return result_pack