Reputation: 647
I have a beginner problem here. I have a parent Class that contains a call to SQLite DB and to ConfigParser ini file and I want my child Class to access all those pieces of information.
Below is my code and what I've tried so far with no success. I understand instantiation but only with minimal examples like employees/salaries lol.
My code works if I copy all the calls to DB and config files into the child Class and it's not how it should be done, I know. Could you please help me?
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)
What I've done so far:
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()
But here the first error I get is the db_conn is not defined and I'm sure I will still have more to handel if this one is taken car of. In fact all the variables within the def setQueryFiltre should be accessible from within both the parent and the child Classes.
Thank you,
EDIT:
With oe without self. i still get the same error
Upvotes: 0
Views: 50
Reputation: 4176
You declared 2 functions inside your constructor, which never get called: setQueryFiltre
and getQueryFiltre
. Moreover, your test calls one of them as if it was a class method. It's not, and it will fail becuase that declaration is only visible inside the function. What you probably meant to write is this:
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()
In Python, indentation is important.
Note that I also moved db initialization to constructor, where it belongs. Because otherwise, if get method gets called before set, you will still have the same crash. I also made a couple other comments that you'll probably get bitten by next.
Upvotes: 1