string.Template question
Can you use dicts with string.Template? e.g. a structure like: game = { 'home': {'team': row['home_team_full'], 'score': row['home_score'], 'record': '0-0', 'pitcher': { 'id': home_pitcher.attrib['id'], 'name': home_pitcher.attrib['last_name'], 'wins': home_pitcher.attrib['wins'], 'losses': home_pitcher.attrib['losses'] }, 'win': home_win} } Then, in the template, 'game' is passed, but I want to access like $home.pitcher.id This doesn't seem to work, though. Is it possible? Or must everything in the dict passed to string.Template be one-level deep string variables? -- http://mail.python.org/mailman/listinfo/python-list
A question on scope...
In writing out python classes, it seems the 'self' is optional, meaning that inside a class method, "self.foo = bar" has the same effect as "foo = bar". Is this right? If so, it seems a little odd- what's the rationale? Or am I mistaken? -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
n00b confusion re: local variable referenced before assignment error
Writing a class which essentially spiders a site and saves the files locally. On a URLError exception, it sleeps for a second and tries again (on 404 it just moves on). The relevant bit of code, including the offending method: class Handler(threading.Thread): def __init__(self, url): threading.Thread.__init__(self) self.url = url def save(self, uri, location): try: handler = urllib2.urlopen(uri) except urllib2.HTTPError, e: if e.code == 404: return else: print "retrying %s (HTTPError)" % uri time.sleep(1) self.save(uri, location) except urllib2.URLError, e: print "retrying %s" % uri time.sleep(1) self.save(uri, location) if not os.path.exists(os.path.dirname(location)): os.makedirs(os.path.dirname(location)) file = open(location, "w") file.write(handler.read()) file.close() ... But what I am seeing is that after a retry (on catching a URLError exception), I see bunches of "UnboundLocalError: local variable 'handler' referenced before assignment" errors on line 38, which is the "file.write(handler.read())" line.. What gives? -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor
Is there some kind of mysterious logic to how the the columns are ordered when executing the following: sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' GROUP BY player_id" % (start, date) cursor.execute(sql) for row in cursor.fetchall(): print row.keys() What I get is: ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] Neither alphabetical nor the order in which they were specified in the query nor... any seeming order I can suss out. Any ideas? Thanks! (cursor being a MySQLdb.cursors.DictCursor object.) -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb and ordering of column names in list returned by keys() w/ a DictCursor
Will this order at least be the same for that same query every time the script is executed? On Thu, Jul 2, 2009 at 10:48 AM, Tim Chase wrote: > sql = "SELECT player_id, SUM(K) AS K, SUM(IP) AS IP, SUM(ER) AS ER, SUM(HR) >> AS HR, SUM(H) AS H, SUM(BB) AS BB, Teams.league FROM Pitching INNER JOIN >> Teams ON Pitching.team = Teams.team_id WHERE Date BETWEEN '%s' AND '%s' >> GROUP BY player_id" % (start, date) >> cursor.execute(sql) >> >> for row in cursor.fetchall(): >>print row.keys() >> >> What I get is: >> >> ['league', 'BB', 'HR', 'IP', 'K', 'H', 'player_id', 'ER'] >> >> Neither alphabetical nor the order in which they were specified in the >> query >> nor... any seeming order I can suss out. Any ideas? Thanks! >> >> (cursor being a MySQLdb.cursors.DictCursor object.) >> > > My guess is you're experiencing the fact that dicts are unordered by nature > which allows it to return in any order it likes (usually per the internal > representation/storage). > > -tkc > > > > -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
Question regarding style/design..
Sometimes I see relatively small application, generally task scripts, written as essentially a list of statements. Other times, I see them neatly divided into functions and then the "if __name__ == '__main__':" convention. Is there a preference? Is there an... application scope such that the preference shifts from the former to the latter? I understand the use of the __name__ == 'main' convention for building unit tests, but I'm mixed on using it in scripts/small applications. Thanks for any thoughts! -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
RPY2 examples?
I am trying to find examples using RPY2 to render R graphs to PNG/PDF/etc. The only things I can find use rpy 1.x. Any references? Thanks! -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
Sorting dict by value w/ operator.itemgetter- using key name?
Bit of code: print sorted(results.items(), key=operator.itemgetter(1)) Would rather use 'H9', which is the name of the key in position 1 like: print sorted(results.items(), key=operator.itemgetter('H9')) Obviously that ain't work else I wouldn't be sending this email. Any tips? -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
Ordering of dict keys & values
I understand that the keys in a dictionary are ordered not randomly but something practically close to it, but if I create a SQL query like so: query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()), ','.join(stats.values())) Can I at least rely on the value being in the same index as its corresponding key? -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list