Templating languages, and YAPTL » |
SQLComp, an experimental LINQ-in-Python
We've (hopefully) all seen C# 3.0's fantastic new feature called LINQ. I think it's a great idea. It makes it easy to query differing data sources using an embedded query syntax. Wouldn't it be great if we had something like this for Python?
We do. It's called a list (or generator) comprehension. For example, it lets you rewrite this LINQ example like this:
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0] lowNums = [number for number in numbers if number < 5]
The problem is, we can only run these comprehensions inside the Python application, which is inefficient if you want to talk to a SQL RDBMS with a huge amount of data to filter.
Enter SQLComp. SQLComp breaks down the ASTs and translates your list and generator comprehensions to SQL, and automatically queries the database and gets the result. It also allows simple, safe variable interpolation. The wiki page has a few simple examples on it.
Please note that SQLComp is experimental, and I intended for someone to perhaps pick it up, hack on it, and make something cool. Shoot me an email if you're interested in collaborating.
2 comments
Hey, that looks really cool! I'm not a huge SQL fan, and I don't feel like learning or dealing with the "bulk" of an ORM. I'll definitely be checking this out.
ps
CP 2.2beta was released today.

Please note that SQLComp is experimental, and I intended for someone to perhaps pick it up, hack on it, and make something cool.
I decided to pick it up, hack on it, and make something cool--two years ago. It's called Dejavu. ;) See the logic* and codewalk*** modules, which "do the same thing" (only putting the iteration outside the lambda). I used logic.Expression instead of "make_query", and **kwargs instead of ctx.
I would also note that I found using the ast to be far too slow, and went with decompiling co_code entirely for speed reasons. You might want to do some speed tests of your own soon.
* http://projects.amor.org/dejavu
** http://projects.amor.org/dejavu/browser/trunk/logic.py
*** http://projects.amor.org/dejavu/browser/trunk/codewalk.py
This post has 2 feedbacks awaiting moderation...