Wednesday, June 29, 2011

Books: Python 3 Web Development Beginner’s Guide


I've been asked by Packt to review a new book called Python 3 Web Development Beginner’s Guide. Here's the overview:
  • Build your own Python web applications from scratch
  • Follow the examples to create a number of different Python-based web applications, including a task list, book database, and wiki application
  • Have the freedom to make your site your own without having to learn another framework
I looked through the table of contents, and I scanned the first 30 page. It looks like a fun book! In fact, it's exactly the sort of book I was hoping someone would write.

Wednesday, June 22, 2011

Python: Increasing the Timeouts for urlfetch in Google App Engine

Google App Engine provides a function, google.appengine.api.urlfetch.fetch, for fetching URLs. I do believe all the other HTTP client libraries are monkey patched to make use of that function, which is written to take advantage of various Google infrastructure. The fetch function has a default timeout of 5 seconds. You can set a higher timeout by passing a deadline parameter, but the maximum is 10 seconds. Unfortunately, passing a deadline keyword parameter is often difficult if it's a third-party library that is making the call to fetch, for instance if you're using the GData client library.

I looked for a way to set the deadline parameter in a more global way, but I couldn't fine one by mere inspection of the code. I came up with the following HACK in order to work around this problem:
# HACK: Monkeypatch google.appengine.api.urlfetch.fetch to increase the
# deadline. This is used by the various client libraries.
def _fetch(*args, **kargs):
from google.appengine.api.urlfetch import _orig_fetch # Import late.
kargs["deadline"] = 10
return _orig_fetch(*args, **kargs)
_fetch.this_is_the_wrapper = True

# HACK: Because of the way the dev app server works when reloading code,
# things are a little tricky here.
from google.appengine.api import urlfetch
if not hasattr(urlfetch.fetch, "this_is_the_wrapper"):
urlfetch._orig_fetch = urlfetch.fetch
urlfetch.fetch = _fetch
else:
assert hasattr(urlfetch, "_orig_fetch")

Thursday, June 09, 2011

Google I/O 2011

I went to Google I/O a few weeks ago. Although I took notes, rather than blogging all my notes, I thought I'd just link to my favorite two talks:

Programming Well with Others: Social Skills for Geeks
Are languages, compilers, debuggers, and algorithms all you need to be a successful software engineer? In a perfect world, those who produce the best code should be the most successful. Unfortunately, we live in a world of imperfect people, and collaborating with others is at least as important as having great technical skills.
HTML5 Showcase for Web Developers: The Wow and the How
We'll share the strengths and extents of HTML5, showing magnificent demos of bleeding-edge features in Google Chrome. Digging into high-fidelity graphics, performance, and system integration, we'll break each demo down on the big screen to show how it was constructed. Then we'll show you how to use Chrome to its full potential in your own projects.

Friday, June 03, 2011

Python: Getting a Fair Flip out of an Unfair Coin

If you have an unfair coin (i.e. one that favors heads or tails), how do generate a fair flip (i.e. one that doesn't favor heads or tails)? My buddy Hy Carrinski and I came up with the following algorithm:
"""Get a fair flip out of an unfair coin."""

from collections import defaultdict
from random import random

FLIP_RATIO = 0.7


def flip_unfair_coin():
"""Flip an unfair coin. Return a boolean."""
return random() > FLIP_RATIO


def create_fair_flip():
"""Generate a fair flip. Return a boolean."""
while True:
flip = flip_unfair_coin()
if flip != flip_unfair_coin():
return flip


# Demonstrate that it works.

if __name__ == '__main__':
results = defaultdict(int)
for i in xrange(1000000):
results[create_fair_flip()] += 1
percentage = (results[False] / float(results[False] + results[True])) * 100
print "Percentage heads:", percentage