3

DISCLAIMER: This question is related to homework. I am not expecting a solution, but am hoping for a better understanding of what is being asked and the (possible) utility of this homework exercise.

This question was asked after a lesson on using Python's MySQLdb module and how to better represent the result set (and possibly its rows) instead of a tuple of tuples (as is default with MySQLdb).

The question is: Modify the classFactory.py source code so that the DataRow class returned by the build_row function has another method:

retrieve(self, curs, condition=None)

self is (as usual) the instance whose method is being called, curs is a database cursor on an existing database connection, and condition (if present) is a string of condition(s) which must be true of all received rows. The retrieve method should be a generator, yielding successive rows of the result set until it is completely exhausted. Each row should be a new object of type DataRow.

def build_row(table, cols):
    """Build a class that creates instances of specific rows"""
    class DataRow:
        """Generic data row class, specialized by surrounding function"""
        def __init__(self, data):
            """Uses data and column names to inject attributes"""
            assert len(data)==len(self.cols)
            for colname, dat in zip(self.cols, data):
                setattr(self, colname, dat)
        def __repr__(self):
            return "{0}_record({1})".format(self.table, ", ".join(["{0!r}".format(getattr(self, c)) for c in self.cols]))
    DataRow.table = table
    DataRow.cols = cols.split()
    return DataRow
  1. What is the purpose of creating a class within a function definition? Is this commonly done by professional developers? If so, what is the benefit?
  2. Why is the database cursor an (explicit) argument of the retrieve method? Shouldn't the database cursor be referenced outside the scope of this script (when the database connection is made)?
  3. Is it correct to say that (currently) the build_row function will accept two arguments - table and cols - and return a string that may look like (as an example) "exampleTable_record(fieldA, fieldB, fieldC)"?
  4. When the retrieve method is successfully added, what should I expect build_row to now return?

As I alluded to earlier, if I had an idea of why I'm being asked to add the retrieve method, then I might have a better idea of how to attack this problem.

mgilson
  • 300,191
  • 65
  • 633
  • 696
Jubbles
  • 4,450
  • 8
  • 35
  • 47
  • Are you missing a level of indentation in your function? – Waleed Khan Oct 22 '12 at 02:39
  • This _type_ of thing is done fairly commonly in Python. In this case I would be worried about columns called "table" or "cols" or"retrieve" overwriting attributes, so I don't think it's a good example of what _should_ be done. However professional developers _do_ write **buggy exploitable** code quite often :) – John La Rooy Oct 28 '12 at 23:34

3 Answers3

3
  1. This is referred to as the Factory Design Pattern ... although in this case its just to make instansiation of the class be forced to the method that contains the class... this forces you to set the table and column name attributes at time of instanciation ... which could cause errors if not properly instanciated when you try and call retrieve ...
  2. Im in order to query the database it needs a cursor object
  3. Yes , but it returns and the DataRow Class not a string... this has a string representation
  4. A datarow that has data ...

here is a link with a discussion of factory design pattern Why do we need Abstract factory design pattern?

an example retrieve function

@classmethod #if this is a classmethod
def retrieve(self, curs, condition=None):
        if condition:
           curs.execute("SELECT * FROM {0} WHERE {1}".format(self.table,condition)
        else:
           curs.execute("SELECT * FROM {0}".format(self.table)
        for row in curs.fetchall():
            yield DataRow(row)

then you would do something like

my_users = build_row("users",["username","password","avatar"])
my_users = my_users()  #if you need to operate on an instance.... but class method would be better
#users is the name of a table already in our database, whose columns are username,password, and avatar
db=sqlite3.connect("some_database.sql")
for user in my_users.retrieve(db.cursor(),"username='bob'"):
    print user #something like users_record({'username':'bob','password':'123%gw','avatar':None)
    print user.username #bob
    print user.avatar #None
    print user.password #whatever
    print user.blah #Error!!!!
Community
  • 1
  • 1
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • (1) Ok, so this is a common practice; what advantage does using the Factory Design Pattern provide? (2) Why does the database cursor need to be called within this function? (4) Can you provide an example (complete with dummy data) of what would be returned after the `retrieve` method is successfully added to the class? – Jubbles Oct 24 '12 at 02:41
  • You are using `retrieve` as if it were a class method. That would be appropriate, but the question and the answer imply an instance method. – Janne Karila Oct 30 '12 at 20:19
  • fixed ... sorta ... but it should probably be a class method – Joran Beasley Oct 30 '12 at 21:10
2

2) Why is the database cursor an (explicit) argument of the retrieve method? Shouldn't the database cursor be referenced outside the scope of this script (when the database connection is made)?

Encapsulation. Relying on global magic variables creates dependencies that should be avoided when possible. Consider that your application might have multiple cursors active at the same time, or might even be talking to two databases through separate connections.

Passing in the argument also makes your code easier to test: You can pass it a mock class or some such, without having to modify global variables.

alexis
  • 48,685
  • 16
  • 101
  • 161
1

What you have is an example where this can be used professionally. The function build_row is a class factory that returns a class suitable of handling data for a particular table and columns.

Although I would have personally not used the Factory Pattern in this specific case, it does work.

So to explain what this piece of code is doing, it basically creates a class that uses the table and cols from the function call. If you notice in the code self.cols and self.table are being but they are not really part of the DataRow, they are attached to it afterwards, which shows off the flexibility of creating classes this way.

What the build_row function actually returns is a Class! this class should be used to get data from the database ... this is the part that you have to build for your assignment. You will need to use table (the table name) in your select statement, you will also need the cols ... essentially your function (which I will not write for you) should do:

SELECT <PUT THE `cols` HERE> FROM `table`;

and return the results. This should be enough to get you going.

dbr
  • 165,801
  • 69
  • 278
  • 343
Pykler
  • 14,565
  • 9
  • 41
  • 50