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

  • Home
  • SEARCH
  • 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 58909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:55:20+00:00 2026-05-10T17:55:20+00:00

I am writing a simple Python web application that consists of several pages of

  • 0

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 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. 2026-05-10T17:55:21+00:00Added an answer on May 10, 2026 at 5:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer To get started with extracting ID3 tags in Python, there's… May 11, 2026 at 3:19 pm
  • added an answer You could use svn status command. If you didn't set… May 11, 2026 at 3:19 pm
  • added an answer According to this, Visual Studio shouldn't be required--just the .NET… May 11, 2026 at 3:19 pm

Related Questions

I'm writing a reasonably complex web application. The Python backend runs an algorithm whose
I know I'll get a thousand Depends on what you're trying to do answers,
I am writing a web-framework for Python, of which the goal is to be
I am writing a program that's similar to a shell. Once started up, there's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.