Aaron Schlesinger

Software Engineer, Soccer Player, Creator of Go In 5 Minutes (bitly.com/goin5minutesyt)

Page 3

Map (no reduce) with Ripeline

I went to MongoSF 2011 and attended a talk called “MongoDB’s New Aggregation Features - A Sneak Peek” by Chris Westin. As of writing, I don’t believe that slides are posted, but they will be soon.

In the talk, Chris said that they’re working on pipeline-based aggregation for Mongo, as well as a rich set of pre-built operators that can be applied in any order in that pipeline. Step 1: define your data (a Mongo collection), step 2: write your pipeline, step 3: profit.

In functional terms, this Mongo pipeline is functionally equivalent to applying multiple mappers to each element in a collection (ruby’s version for Arrays: http://www.ruby-doc.org/core/classes/Array.html#M000249), and streaming results from each mapper to the next. That model fits lots of problems too, and it’s simpler to conceptualize. Sometimes you just don’t need to reduce.

Turns out someone wrote a pipeline for...

Continue reading →

Type Safety For Python Functions

Have you ever written something like this Python code?

def myFunction(a, b):
  if type(a) != str and type(b) != int:
    raise "a must be a string or b must be an int"
  #do some more stuff

I find myself writing that all the time. I want the type flexibility of Python a lot of the time, but I also want to enforce types when I need to. Writing that if statement over and over at the top of all of your functions is not very DRY, and it’s error prone and annoying. I solved that problem with a decorator.

 Function Decorators FTW

Python decorators are functions that you tell Python to call & pass another function into it. Python will replace the passed function with whatever your decorator function returns. Here’s a concrete example:

def log(func):
    def inner(*argv):
        print "calling %s:"%(func.__name__)
        print "done calling %s"%(func.__name__)

Continue reading →