views:

584

answers:

4

I am writing a simple Python web application that consists of several pages of business data formatted for the iPhone. I'm comfortable programming Python, but I'm not very familiar with Python "idiom," especially regarding classes and objects. Python's object oriented design differs somewhat from other languages I've worked with. So, even though my application is working, I'm curious whether there is a better way to accomplish my goals.

Specifics: How does one typically implement the request-transform-render database workflow in Python? Currently, I am using pyodbc to fetch data, copying the results into attributes on an object, performing some calculations and merges using a list of these objects, then rendering the output from the list of objects. (Sample code below, SQL queries redacted.) Is this sane? Is there a better way? Are there any specific "gotchas" I've stumbled into in my relative ignorance of Python? I'm particularly concerned about how I've implemented the list of rows using the empty "Record" class.

class Record(object):
    pass

def calculate_pnl(records, node_prices):
    for record in records:
        try:
            # fill RT and DA prices from the hash retrieved above
            if hasattr(record, 'sink') and record.sink:
                record.da = node_prices[record.sink][0] - node_prices[record.id][0]
                record.rt = node_prices[record.sink][1] - node_prices[record.id][1]
            else:
                record.da = node_prices[record.id][0]
                record.rt = node_prices[record.id][1]

            # calculate dependent values: RT-DA and PNL
            record.rtda = record.rt - record.da
            record.pnl = record.rtda * record.mw
        except:
            print sys.exc_info()

def map_rows(cursor, mappings, callback=None):
    records = []
    for row in cursor:
        record = Record()
        for field, attr in mappings.iteritems():
            setattr(record, attr, getattr(row, field, None))
        if not callback or callback(record):
            records.append(record)

    return records

def get_positions(cursor):
    # get the latest position time
    cursor.execute("SELECT latest data time")
    time = cursor.fetchone().time
    hour = eelib.util.get_hour_ending(time)

    # fetch the current positions
    cursor.execute("SELECT stuff FROM atable", (hour))

    # read the rows
    nodes = {}
    def record_callback(record):
        if abs(record.mw) > 0:
            if record.id: nodes[record.id] = None
            return True
        else:
            return False
    records = util.map_rows(cursor, {
        'id': 'id',
        'name': 'name',
        'mw': 'mw'
    }, record_callback)

    # query prices
    for node_id in nodes:
        # RT price
        row = cursor.execute("SELECT price WHERE ? ? ?", (node_id, time, time)).fetchone()
        rt5 = row.lmp if row else None

        # DA price
        row = cursor.execute("SELECT price WHERE ? ? ?", (node_id, hour, hour)).fetchone()
        da = row.da_lmp if row else None

        # update the hash value
        nodes[node_id] = (da, rt5)

    # calculate the position pricing
    calculate_pnl(records, nodes)

    # sort
    records.sort(key=lambda r: r.name)

    # return the records
    return records
+1  A: 

Have you considered using an ORM? SQLAlchemy is pretty good, and Elixir makes it beautiful. It can really reduce the ammount of boilerplate code needed to deal with databases. Also, a lot of the gotchas mentioned have already shown up and the SQLAlchemy developers dealt with them.

nosklo
I don't think an ORM is appropriate. I don't expect to write to the database at all -- this is strictly a data display project. And writing the queries is not a problem (in fact, I plan to simplify the first-draft code I posted above to remove the queries-in-a-loop issue).
Will Rogers
A: 

Using a ORM for an iPhone app might be a bad idea because of performance issues, you want your code to be as fast as possible. So you can't avoid boilerplate code. If you are considering a ORM, besides SQLAlchemy I'd recommend Storm.

Vasil
He says "formatted for the iPhone", a web page I would guess. The ORM would be on the server side. I know ORMs add a little overhead, but the cell phone network will be the real latency inducer.
alif
ORM is in Python. Can be on the client. ORM ships pure SQL to the server.
S.Lott
Like I said in the first sentence of my question, this is a web app. The Python code lives on the server. Also, I'm not really interested in ORM because I have only a handful of set read-only queries to implement.
Will Rogers
SQLAlchemy is not a ORM in a ActiveRecord style but a DataMapper which can be very efficient. SQLAlchemy also generates the ideal query for your desired result-set and does not do excessive multi querying...
pi
A: 

Depending on how much you want to do with the data you may not need to populate an intermediate object. The cursor's header data structure will let you get the column names - a bit of introspection will let you make a dictionary with col-name:value pairs for the row. You can pass the dictionary to the % operator. The docs for the odbc module will explain how to get at the column metadata.

This snippet of code to shows the application of the % operator in this manner.

>>> a={'col1': 'foo', 'col2': 'bar', 'col3': 'wibble'}
>>> 'Col1=%(col1)s, Col2=%(col2)s, Col3=%(col3)s' % a
'Col1=foo, Col2=bar, Col3=wibble'
>>>
ConcernedOfTunbridgeWells
Are there any benefits or drawbacks to using a dict vs. using an object for my intermediate rows? I do need some kind of intermediate step because I need to calculate and add additional columns in each row.
Will Rogers
If you are just rendering something from the contents of the query results then the '%' operator can substitute the dictionary values into placeholders marked with the %(xxx)s operator. For simple tasks this is simpler than instantiating a model or DB row object.
ConcernedOfTunbridgeWells
+1  A: 

The empty Record class and the free-floating function that (generally) applies to an individual Record is a hint that you haven't designed your class properly.

class Record( object ):
    """Assuming rtda and pnl must exist."""
    def __init__( self ):
        self.da= 0
        self.rt= 0
        self.rtda= 0 # or whatever
        self.pnl= None # 
        self.sink = None # Not clear what this is
    def setPnl( self, node_prices ):
        # fill RT and DA prices from the hash retrieved above
        # calculate dependent values: RT-DA and PNL

Now, your calculate_pnl( records, node_prices ) is simpler and uses the object properly.

def calculate_pnl( records, node_prices ):
    for record in records:
        record.setPnl( node_prices )

The point isn't to trivially refactor the code in small ways.

The point is this: A Class Encapsulates Responsibility.

Yes, an empty-looking class is usually a problem. It means the responsibilities are scattered somewhere else.

A similar analysis holds for the collection of records. This is more than a simple list, since the collection -- as a whole -- has operations it performs.

The "Request-Transform-Render" isn't quite right. You have a Model (the Record class). Instances of the Model get built (possibly because of a Request.) The Model objects are responsible for their own state transformations and updates. Perhaps they get displayed (or rendered) by some object that examines their state.

It's that "Transform" step that often violates good design by scattering responsibility all over the place. "Transform" is a hold-over from non-object design, where responsibility was a nebulous concept.

S.Lott
Thanks for actually taking the time to read what I posted and offer a thoughtful response. I'll have to think about your suggestion, it does make sense.
Will Rogers
Wrote a book on OO design. Not very good, but pretty complete. Hits this point pretty hard. See http://homepage.mac.com/s_lott/books/oodesign.html
S.Lott