github.com/hoffmann Peter Hoffmann on Stackoverflow @peterhoffmann on twitter Peter Hoffmann on Facebook Contact me per email Subscribe to Atom Feed

Peter Hoffmann

Software Engineer
prev page next page

asq - a simple implementation of a LINQ-inspired API for python

Posted on June 6, 2011
#python

Robert Smallshire has released version 1.0 of asq:

asq is simple implementation of a LINQ-inspired API for Python which operates over Python iterables, including a parallel version implemented in terms of the Python standard library multiprocessing module.

An example what you can do with asq from the nice documentation

>>> students = [dict(firstname='Joe', lastname='Blogs', scores=[56, 23, 21, 89]),
            dict(firstname='John', lastname='Doe', scores=[34, 12, 92, 93]),
            dict(firstname='Jane', lastname='Doe', scores=[33, 94, 91, 13]),
            dict(firstname='Ola', lastname='Nordmann', scores=[98, 23, 98, 87]),
            dict(firstname='Kari', lastname='Nordmann', scores=[86, 37, 88, 87]),
            dict(firstname='Mario', lastname='Rossi', scores=[37, 95, 45, 18])]

>>> query(students).order_by(lambda s: query(s['scores']).average()) \
...                 .where(lambda student: student['firstname'].startswith('J')) \
...                 .select(lambda s: s['firstname'] + ' ' + s['lastname']) \
...                 .to_list()
['Joe Blogs', 'John Doe', 'Jane Doe']

You can do the same with list comprehension/generator expressions:

def avgscore(s):
	return avg(s['scores'])

[s['firstname']+' '+s['lastname'] for s in sorted(students, key=avgscore) if s['firstname'].startswith('J')]

But as discussed on reddit asq has some nice features like partial sorting and support for performing queries in parallel across multiple cores.