git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Getter is not returning the data it should be


Bonjour Barb,

Please allow me to answer your question, somewhat, backwards:-

There is a principle in program design known as "separation of 
concerns". If you look at the class SimpleGrid and consider that the 
bulk of its code lies after the first comment, how would you summarise 
(in English) its actions? Does the answer contain the word "database" 
more often than any word relating to wxPython?

- Might it simplify matters if the functions related to those two words 
were separated?

Thus, let's look at the overall objective in the manner of OOP. There is 
a data-source (or two) and an output screen/frame/grid. Let's go 
completely 'purist' and create an object for each one.

- As soon as the 'database' (or query) object is created, does it answer 
the "both classes" question, below?

What is the need for separate (database) "getter" and "setter" methods? 
Once the query text has been built, why not query the DB immediately?

Please review "getQueryFiltre". Again, summarise (in English) exactly 
what you think/want this method to do.

- If the two separate methods are required, and they both appear in a 
'database' object, then make the query an attribute of that object 
(self) and you won't need to pass the query as a result/parameter.

- However, in Python we don't need to do that anyway because if 
setQueryFiltre set self.queryFiltre (instead of returning it) the 
calling-code would be able to access db.queryFiltre without any need for 
a "getter" (this Python idiom is quite different to many other 
programming languages, eg Java!)

Once the DB and screen operations have been separated:

(a) you will be able to test the DB code-operations independently and 
ensure the correct data is being retrieved for each usual- and 
special-case; and
(b) you will no longer receive the quite mystifying SimpleGrid errmsg 
(from wxPython) when you are expecting something data-related to be 
returned!


On 2/06/19 6:52 AM, Barb Cr33k wrote:
> I have a parent Class that has a setter that returns queryFiltre value and a getter that is supposed to pass the queryFiltre value to my child Class. queryFiltre Should return an SQL query like "SELECT * FROM Report WHERE GA_RPM > 0 and CAMPAIGN LIKE '%TT%'... ".
> 
> The print() in the setter returns a SQL query, but the print() of the getter when called in the child Class returns something like " <main.SimpleGrid object at 0x042AF2B0>".
> 
> What's wrong with my code? Please bear with me as I'm still learning and oop is still an abstract concept in my head.
> 
> I've added comments in the code so you can see what happens where:
> 
> 
> 
> class SimpleGrid(gridlib.Grid): ##, mixins.GridAutoEditMixin):
>      def __init__(self, parent, log):
>          gridlib.Grid.__init__(self, parent, -1)
> 
> 
>          ########### DATABASE CONNECT
>          self.path =os.path.dirname(os.path.realpath(__file__))
>          self.dbfile = os.path.join(self.path , "report.db")
>          self.db_conn = sqlite3.connect(self.dbfile)
>          self.theCursor =  self.db_conn.cursor()
>          ########### SETTING FILE CONNECT
>          self.configFile = os.path.join(self.path , "config.ini")
>          self.config = configparser.ConfigParser()
>          self.config.read(self.configFile)
> 
>          ########### Calling th Getter and Setter
>          self.queryFiltre = self.setQueryFiltre(self)
>          self.getQueryFiltre()
> 
>      ########### Setter
>      def setQueryFiltre(self,queryFiltre):
> 
>              if self.config.get('Network', 'taboola') == "true" and self.config.get('Network', 'ob') == "true":
>                  network = ""
>              elif self.config.get('Network', 'taboola') == "true" and self.config.get('Network', 'ob') == "false":
>                  network = " and CAMPAIGN LIKE '%TB%'"
>              elif self.config.get('Network', 'outbrain') == "true" and self.config.get('Network', 'tb') == "false":
>                  network = " and CAMPAIGN LIKE '%OB%'"
>              else:
>                  network = ""
> 
>              queryFiltre = "SELECT * FROM Report WHERE  GA_RPM > 0 " + network + "  and STATUS = '1' ORDER BY CLICKS DESC"
> 
>              ########### The print below returns the right value of queryFiltre
>              print(queryFiltre)
> 
>              return queryFiltre
> 
>          ########### Getter
>          def getQueryFiltre(queryFiltre):
>              queryFiltre = queryFiltre
>              return queryFiltre
> 
> 
> class TestFrame(wx.Frame):
>      def __init__(self, parent, log):
>          wx.Frame.__init__(self, parent, 0, "Native Ads Reports V1.0", size=(1400,800))
>          self.grid = SimpleGrid(self, log)
> 
>          ########### Calling the Getter of the parent Class
>          self.queryFiltre = self.grid.getQueryFiltre()
> 
>          ########### DATABASE CONNECT
>          self.path =os.path.dirname(os.path.realpath(__file__))
>          self.dbfile = os.path.join(self.path , "report.db")
>          self.db_conn = sqlite3.connect(self.dbfile)
>          self.theCursor =  self.db_conn.cursor()
> 
>          ########### The print below returns a bad value, something like : <__main__.SimpleGrid object at 0x042AF2B0>
>          print(self.queryFiltre)
> 
> 
> You'll notice also that I've added the script to define the path and to connect to the db in both classes, is there a way to do it only once in the first Class?
> 
> Thank you,
> 

-- 
Regards =dn