philihp.com

Tag: Python

How to retrieve the ID after a MySQL Insert in Python

The MySQLdb documentation mentions conn.insert_id(), however this appears to have been deprecated, maybe? This really should be on the cursor object anyway. And behold! There is a lastrowid on it! It’s just a little bit undocumented

Debugging Urllib2 in Python

I was having some trouble debugging an HTTP request using urllib2 in Python which did some unorthodox things with HTTP headers. The urllib2 module itself doesn’t have much debug facility. You can see response headers by looking at the hdrs attribute on the exception like this try: opener.open(url) except urllib2.HTTPError, e: print e.hdrs But in [...]

Backward for loop iteration in Python

So lately I’ve been hacking up Python on the self imposed restriction of running everything on a 450 MHz Pentium II. This way if I ever do anything less-than-optimal, it’s immediately obvious, and I don’t learn any bad habits. Then I came across the following optimization. >>> import os>>> for k, v in os.environ.items():… print [...]