Linq to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hrishy

    Linq to Python

    Hi

    Will LINQ be ported to Python ?

    regards
    Hrishy



  • Diez B. Roggisch

    #2
    Re: Linq to Python

    hrishy wrote:
    Hi
    >
    Will LINQ be ported to Python ?
    Take a look at SQLAlchemy or SQLObject for python-based
    ORM/SQL-abstractions.

    Apart from that, python is already heavily based on concepts like iterators,
    filtering. Take a look at itertools.

    Diez

    Comment

    • Jason Scheirer

      #3
      Re: Linq to Python

      On Sep 23, 7:48 am, hrishy <hris...@yahoo. co.ukwrote:
      Hi
      >
      Will LINQ be ported to Python ?
      >
      regards
      Hrishy
      I think this question is more appropriate to ask on an IronPython
      development list -- LINQ is pretty solidly intertwined with .Net, and
      so you'll likely want to look at the .Net implementation of Python.

      Comment

      • namekuseijin

        #4
        Re: Linq to Python

        On Sep 23, 2:07 pm, Jason Scheirer <jason.schei... @gmail.comwrote :
        On Sep 23, 7:48 am, hrishy <hris...@yahoo. co.ukwrote:
        >
        Hi
        >
        Will LINQ be ported to Python ?
        >
        regards
        Hrishy
        >
        I think this question is more appropriate to ask on an IronPython
        development list -- LINQ is pretty solidly intertwined with .Net, and
        so you'll likely want to look at the .Net implementation of Python.
        But surely the idea behind it will eventually spread. It's really
        just comprehensions generalized over XML and relational datasets, a
        noble goal. Besides, it's main purpose for .NET was to bring
        functional programming to it. Python already has that, somewhat...

        Comment

        • Thomas G. Willis

          #5
          Re: Linq to Python

          But surely the idea behind it will eventually spread.  It's really
          just comprehensions generalized over XML and relational datasets, a
          noble goal.  Besides, it's main purpose for .NET was to bring
          functional programming to it.  Python already has that, somewhat...
          it's really any object out of the box, i think the sql linq stuff is
          more of a query compiler, IMO sqlalchemy does that.


          query = select(user_col s,

          and_(table_rela tionship.c.acce pt_user_id==use r.id,

          table_relations hip.c.start_dat e==None

          ),

          from_obj=join(

          table_relations hip,table_user,

          onclause=table_ user.c.id==tabl e_relationship. c.init_user_id

          ).outerjoin(tab le_profile)

          )

          session.execute (query).fetchal l()






          XML? meh hopefully I would never need it. :)


          C# is my day job, and when I got my hands on LINQ back in January my
          initial thought was "Finally I have list comprehensions! !!! day job is
          fun again"

          For the most part, I think C# is catching up.

          Comment

          • sturlamolden

            #6
            Re: Linq to Python

            On Sep 23, 4:48 pm, hrishy <hris...@yahoo. co.ukwrote:
            Will LINQ be ported to Python ?
            No, because Python already has list comprehensions and we don't need
            the XML buzzword.

            Comment

            • bearophileHUGS@lycos.com

              #7
              Re: Linq to Python

              sturlamolden:
              >No, because Python already has list comprehensions and we don't need the XML buzzword.<
              LINQ is more than buzzwords. Python misses several of those features.
              So maybe for once the Python crowd may recognize such C# feature as
              much better than things present in Python.
              Said that, I presume Python will go on as usual, and LINQ-like
              capabilities will not be integrated in Python. In the meantime where I
              live lot of people will keep using C# instead of Python and CLisp,
              natural selection at work indeed.

              Bye,
              bearophile

              Comment

              • Tim Golden

                #8
                Re: Linq to Python

                bearophileHUGS@ lycos.com wrote:
                sturlamolden:
                >No, because Python already has list comprehensions and we don't need the XML buzzword.<
                >
                LINQ is more than buzzwords. Python misses several of those features.
                So maybe for once the Python crowd may recognize such C# feature as
                much better than things present in Python.
                Said that, I presume Python will go on as usual, and LINQ-like
                capabilities will not be integrated in Python. In the meantime where I
                live lot of people will keep using C# instead of Python and CLisp,
                natural selection at work indeed.
                Perhaps a quick summary of what LINQ offers which might
                "be integrated into Python" would help those of us who
                are ignorant? (This is a serious comment; I'd like to know).

                TJG

                Comment

                • Chris Mellon

                  #9
                  Re: Linq to Python

                  On Wed, Sep 24, 2008 at 2:11 PM, <bearophileHUGS @lycos.comwrote :
                  sturlamolden:
                  >>No, because Python already has list comprehensions and we don't need the XML buzzword.<
                  >
                  LINQ is more than buzzwords. Python misses several of those features.
                  So maybe for once the Python crowd may recognize such C# feature as
                  much better than things present in Python.
                  Said that, I presume Python will go on as usual, and LINQ-like
                  capabilities will not be integrated in Python. In the meantime where I
                  live lot of people will keep using C# instead of Python and CLisp,
                  natural selection at work indeed.
                  >
                  Why do you swing so widely between being an interesting poster with
                  interesting, useful things to say and mindless trolling? There's a lot
                  of reasons to use C# instead of Python or Lisp (or any other language
                  for that matter), but I can't imagine that someone would make that
                  decision based solely on LINQ. I'd go so far as to say that if they
                  do, I question their professional competence.

                  LINQ is an interesting implementation of an old idea. The place where
                  it differs from list comps is that it has appropriate hooks for the
                  specific linq implementation to override the way results are gathered,
                  and such hooks in Python would be an interesting idea. I've taken a
                  shot and figuring out where and how they should be implemented, but I
                  haven't come up with anything I like. Maybe you should try it?

                  LINQs "native" object API (not the syntax sugar available in C#) is
                  not very different from Python ORMs.

                  Comment

                  • r0g

                    #10
                    Re: Linq to Python

                    bearophileHUGS@ lycos.com wrote:
                    sturlamolden:
                    >No, because Python already has list comprehensions and we don't need the XML buzzword.<
                    >
                    LINQ is more than buzzwords. Python misses several of those features.
                    So maybe for once the Python crowd may recognize such C# feature as
                    much better than things present in Python.
                    Said that, I presume Python will go on as usual, and LINQ-like
                    capabilities will not be integrated in Python. In the meantime where I
                    live lot of people will keep using C# instead of Python and CLisp,
                    natural selection at work indeed.
                    >
                    Bye,
                    bearophile
                    LOL, I just read that and thought - Ok this sounds serious I'd better go
                    find out what this LINQ business is all about so I googled.. and ended
                    up on MSDN where there's impressive sounding talk about how we need a
                    way to query databases and XML files with a unified syntax like we do
                    for for standard datatypes like files and arrays. Well yes,that makes
                    sense I think and proceed to look at their example code, curious as to
                    what this new paradigm looks like:

                    using System;
                    using System.Linq;
                    using System.Collecti ons.Generic;

                    class app {
                    static void Main() {
                    string[] names = { "Burke", "Connor", "Frank",
                    "Everett", "Albert", "George",
                    "Harris", "David" };

                    IEnumerable<str ingquery = from s in names
                    where s.Length == 5
                    orderby s
                    select s.ToUpper();

                    foreach (string item in query)
                    Console.WriteLi ne(item);
                    }
                    }

                    ROTFLMAO! Wow, what progress they're making! Quick guys let's jump on
                    before we get left behind - we dont want to miss out on this exciting
                    and mysterious 'foreach' construct or this strange and exotic sounding
                    'IEnumerable query' thing. To think that python might someday reach such
                    lofty heights where we'll be able to simply write...

                    names = ["Burke", "Connor", "Frank", "Everett",
                    "Albert", "George", "Harris", "David"]

                    result = [each.upper() for each in names if len(each) == 5]

                    result.sort()

                    for each in result: print each


                    Yes clearly 'the Python crowd' must admit LINQ is 'much better', I'm
                    sold, in fact off to download my "Free, but limited editions of Visual
                    Studio 2005 for a single programming language supported by .NET" right away!

                    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?


                    Roger Heathcote.

                    Comment

                    • sturlamolden

                      #11
                      Re: Linq to Python

                      On Sep 24, 9:11 pm, bearophileH...@ lycos.com wrote:
                      In the meantime where I
                      live lot of people will keep using C# instead of Python and CLisp,
                      natural selection at work indeed.
                      Please explain to me what Linq can do that Python does not. Put you
                      emphasis on why this can't be done with a library, and thus will
                      require addition of new syntax to the language.

                      Comment

                      • Duncan Booth

                        #12
                        Re: Linq to Python

                        r0g <aioe.org@techn icalbloke.comwr ote:
                        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<Customercu stomers = GetCustomerList ();

                        var customerOrderGr oups =
                        from c in customers
                        select
                        new {c.CompanyName,
                        YearGroups =
                        from o in c.Orders
                        group o by o.OrderDate.Yea r into yg
                        select
                        new {Year = yg.Key,
                        MonthGroups =
                        from o in yg
                        group o by o.OrderDate.Mon th into mg
                        select new { Month = mg.Key, Orders = mg }
                        }
                        };

                        ObjectDumper.Wr ite(customerOrd erGroups, 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.GroupB y(
                        w =w.Trim(),
                        a =a.ToUpper(),
                        new AnagramEquality Comparer()
                        );

                        ObjectDumper.Wr ite(orderGroups , 1);
                        }

                        public class AnagramEquality Comparer : IEqualityCompar er<string>
                        {
                        public bool Equals(string x, string y) {
                        return getCanonicalStr ing(x) == getCanonicalStr ing(y);
                        }

                        public int GetHashCode(str ing obj) {
                        return getCanonicalStr ing(obj).GetHas hCode();
                        }

                        private string getCanonicalStr ing(string word) {
                        char[] wordChars = word.ToCharArra y();
                        Array.Sort<char >(wordChars);
                        return new string(wordChar s);
                        }
                        }

                        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.

                        Comment

                        • sturlamolden

                          #13
                          Re: Linq to Python

                          On Sep 24, 10:59 pm, Duncan Booth <duncan.bo...@i nvalid.invalid>
                          wrote:
                          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.
                          han extensions to syntax.
                          List comprehensions work with any iterable sequence. You can nest them
                          to make more complex statements. You can also use a generator to
                          iterate through a database or an XML document. Here is approximately
                          where linq stops being a marvelous addition to Python.

                          And I can honestly do without the SQL syntax.





                          Comment

                          • Thomas G. Willis

                            #14
                            Re: Linq to Python

                            On Sep 24, 4:59 pm, Duncan Booth <duncan.bo...@i nvalid.invalidw rote:
                            ....
                            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.
                            My thoughts exactly when I first looked at it. "Hey C# NOW has list
                            comprehensions! !! SWEET!!!"

                            As I understand it LINQ is libraries(Syste m.Data.Linq.dll ,
                            System.XML.Linq .dll) + syntactic sugar, i wouldn't call that a change
                            to the language. The addition of lambdas and functions as first class
                            objects however, that was indeed a language change, and it was a
                            change for the better that makes LINQ possible, (also makes writing
                            delegates less cumbersome). To someone other than a C# person, these
                            features are not new or revolutionary, it's just C# catching up. Kudos
                            to them for getting there before java.

                            After some more thought on what might conceivably be missing from
                            python that LINQ has, that someone might want is the equivalent of
                            System.XML.Linq .dll, and I can't see any reason why someone couldn't
                            write it with the exception that there doesn't seem to be a definitive
                            XML lib for python, meaning generally regarded as the best. That in my
                            mind would be a prerequisite. But if someone wrote both the XML lib
                            with the LINQ-ish syntax, maybe it would become the definitive XML
                            lib.

                            Again, I personally don't see myself using XML and thus needing that
                            unless my hand was forced by some business requirement that dictated I
                            use XML, I would not use it if I had a choice. The SQL LiNQ-ish
                            though, I think every python ORM has that covered to a degree, and I
                            use that quite a bit.



                            Comment

                            • r0g

                              #15
                              Re: Linq to Python

                              Duncan Booth wrote:
                              r0g <aioe.org@techn icalbloke.comwr ote:
                              >
                              >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.
                              Hmm, that's a nice idea in theory but I don't think it's the python
                              killer Mr/Ms bearophile thinks it is. I can't think of any use cases
                              where this would save me a great deal of time (certainly not enough to
                              offset the relative slowness of working in C#) but supposing they do
                              exist this stuff definitely belongs in a module. Seems to me C# is
                              playing catchup in most ways and this nugget of 'innovation' is just the
                              exception that proves the rule.

                              Roger.

                              Comment

                              Working...