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
retrieve method? Shouldn't the database cursor be referenced outside the scope of this script (when the database connection is made)?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)"?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.
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!!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With