Re: Linq to Python



r0g <aioe.org@xxxxxxxxxxxxxxxxxx> wrote:

OK so maybe I'm being naive here but it looks to me like this new
paradigm's big idea is to use a python + SQL type syntax to access data
in random objects. Big whoop. It's not that difficult to write a
generators that wraps XML files and databases is it?

What am I missing here?

Simple LINQ expressions like the one you gave map easily to Python list
comprehensions. What Microsoft have done though is provide a consistent
implementation which allows you to write complex SQL like expressions which
will work identically on databases or most other sequence types.

Here's another LINQ example:

List<Customer> customers = GetCustomerList();

var customerOrderGroups =
from c in customers
select
new {c.CompanyName,
YearGroups =
from o in c.Orders
group o by o.OrderDate.Year into yg
select
new {Year = yg.Key,
MonthGroups =
from o in yg
group o by o.OrderDate.Month into mg
select new { Month = mg.Key, Orders = mg }
}
};

ObjectDumper.Write(customerOrderGroups, 3);

I'm not overly keen on SQL syntax: I think this sort of complex expression
is hard to read. LINQ has a pretty straightforward conversion to method
calls, and for me this consistent set of methods is the important thing
rather than the SQL syntax. e.g. 'group by' maps directly to a call to a
method GroupBy(), so another of Microsoft's LINQ examples is:

public void Linq45() {
string[] anagrams = {"from ", " salt", " earn ", " last ",
" near ", " form "};

var orderGroups = anagrams.GroupBy(
w => w.Trim(),
a => a.ToUpper(),
new AnagramEqualityComparer()
);

ObjectDumper.Write(orderGroups, 1);
}

public class AnagramEqualityComparer : IEqualityComparer<string>
{
public bool Equals(string x, string y) {
return getCanonicalString(x) == getCanonicalString(y);
}

public int GetHashCode(string obj) {
return getCanonicalString(obj).GetHashCode();
}

private string getCanonicalString(string word) {
char[] wordChars = word.ToCharArray();
Array.Sort<char>(wordChars);
return new string(wordChars);
}
}

Python still wins hands down on this example both in verbosity and
readability:

anagrams = ["from ", " salt", " earn ", " last ",
" near ", " form "]
from itertools import groupby
def AnagramKey(w):
return sorted(w.strip().upper())

for k,words in groupby(sorted(anagrams, key=AnagramKey),
key=AnagramKey):
for w in words:
print w.strip().upper()
print "..."


EARN
NEAR
....
SALT
LAST
....
FROM
FORM
....

I haven't yet had occasion to use LINQ in anger yet, so I have no idea
whether its an idea to love or to hate. I do think it is good that C# has
effectively sprouted list comprehensions (not to mention anonymous types
and type inferencing) and I expect there may be some aspects worth looking
at for Python but I think they are more likely to lead to itertools
functions than extensions to syntax.
.



Relevant Pages

  • Re: SQL, lite lite lite
    ...  'sqllite3' statements are valid SQL expressions, ... how and whether it can include arbitrary Python ... whether to permit transactions; and what the simplest and coolest ... thing you can do with a little Python syntax is. ...
    (comp.lang.python)
  • SQL, lite lite lite
    ... There are some options, such as 'sqllite3', but they are not ... 'sqllite3' statements are valid SQL expressions, ... how and whether it can include arbitrary Python ... thing you can do with a little Python syntax is. ...
    (comp.lang.python)
  • Re: To LinQ or not LinQ
    ... the context, it should be clear this means the same thing that Jani said, i.e. "the SQL-like" syntax. ... speaking of SQL as a language and Linq as a language, ... that the languages differ in meaning even when they superficially look ... perhaps I should stop warning people that Linq queries aren't sql queries and shouldn't be seen as such, and just let them plow on and wondering why their code is so dogslow in production. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: To LinQ or not LinQ
    ... i.e. "the SQL-like" syntax. ... with 'sql like syntax', i.e.: not only do the words look the same, hte ... speaking of SQL as a language and Linq as a language, ... tiny differences and it's up to semantics (I'm not a native english ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [OT] C# 3.0 - ein Vorbild =?ISO-8859-15?Q?f=FCr_Java_7=3F?=
    ... dass man die Syntax nicht beliebig gross ... du denkst hier nur an SQL. ... Doch LINQ ... Es bringt ein set neuer Verben in die Sprache, die IMHO längst ...
    (de.comp.lang.java)