Python 3:将变量从一个 CLASS 传递给另一个
Python 3: Pass variable from one CLASS to another
我这里有一个初学者问题。我有一个 parent Class,其中包含对 SQLite DB 和 ConfigParser ini 文件的调用,我希望我的 child Class 能够访问所有这些信息。
下面是我的代码以及我迄今为止尝试过但没有成功的代码。我理解实例化,但只有很少的例子,比如 employees/salaries 哈哈。
如果我将对数据库和配置文件的所有调用都复制到 child Class 中,我的代码就可以工作,但我知道这不是应该如何完成的。你能帮帮我吗?
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 中的所有变量都应该可以从 parent 和 child Classes.
中访问
谢谢,
编辑:
有oe没有自我。我仍然得到同样的错误
您在构造函数中声明了 2 个函数,它们从未被调用:setQueryFiltre
和 getQueryFiltre
。此外,您的测试调用其中之一,就好像它是 class 方法一样。它不是,它会失败,因为声明只在函数内部可见。你可能想写的是:
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 方法,您仍然会遇到同样的崩溃。我还发表了一些其他评论,您接下来可能会被咬到。
我这里有一个初学者问题。我有一个 parent Class,其中包含对 SQLite DB 和 ConfigParser ini 文件的调用,我希望我的 child Class 能够访问所有这些信息。
下面是我的代码以及我迄今为止尝试过但没有成功的代码。我理解实例化,但只有很少的例子,比如 employees/salaries 哈哈。
如果我将对数据库和配置文件的所有调用都复制到 child Class 中,我的代码就可以工作,但我知道这不是应该如何完成的。你能帮帮我吗?
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 中的所有变量都应该可以从 parent 和 child Classes.
中访问谢谢,
编辑:
有oe没有自我。我仍然得到同样的错误
您在构造函数中声明了 2 个函数,它们从未被调用:setQueryFiltre
和 getQueryFiltre
。此外,您的测试调用其中之一,就好像它是 class 方法一样。它不是,它会失败,因为声明只在函数内部可见。你可能想写的是:
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 方法,您仍然会遇到同样的崩溃。我还发表了一些其他评论,您接下来可能会被咬到。