Skip to content
heynemann edited this page Sep 13, 2010 · 16 revisions

Pynq – Python Language Integrated Query

Microsoft created Linq (Language Integrated Query) using Expression trees, which is a math concept on how to parse operations into trees in a way that you can analyze the operations independently from the result.

Pynq is an implementation in Python of the Expression Tree theory and some of the providers. There will be more providers gradually, but Pynq will strive to make it as easy as possible to write your own provider.

Installing Pynq

Debian / Ubuntu users

We have a .deb repository that you can add to your sources list.

Just add these two lines in the /etc/apt/sources.list file:

deb http://deb.gabrielfalcao.com/unstable/ ./
deb-src http://deb.gabrielfalcao.com/unstable/ ./

After adding those, execute the following commands in a terminal window:

sudo aptitude update
sudo aptitude install python-pynq

If aptitude asks if you confirm installing a package that’s not trusted it’s because we haven’t yet submitted the package for debian approval (we will though when the project is a little more mature).

It should be installed by now, but just to make sure, run ipython and type:


import pynq

If you don’t get any error messages it’s installed properly.

Windows / Linux / Mac OS users

When installing from source, you have two options: stable or head.

If you want stable release versions, go to http://pypi.python.org/pypi/Pynq/ and you can find the latest stable release. The same files can be found in http://github.com/heynemann/pynq/downloads. This is so we keep mirrors of Pynq code in case one of them is down.

If you’d rather get the current HEAD revision from git, just click on Source above and select the Public Clone Url. Using git to download the code is beyond the scope of this page.

After getting the .tar.gz version of the release you want (or the source code for it) you can:

  • Create a symbolic link for it in Linux, under python site-packages folder
  • Add it to the PYTHONPATH environment variable in Windows/Mac OS

It should be installed by now, but just to make sure, run ipython and type:


import pynq

If you don’t get any error messages it’s installed properly.

Easy Install

You can also use setuptools easy_install to install pynq, even though it is discouraged to do so under Debian/Ubuntu (check the first install method).

To install it using easy_install do:

sudo easy_install pynq

This assumes you have easy_install installed (for more info about easy_install check http://peak.telecommunity.com/DevCenter/EasyInstall).

It should be installed by now, but just to make sure, run ipython and type:


import pynq

If you don’t get any error messages it’s installed properly.

Introduction to Expression Trees

Since Pynq uses solely expression trees as means to the provider to parse whatever it needs to parse, it means that anyone can implement a provider to basically anything that can be queried.

Expression Trees are a pretty simple concept to grasp (even though they are complex to implement). Let’s see an example:

Let’s say we want an expression tree of the “2+3” expression. This is what it evaluates to:

  
    BinaryExpression("Add", ConstantExpression(2), ConstantExpression(3))
  

What that means is that a provider can now parse the expression you informed in order to find that you wanted the sum of 2 and 3, whatever that means in the given context.

If you want to know more about how Pynq implements expression trees and the ones available to you, check the Expression Trees page.

Pynq Factory

Pynq comes with a factory called From (from is a keyword in Python, thus we chose to ignore PEP-81).

From is the entry-point to Pynq. You use From to specify the Pynq Provider you want to use (we’ll talk more about Pynq Providers later). The following is a typical Pynq query:

  
filtered_collection = From(some_collection).where("item.property > 10").select_many()
  

This code gets all the items in the given collection (some_collection) that have the property “property” greater than 10. The key thing to note here is the From(some_collection) call.

The From method takes a pynq provider as parameter. Passing in collections is just a shortcut to use the CollectionProvider. If you pass Tuple or List instances to the From method, Pynq will assume you want CollectionProvider.

Nothing stops you from using other providers, though. Imagine we want to write a Twitter2 provider. We could use it like so:

  
tweets = From(TwitterProvider("user","password")
                       .where("item.author == 'heynemann'")
                       .select_many()
  

This code selects all the tweets that have heynemann as author from the tweet list of “user”. This provider is not implemented (maybe yet), but it’s completely feasible.

To know more about the Pynq Factory you can check the Pynq Factory page.

External References

1 PEP-8 is a Style Guide for Python. It guides most style-related decisions in Pynq.

2 Twitter is a micro-blogging website.

Clone this wiki locally