Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8726353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:13:42+00:00 2026-06-13T08:13:42+00:00

DISCLAIMER: This question is related to homework. I am not expecting a solution, but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:13:43+00:00Added an answer on June 13, 2026 at 8:13 am
    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!!!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: This is for a homework assignment, but the question is not regarding the
Disclaimer This is not strictly a programming question, but most programmers soon or later
Disclaimer : I already asked this question , but without the deployment requirement. I
Disclaimer: this question is directly related to my programming homework. My C++ assignment consists
( DISCLAIMER : This is NOT a question about understanding the difference between abstract
Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This is for a programming class, but it is not the answer or
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.