Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add method to class returned by function

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.

like image 834
Jubbles Avatar asked Apr 22 '26 13:04

Jubbles


1 Answers

  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!!!!
like image 50
Joran Beasley Avatar answered Apr 25 '26 02:04

Joran Beasley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!