Can you do LINQ-like queries in Python or Boo?

Take this simple C # LINQ query and imagine that 'db.Numbers' is a single column SQL table, Number:

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

This will work very efficiently in C # because it generates an SQL query, for example, "select Number from Numbers where Number <5". What he does not do is to select all the numbers from the database and then filter them in C #, as it might seem at first.

Python supports a similar syntax:

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

But this "if" clause here performs filtering on the client side, and not on the server side, which is much less efficient.

Is there something as effective as LINQ in Python? (I am currently evaluating Python vs IronPython vs Boo, so an answer that works in any of these languages ​​is fine.)

+4
source share
6 answers

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

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

Something better could have turned into IronPython 2.0b4 - there has been a lot of ongoing discussion about how name conflicts were handled.

+4
source

sqlsoup in sqlalchemy gives you the fastest solution in python, I think if you want a clear (ish) single 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()] 
+6
source

Take a close look at SQLAlchemy . This can probably do a lot of what you want. It gives you Python syntax for a regular SQL server that runs on the server.

+5
source

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

Expression trees are slightly different in that they are not special syntax. They are written in the same way as any other class instances, but the compiler really treats them specifically under covers, turning the lambda into the creation of a temporary tree of the abstract syntax tree . They can be manipulated at run time to create a command in another language (i.e. SQL).

C # and VB.NET compilers take the LINQ syntax and turn it into lambdas, and then pass them into instances of the expression tree. Then there are a bunch of framework classes that manipulate these trees to create SQL. You can also find other libraries produced by MS as well as third parties that offer "LINQ providers" that mainly use a different AST handler to create something from LINQ other than SQL.

Thus, one of the obstacles to performing these actions in another language is the question of whether they support the building of AST / manipulation at run time. I don't know if any Python or Boo implementations are running, but I have not heard of such functions.

+5
source

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

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

It should be possible to create a similar macro for Boo. However, this is quite a bit of work, given the large set of possible expressions that need to be supported. Ayende gave a proof of concept here: http://ayende.com/Blog/archive/2008/08/05/Ugly-Linq.aspx

+1
source

Boo supports list generator expressions using the same syntax as python. For more information about this, check out the Boo documentation on generator expressions and List Accounting .

+1
source

Source: https://habr.com/ru/post/1277042/


All Articles