BeautifulSouping Some Movie Recommendations

There was a link on Metafilter a while back to a bunch of movie directors and their top ten movie picks (over at I found some good ones on there, but kinda wanted a big ol' aggregate list, so I python'd one up. I'm just giving them points according to how high they are on a given list and adding those up. List and codes follow:

from bs4 import BeautifulSoup
import urllib
from collections import defaultdict

def get_things():
    soup = BeautifulSoup(urllib.urlopen(''))
    rows = soup.find_all('table')
    result = []
    for r in rows:
        children = list(r.children)
        if len(children) == 5:
            kidz = children[3].find_all('i')
            result.append((children[1].find('font'), [(tag.string, len(kidz) - (i)) for i, tag in enumerate(kidz)]))
    return result

if __name__ == "__main__":
    things = get_things()
    aggregate = defaultdict(int)
    for thing, films in things:
        for film, score in films:
            aggregate[film] += score

    sorts = sorted(aggregate.items(), cmp=lambda x,y:cmp(x[1], y[1]))[::-1]

    print "\n".join(["%s : %d" % (k,v) for k, v in sorts])

Continue reading "BeautifulSouping Some Movie Recommendations"

An interesting visualization demonstrating why not to use floating point calculations to determine if large integers are perfect squares

I've been working through The Art and Craft of Problem Solving, and there's been a few problems in it based on Pascal's Triangle. After some hours of scribbling numbers on paper, I got lazy and wrote up a generator for it in Python. The book wants the reader to examine some of the properties of the triangle, so I had my triangle renderer (renderer? ha, it's text!) take a function that'd return a boolean to determine if something should be shown or not. While scribbling odd and even values of the triangle on paper, I noticed that it kind of resembled Sierpinski's triangle, so I started with that.

Now this looked pretty neat to me, so I was thinking of another easy pattern I could look for. Perfect squares came to mind; just check to see if the square root of a number is an integer and you're all setup, right? I tried passing that function into the triangle renderer and beheld an interesting result.

It appeared as if perfect squares formed a kind of parabola past 100 rows of the triangle! What a discovery! Of course, I'm really doubting my naive perfect square test at this point, so I googled up a better one and tried that out. A less interesting result awaited me:

Even though I hadn't made the amazing discovery that perfect squares existed, buried within Pascal's triangle in an almost perfect parabola, I had learned to distrust floating point tests on gigantic integers. So there's that.

Code Follows:
Continue reading "An interesting visualization demonstrating why not to use floating point calculations to determine if large integers are perfect squares"

Django TestCase Comedy

Here's a little snippet that'll entertain the vast throngs of people who are interested in both Rock Paper Scissors strategy and Django unit testing.

def test_desperate_futility_of_avalanche(self):
        for futile_attempt in range(100):
        self.failUnlessEqual(self.normal_game.result(), UNRESOLVED)

For the uninitiated:

Implementing Partials in Python

A colleague of mine was wistfully pining for Haskell-style currying in Python. This sounded like an interesting syntactic feature, so I decided to see if I could toss together something that would at least emulate the behavior of returning functions when a function is called with too few arguments. I've always admired the flexibility of python, so it seemed like an interesting challenge to prove that.

Using a decorator seemed like a bit of a copout due to the additional syntax required, but was otherwise ideal, as it made checking arguments fairly trivial.

def partial(f):
    def wrapper(*args, **kwargs):
            return f(*args, **kwargs)
        except TypeError, t:
            return lambda *_args,**_kwargs:f(*(args+_args), **(dict(kwargs.items()+_kwargs.items())))
    return wrapper

Continue reading "Implementing Partials in Python"