Can you do LINQ-like queries in a language like Python or Boo?
Asked Answered
H

6

5

Take this simple C# LINQ query, and imagine that db.Numbers is an SQL table with one column Number:

var result = 
    from n in db.Numbers
        where n.Number < 5
        select n.Number;

This will run very efficiently in C#, because it generates an SQL query something like

select Number from Numbers where Number < 5 

What it doesn't do is select all the numbers from the database, and then filter them in C#, as it might appear to do at first.

Python supports a similar syntax:

result = [n.Number for n in Numbers if n.Number < 5]

But it the if clause here does the filtering on the client side, rather than the server side, which is much less efficient.

Is there something as efficient as LINQ in Python? (I'm currently evaluating Python vs. IronPython vs. Boo, so an answer that works in any of those languages is fine.)

Helotry answered 22/9, 2008 at 21:24 Comment(0)
F
4

I believe that when IronPython 2.0 is complete, it will have LINQ support (see this thread for some example discussion). Right now you should be able to write something like:

Queryable.Select(Queryable.Where(someInputSequence, somePredicate), someFuncThatReturnsTheSequenceElement) 

Something better might have made it into IronPython 2.0b4 - there's a lot of current discussion about how naming conflicts were handled.

Figwort answered 23/9, 2008 at 0:11 Comment(1)
IronPython 2.0 is just about complete, and I would say that we don't support LINQ at this point.Aarhus
R
7

sqlsoup in sqlalchemy gives you the quickest solution in python I think if you want a clear(ish) one liner . Look at the page to see.

It should be something like...

result = [n.Number for n in db.Numbers.filter(db.Numbers.Number < 5).all()]
Rete answered 23/9, 2008 at 0:52 Comment(0)
S
5

Look closely at SQLAlchemy. This can probably do much of what you want. It gives you Python syntax for plain-old SQL that runs on the server.

Sherri answered 22/9, 2008 at 21:35 Comment(0)
T
5

LINQ is a language feature of C# and VB.NET. It is a special syntax recognized by the compiler and treated specially. It is also dependent on another language feature called expression trees.

Expression trees are a little different in that they are not special syntax. They are written just like any other class instantiation, but the compiler does treat them specially under the covers by turning a lambda into an instantiation of a run-time abstract syntax tree. These can be manipulated at run-time to produce a command in another language (i.e. SQL).

The C# and VB.NET compilers take LINQ syntax, and turn it into lambdas, then pass those into expression tree instantiations. Then there are a bunch of framework classes that manipulate these trees to produce SQL. You can also find other libraries, both MS-produced and third party, that offer "LINQ providers", which basically pop a different AST processer in to produce something from the LINQ other than SQL.

So one obstacle to doing these things in another language is the question whether they support run-time AST building/manipulation. I don't know whether any implementations of Python or Boo do, but I haven't heard of any such features.

Termitarium answered 22/9, 2008 at 21:40 Comment(0)
F
4

I believe that when IronPython 2.0 is complete, it will have LINQ support (see this thread for some example discussion). Right now you should be able to write something like:

Queryable.Select(Queryable.Where(someInputSequence, somePredicate), someFuncThatReturnsTheSequenceElement) 

Something better might have made it into IronPython 2.0b4 - there's a lot of current discussion about how naming conflicts were handled.

Figwort answered 23/9, 2008 at 0:11 Comment(1)
IronPython 2.0 is just about complete, and I would say that we don't support LINQ at this point.Aarhus
P
1

Boo supports list generator expressions using the same syntax as python. For more information on that, check out the Boo documentation on Generator expressions and List comprehensions.

Paperboy answered 31/10, 2008 at 18:55 Comment(0)
L
0

A key factor for LINQ is the ability of the compiler to generate expression trees. I am using a macro in Nemerle that converts a given Nemerle expression into an Expression tree object. I can then pass this to the Where/Select/etc extension methods on IQueryables. It's not quite the syntax of C# and VB, but it's close enough for me.

I got the Nemerle macro via a link on this post: http://groups.google.com/group/nemerle-dev/browse_thread/thread/99b9dcfe204a578e

It should be possible to create a similar macro for Boo. It's quite a bit of work however, given the large set of possible expressions you need to support. Ayende has given a proof of concept here: http://ayende.com/Blog/archive/2008/08/05/Ugly-Linq.aspx

Leyden answered 25/10, 2008 at 1:12 Comment(1)
Nemerle supports Linq expression like C# and even more: code.google.com/p/nemerle/source/browse/nemerle/trunk/Linq . For example supporting Nemerle tuples with Linq: def xs = [(1,1,"aaa"), (2,2,"aaa")]; def xs = xs.AsQueryable(); def res = linq <# from p in xs select p[0] #>.NToList();Beriberi

© 2022 - 2024 — McMap. All rights reserved.