Python 3:将变量从一个CLASS传递到另一个

时间:2019-06-01 04:49:12

标签: python-3.x oop instantiation

我在这里有一个初学者的问题。我有一个父类,其中包含对SQLite DB和ConfigParser ini文件的调用,我希望我的子类访问所有这些信息。

下面是我的代码,到目前为止我没有尝试过。我了解实例化,但仅使用最少的示例,例如雇员/薪水大声笑。

如果我将所有对DB和配置文件的调用复制到子类中,则我的代码有效,我知道这不是应该怎么做。你能帮我吗?

class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)

        self.wxFont = "9, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL"

        path =os.path.dirname(os.path.realpath(__file__))
        configFile = os.path.join(path , "config.ini")

        dbfile = os.path.join(path , "report.db")
        self.db_conn = sqlite3.connect(dbfile)
        self.theCursor =  self.db_conn.cursor()

        config = configparser.ConfigParser()
        config.read(configFile)


        if config.get('Network', 'taboola') == "true" and config.get('Network', 'outbrain') == "true":
            network = ""
        elif config.get('Network', 'taboola') == "true" and config.get('Network', 'outbrain') == "false":
            network = " and CAMPAIGN LIKE '%TB%'"
        elif config.get('Network', 'outbrain') == "true" and config.get('Network', 'taboola') == "false":
            network = " and CAMPAIGN LIKE '%OB%'"
        else:
            network = ""

        if config.get('Sort', 'value') == "Impr.":
            sort = "IMPR" 
        elif config.get('Sort', 'value') == "Clicks":
            sort = "CLICKS"
        elif config.get('Sort', 'value') == "CTR":
            sort = "CTR"  
        elif config.get('Sort', 'value') == "CPC":
            sort = "CPC"  
        elif config.get('Sort', 'value') == "eCPC":
            sort = "eCPC"
        elif config.get('Sort', 'value') == "Spent":
            sort = "SPENT"
        elif config.get('Sort', 'value') == "Revenue":
            sort = "GA_REV" 
        elif config.get('Sort', 'value') == "GA Impr.":
            sort = "GA_IMPR"  
        elif config.get('Sort', 'value') == "GA Clicks":
            sort = "GA_CLICKS"
        elif config.get('Sort', 'value') == "GA CTR":
            sort = "GA_CTR"
        elif config.get('Sort', 'value') == "Rev. /1000":
            sort = "GA_RPM"
        else:
            sort = "SPENT"

        #['Impr.', 'Clicks', 'CTR', 'CPC', 'eCPC', 'Spent', 'Revenue', 'GA Impr.', 'GA Clicks', 'GA CTR', 'Rev. /1000'] 

        if config.get('Filter', 'column') == "Campaign":
            column = "CAMPAIGN"
        elif config.get('Filter', 'column') == "Impr.":
            column = "IMPR"
        elif config.get('Filter', 'column') == "Clicks":
            column = "CLICKS"
        elif config.get('Filter', 'column') == "CTR":
            column = "CTR" 
        elif config.get('Filter', 'column') == "CPC":
            column = "CPC"  
        elif config.get('Filter', 'column') == "eCPC":
            column = "eCPC"    
        elif config.get('Filter', 'column') == "Spent":
            column = "SPENT"
        elif config.get('Filter', 'column') == "Revenue":
            column = "GA_REV" 
        elif config.get('Filter', 'column') == "GA Impr.":
            column = "GA_IMPR"
        elif config.get('Filter', 'column') == "GA Clicks":
            column = "GA_CLICKS"
        elif config.get('Filter', 'column') == "GA CTR":
            column = "GA_CTR"        
        elif config.get('Filter', 'column') == "Rev. /1000":
            column = "GA_RPM"
        else:
            column = ""

        if config.get('Filter', 'operator') == "Contains":
            query = "and " + column + " LIKE '%" + config.get('Filter', 'string') + "%'"
        elif config.get('Filter', 'operator') == "Doesn't Contain":
            query = "and " + column + " NOT LIKE '%" + config.get('Filter', 'string') + "%'"


        if config.get('Filter', 'operator') == "<":
            query = "and " + column + " < " + config.get('Filter', 'string')
        elif config.get('Filter', 'operator') == ">":
            query = "and " + column + " > " + config.get('Filter', 'string')

        #To instantiate
        queryFiltre = "SELECT * FROM Report WHERE  GA_RPM > 0 " + query + network + "  and STATUS = '1' ORDER BY " + sort + " DESC"
        print(queryFiltre) 

        rows = self.db_conn.execute(queryFiltre)
        rowsCount = len(rows.fetchall())

class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, 0, "V1.0", size=(1400,800))
        self.grid = SimpleGrid(self, log)

到目前为止我所做的:

class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)


        self.wxFont = "9, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL"
        self.queryFiltre = ""

        def setQueryFiltre(queryFiltre):
            path =os.path.dirname(os.path.realpath(__file__))
            configFile = os.path.join(path , "config.ini")
            dbfile = os.path.join(path , "report.db")
            self.db_conn = sqlite3.connect(dbfile)
            self.theCursor =  self.db_conn.cursor()

            config = configparser.ConfigParser()
            config.read(configFile)

            #All the calls to DB and config file here

            return queryFiltre

        def getQueryFiltre(queryFiltre):
            queryFiltre = queryFiltre

        rows = db_conn.execute(queryFiltre)
        rowsCount = len(rows.fetchall())


class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, 0, "V1.0", size=(1400,800))
        self.grid = SimpleGrid(self, log)

        self.wxFont = SimpleGrid(self.wxFont)
        self.queryFiltre = SimpleGrid.getQueryFiltre()

但是在这里,我得到的第一个错误是未定义db_conn,如果将它带走,我敢肯定我还有很多事情要做。实际上,可以从父类和子类中访问def setQueryFiltre中的所有变量。

谢谢

编辑:

有了oe而没有自我。我仍然遇到相同的错误 enter image description here

1 个答案:

答案 0 :(得分:1)

您在构造函数中声明了两个函数,它们从未被调用:setQueryFiltregetQueryFiltre。而且,您的测试将调用其中一个,就好像它是一个类方法一样。并非如此,因为声明仅在函数内部可见,因此失败。您可能要写的是这样的:

class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)
        self.setQueryFiltre(None)

        self.wxFont = "9, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.NORMAL"
        self.queryFiltre = ""

    def setQueryFiltre(self, queryFiltre):
        path = os.path.dirname(os.path.realpath(__file__))
        configFile = os.path.join(path , "config.ini")
        dbfile = os.path.join(path , "report.db")
        self.db_conn = sqlite3.connect(dbfile)
        self.theCursor =  self.db_conn.cursor()

        config = configparser.ConfigParser()
        config.read(configFile)

        #All the calls to DB and config file here
        return queryFiltre # why are you returning the argument? For chaining?

    def getQueryFiltre(self, queryFiltre):
        queryFiltre = queryFiltre

        rows = self.db_conn.execute(queryFiltre)
        rowsCount = len(rows.fetchall())
        # you probably want to return rows here?


class TestFrame(wx.Frame):
    def __init__(self, parent, log):
        wx.Frame.__init__(self, parent, 0, "V1.0", size=(1400,800))
        self.grid = SimpleGrid(self, log)

        # self.wxFont = SimpleGrid(self.wxFont) # what does this try to do?
        self.queryFiltre = self.grid.getQueryFiltre()

在Python中,缩进很重要。

请注意,我还将数据库初始化移到了它所属的构造函数中。因为否则,如果在设置之前调用get方法,则仍然会发生相同的崩溃。我还发表了其他几条评论,可能会被您咬住。

相关问题