Re: Linq to Python

2008-10-27 Thread Tim Rowe
2008/9/24 Duncan Booth [EMAIL PROTECTED]:


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

But AFAICS, the Python version you give creates a temporary. One of
the advantages cited for LINQs functional programming paradigm is that
it specifies what is wanted at a higher level, so the compiler can
decide whether to create temporaries, and can also decide whether to
farm the thing off to multiple processors -- harder if you've
specified in detail /how/ to do the job. Not an issue for little jobs,
but certainly an issue for, for example, a friend who had a daily
database job to do that took over 24 hours to run.

 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.

Yes, looking at what LINQ adds to C# (according to
http://msdn.microsoft.com/en-gb/library/bb397909.aspx):
- Implicitly typed variables: Python already has.
- Object and collection initialisers: Not sure whether Python can do
this directly, but it can certainly emulate it with a dictionary.
- Anonymous types: Not sure whether Python can do this directly, but
it can certainly emulate it with a dictionary.
- Extension methods: Python already has.
- Lambda expressions: Python already has.
- Auto-Implemented properties: No, but that's just syntactic sugar to
make declarations more compact.

So all of the language elements that are needed for LINQ are already
in Python; a library should do the trick.

-- 
Tim Rowe
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-10-27 Thread Duncan Booth
Tim Rowe [EMAIL PROTECTED] wrote:

 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.
 
 Yes, looking at what LINQ adds to C# (according to
 http://msdn.microsoft.com/en-gb/library/bb397909.aspx):
 - Implicitly typed variables: Python already has.
 - Object and collection initialisers: Not sure whether Python can do
 this directly, but it can certainly emulate it with a dictionary.
 - Anonymous types: Not sure whether Python can do this directly, but
 it can certainly emulate it with a dictionary.
 - Extension methods: Python already has.
 - Lambda expressions: Python already has.
 - Auto-Implemented properties: No, but that's just syntactic sugar to
 make declarations more compact.
 
 So all of the language elements that are needed for LINQ are already
 in Python; a library should do the trick.
 
Not quite. The C# implementation of lambda expressions has a neat trick 
that is central to LINQ. A lambda expression compiles either to executable 
code, or to an expression tree. If you are filtering some C# sequence 
object then (as with Python lambdas) the LINQ code simply calls the 
compiled lambda expression like any other delegate, but if you are 
filtering a SQL lookup the LINQ code gets an expression syntax tree and 
further compiles it (at runtime) into SQL code to perform the filtering at 
the database level.

Python can't do the compile time matching to vary what it produces 
according to how the lambda is used, but it would be perfectly possible for 
function objects which are compiled from a lambda expression to grow an 
extra attribute that would hold an AST for the expression. That way you 
could write code that took a filter function and, if it had the AST 
attribute available, compiled it to SQL or xpath or whatever else you 
fancied.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread Duncan Booth
sturlamolden [EMAIL PROTECTED] wrote:

 On Sep 24, 10:59 pm, Duncan Booth [EMAIL PROTECTED]
 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 whi 
 ch
 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.
 
A lot of what LINQ does is already easy to do in Python, and most of the 
rest can probably be added fairly easily, but it does provide a consistent 
framework which may make it easier to do complex LINQ statements than 
complex list comprehensions.

BTW, a minor correction: LINQ statements are closer to generators, not list 
comprehensions. They don't actually evaluate their results until you 
iterate over them and you can re-used the same LINQ statement multiple 
times getting different results if the data has changed.

 And I can honestly do without the SQL syntax.
 
snap :)

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread sturlamolden
On 25 Sep, 10:08, Duncan Booth [EMAIL PROTECTED] wrote:

 A lot of what LINQ does is already easy to do in Python, and most of the
 rest can probably be added fairly easily, but it does provide a consistent
 framework which may make it easier to do complex LINQ statements than
 complex list comprehensions.

Yes, that's the word, consistent framework. I wonder what that
means? Do you mean Python syntax is inconsitent?


 BTW, a minor correction: LINQ statements are closer to generators, not list
 comprehensions. They don't actually evaluate their results until you
 iterate over them and you can re-used the same LINQ statement multiple
 times getting different results if the data has changed.

Python has generator expressions with the same syntax as list
comprehensions, except you use () instead of [].







--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread hrishy
Hi

If i rephrase my question how will i do this in Python

http://informationr.net/ir/13-2/TB0806.html

Watch this query on the page Where he joins all different kind of things with 
ease and elegance(as per my opinion)

[code]
var stoogeGuys = 
 Beginning with the XML source
 from xmlGuys in xmlSource.Descendants(Stooge)
 Join to the array on the common element stoogeName
 join arrayGuys in familyFacts 
   on xmlGuys.Element(stoogeName).Value equals arrayGuys.stoogeName
 Join to the database on the common element stoogeName
 join dbGuys in stoogeContext.stoogeTables 
   on xmlGuys.Element(stoogeName).Value equals dbGuys.stoogeName 
 select new
 {
firstName= dbGuys.stoogeName,
familyName   = arrayGuys.familyName,
birthDate= xmlGuys.Element(birthDate).Value,
deathDate= xmlGuys.Element(deathDate).Value,
hairCutStyle = dbGuys.stoogeHaircut,
 };
[/code]

regards
Hrishy


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread hrishy
Hi Grant

haha :-) i discounted that perspective :-)

regards
Hrishy


--- On Thu, 25/9/08, Grant Edwards [EMAIL PROTECTED] wrote:

 From: Grant Edwards [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Thursday, 25 September, 2008, 2:22 AM
 On 2008-09-24, Bruno Desthuilliers
 [EMAIL PROTECTED] wrote:
  hrishy a écrit :
  (snip)
 
 
  I apologise
  (I thought Python programmers were smart and they
 did know what LINQ was)
 
  Is there really any relation between being
 smart and knowing anything 
  about the latest MS fad ?
 
 God, I hope not -- or I'd rather be stupid.
 
 -- 
 Grant
 
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread hrishy
Hi Roger

I am impressed (i always suspected Python programmers are smart no doubt about 
it).

But what about the case where they join different sources like the one here

http://informationr.net/ir/13-2/TB0806.html

Thanks for teaching me :-) i am thankful for that

regards
Hrishy




 
 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.
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread sturlamolden
On 25 Sep, 12:06, hrishy [EMAIL PROTECTED] wrote:

 var stoogeGuys =
      Beginning with the XML source
      from xmlGuys in xmlSource.Descendants(Stooge)
      Join to the array on the common element stoogeName
      join arrayGuys in familyFacts
            on xmlGuys.Element(stoogeName).Value equals arrayGuys.stoogeName
      Join to the database on the common element stoogeName
      join dbGuys in stoogeContext.stoogeTables
            on xmlGuys.Element(stoogeName).Value equals dbGuys.stoogeName
      select new
      {
         firstName    = dbGuys.stoogeName,
         familyName   = arrayGuys.familyName,
         birthDate    = xmlGuys.Element(birthDate).Value,
         deathDate    = xmlGuys.Element(deathDate).Value,
         hairCutStyle = dbGuys.stoogeHaircut,
      };
 [/code]


That is a for loop over xmlGuys in xmlSource.Descendants(Stooge).
Those joins are e.g. dictionary lookups, and finally an object with
names, birthdates, etc. are appended to the list stoogeGuys.






--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread hrishy
Hi Tim

I am not a LINQ expert just a LINQ user and (was a little envious why the 
langauge i fantasize doesnt have it (pardon my ignorance of python))

LINQ as far as i know allows you to query all sources using a consistent 
interface .

You can query a message queue ,xml document ,array object or relational source 
by learning LINQ and even join them as illustrated below

http://informationr.net/ir/13-2/TB0806.html

If somebody can tutor me how i can do that in python that would be great (and 
mayeb satisfy my greed and leave me with a happy feeling that my langauge 
python can do it)

regards
Hrishy


--- On Wed, 24/9/08, Tim Golden [EMAIL PROTECTED] wrote:

 From: Tim Golden [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: 
 Cc: python-list@python.org
 Date: Wednesday, 24 September, 2008, 8:20 PM
 [EMAIL PROTECTED] 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
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread sturlamolden
On 25 Sep, 12:06, hrishy [EMAIL PROTECTED] wrote:

 [code]
 var stoogeGuys =
      Beginning with the XML source
      from xmlGuys in xmlSource.Descendants(Stooge)
      Join to the array on the common element stoogeName
      join arrayGuys in familyFacts
            on xmlGuys.Element(stoogeName).Value equals arrayGuys.stoogeName
      Join to the database on the common element stoogeName
      join dbGuys in stoogeContext.stoogeTables
            on xmlGuys.Element(stoogeName).Value equals dbGuys.stoogeName
      select new
      {
         firstName    = dbGuys.stoogeName,
         familyName   = arrayGuys.familyName,
         birthDate    = xmlGuys.Element(birthDate).Value,
         deathDate    = xmlGuys.Element(deathDate).Value,
         hairCutStyle = dbGuys.stoogeHaircut,
      };
 [/code]

It could e.g. look like this in Python:

stoogeGuys = []
for xmlGuys in xmlSource.Descendants[Stooge]:
arrayGuys = familyFacts[xmlGuys.stoogeName]
dbGuys = stoogeContext.stoogeTables[xmlGuys.stoogeName]
stoogeGuys += \
   [{'firstName':dbGuys.stoogeName,
 'familyName':   arrayGuys.familyName,
 'birthDate':xmlGuys.birthDate,
 'deathDate':dbGuys.deathDate,
 'hairCutStyle': dbGuys.stoogeHaircut}]

--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread hrishy
Hi

Pardon my ignorance again but id ont see any join in python or did i miss 
something ?

regards
Hrishy


--- On Thu, 25/9/08, sturlamolden [EMAIL PROTECTED] wrote:

 From: sturlamolden [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Thursday, 25 September, 2008, 12:02 PM
 On 25 Sep, 12:06, hrishy [EMAIL PROTECTED] wrote:
 
  [code]
  var stoogeGuys =
       Beginning with the XML source
       from xmlGuys in
 xmlSource.Descendants(Stooge)
       Join to the array on the common element
 stoogeName
       join arrayGuys in familyFacts
             on
 xmlGuys.Element(stoogeName).Value equals
 arrayGuys.stoogeName
       Join to the database on the common element
 stoogeName
       join dbGuys in stoogeContext.stoogeTables
             on
 xmlGuys.Element(stoogeName).Value equals
 dbGuys.stoogeName
       select new
       {
          firstName    = dbGuys.stoogeName,
          familyName   = arrayGuys.familyName,
          birthDate    =
 xmlGuys.Element(birthDate).Value,
          deathDate    =
 xmlGuys.Element(deathDate).Value,
          hairCutStyle = dbGuys.stoogeHaircut,
       };
  [/code]
 
 It could e.g. look like this in Python:
 
 stoogeGuys = []
 for xmlGuys in xmlSource.Descendants[Stooge]:
 arrayGuys = familyFacts[xmlGuys.stoogeName]
 dbGuys = stoogeContext.stoogeTables[xmlGuys.stoogeName]
 stoogeGuys += \
[{'firstName':dbGuys.stoogeName,
  'familyName':   arrayGuys.familyName,
  'birthDate':xmlGuys.birthDate,
  'deathDate':dbGuys.deathDate,
  'hairCutStyle': dbGuys.stoogeHaircut}]
 
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread sturlamolden
On 25 Sep, 13:08, hrishy [EMAIL PROTECTED] wrote:

 Pardon my ignorance again but id ont see any join in python or did i miss 
 something ?

It's more Pythonic to use the syntax of dictionary lookups.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread hrishy
Hi

Thank you very much I appreciate taking the pain to explain this to me.

regards
Hrishy


--- On Thu, 25/9/08, sturlamolden [EMAIL PROTECTED] wrote:

 From: sturlamolden [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Thursday, 25 September, 2008, 12:16 PM
 On 25 Sep, 13:08, hrishy [EMAIL PROTECTED] wrote:
 
  Pardon my ignorance again but id ont see any join in
 python or did i miss something ?
 
 It's more Pythonic to use the syntax of dictionary
 lookups.
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread Duncan Booth
sturlamolden [EMAIL PROTECTED] wrote:

 On 25 Sep, 10:08, Duncan Booth [EMAIL PROTECTED] wrote:
 
 A lot of what LINQ does is already easy to do in Python, and most of
 the rest can probably be added fairly easily, but it does provide a
 consistent framework which may make it easier to do complex LINQ
 statements than complex list comprehensions.
 
 Yes, that's the word, consistent framework. I wonder what that
 means? Do you mean Python syntax is inconsitent?

No Python's syntax is fine. The api's aren't as consistent though: 
Microsoft added a common set of extension methods which work on 
databases, xml, builtin sequences and can be easily extended to include 
other custom sequences.

As an example to filter a list in Python you'd use a list comprehension 
with an 'if', but for a database you'd probably prefer a select with a 
'where' clause so as to avoid retrieving and discarding 99 or your 1 
million rows. The apis defined by LINQ allow that sort of optimisation 
to happen transparently, the simple list would just test each element 
but the database would run an appropriate query.

So what the 'can we have LINQ in Python' people are asking is to be able 
to write things like:

   x = (c for c in customers if c.id=='123')

and know that they aren't doing a linear search unless that is the best 
that can be done. The LINQ equivalent would be something like:

  var x = from c in customers where c.id=='123'
  select new { c.name, c.id };

which is compiled to:

  var x = customers.Where(c = c.id=='123');

and depending on the type of 'customers' the Where method can either get 
a callable function to test the condition or an expression tree which it 
can compile into another language such as SQL (C# lambdas can compile 
either to executable code or to Expression objects that you can 
further process).

There's an article at 
http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees which 
has a real example showing how:

DataContext db = new DataContext(server=.;initial catalog=northwind);
TableOrders orders = db.GetTableOrders();
TableCustomers customers = db.GetTableCustomers();

var q = from o in orders, c in customers
where o.ShipCity == London  (o.CustomerID == c.CustomerID)
select new { o.OrderDate, c.CompanyName, c.ContactTitle,
 c.ContactName };

actually ends up as a single SQL query:

exec sp_executesql N'SELECT [t1].[CompanyName], [t1].[ContactName], 
[t1].[ContactTitle], [t0].[OrderDate]
FROM [Orders] AS [t0], [Customers] AS [t1]
WHERE ([t0].[ShipCity] = @p0) AND ([t0].[CustomerID] = [t1].
[CustomerID])', N'@p0 nvarchar(6)', @p0 = N'London' 


 BTW, a minor correction: LINQ statements are closer to generators,
 not list comprehensions. They don't actually evaluate their results
 until you iterate over them and you can re-used the same LINQ
 statement multiple times getting different results if the data has
 changed. 
 
 Python has generator expressions with the same syntax as list
 comprehensions, except you use () instead of [].
 
It might surprise you to know that I have actually come across generator 
expressions. :^)

My wording was deliberate: LINQ queries are re-usable, so are Python's 
generators, but generator expressions are not. The comparison isn't 
exact, you have to call the generator to get an iterator whereas a LINQ 
expression gives you something which is directly iterable.

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread sturlamolden
On 25 Sep, 14:22, Duncan Booth [EMAIL PROTECTED] wrote:

 No Python's syntax is fine. The api's aren't as consistent though:
 Microsoft added a common set of extension methods which work on
 databases, xml, builtin sequences and can be easily extended to include
 other custom sequences.

That is correct, but it is a library issue and cannot be solved by
adding new syntax.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-25 Thread bearophileHUGS
Duncan Booth:
 Microsoft added a common set of extension methods which work on
 databases, xml, builtin sequences and can be easily extended to include
 other custom sequences.

When the processing is done in memory, LINQ may also work well with
multi-core CPUs, see PLINQ.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread hrishy
Hi Terry

Oops i never realised the mistake i have commited

I apologise 
(i thought changing the subject line would make a new thread)
I apologise
(I thought Python programmers were smart and they did know what LINQ was)
I don't apologise
( i dont apologise for the third one not sounding cocky here but i did google 
but nothing much came up thats when i posted the question here since i always 
see Python programmers are somehow smarter then programmers in other langauges 
i don't know if its the language or the programmers themselves that make them 
smart)

regards
Hrishy




--- On Tue, 23/9/08, Terry Reedy [EMAIL PROTECTED] wrote:

 From: Terry Reedy [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Tuesday, 23 September, 2008, 7:51 PM
  Will LINQ be ported to Python ?
 
 I have three suggestions:
 
 1. When starting a new thread, start a *new* thread. 
 Don't tack a new, 
 unrelated subject onto an existing thread.  Your post will
 not be seen 
 by people with readers that collapse thread and who do not
 happen to 
 read the 'Python is slow?' thread.
 
 2. Also, give enough informaton that people can understand
 your question 
 without searching the web and guessing.  In particular,
 that do *you* 
 mean by LINQ?  The game?  The .NET component? Or something
 else?
 
 3. Before posting, search the Python manuals or the web a
 bit for an 
 answer.  If you mean the .NET component, googling
 'Python LINQ' should 
 partly answer your question.
 
 tjr
 
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread hrishy
Hi 

Well wouldn't it be a lot easier to query and join a xml source with a 
relational source with LINQ capabilites in Python.

Hmm what am i missing here is there a site that takes all LINQ examples and 
does them using list comprehensions and makes them sound easy ?

wasn't python supposed to make everything easy ?

regards
Hrishy


--- On Tue, 23/9/08, sturlamolden [EMAIL PROTECTED] wrote:

 From: sturlamolden [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Tuesday, 23 September, 2008, 7:49 PM
 On Sep 23, 4:48 pm, hrishy [EMAIL PROTECTED]
 wrote:
 
  Will LINQ be ported to Python ?
 
 No, because Python already has list comprehensions and we
 don't need
 the XML buzzword.
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread hrishy
Hi Tom

This is what i like and feel of the Python programmers smarter then every other 
langauge i know of.

But i am not comfortable with your second statement XML i never need it 
one day everybody would need it.


regards
Hrishy


--- On Tue, 23/9/08, Thomas G. Willis [EMAIL PROTECTED] wrote:

 From: Thomas G. Willis [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Tuesday, 23 September, 2008, 7:45 PM
  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_cols,
 
 and_(table_relationship.c.accept_user_id==user.id,
 
 table_relationship.c.start_date==None
 
 ),
 
 from_obj=join(
 
 table_relationship,table_user,
 

 onclause=table_user.c.id==table_relationship.c.init_user_id
 
 ).outerjoin(table_profile)
 
 )
 
 session.execute(query).fetchall()
 
 
 
 
 
 
 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.
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Thomas G. Willis
On Wed, Sep 24, 2008 at 11:25 AM, hrishy [EMAIL PROTECTED] wrote:

 Hi Tom

 This is what i like and feel of the Python programmers smarter then every
 other langauge i know of.

 But i am not comfortable with your second statement XML i never need it
 one day everybody would need it.


 regards
 Hrishy


Well you may be right which is why I said hopefully one thing I do know
is  that in my work in both .net and java, XML plays a more significant
role. Everyone can probably speculate differently as to why this is. My
personal feeling is that it is because XML is less of a hassle than
building/passing around class hierarchies for application state in various
scenarios.But in python, it is far easier for me to pass around a dict or
some similar structure to get roughly the same effect. So in my mind, XML
solves a problem that is present in both .net and java, but not necessarily
python. In the same way, linq to xml solves the problem of handling xml in a
more convenient way.  In order for it to be useful in python, the problem
that xml solves would have to be present IMO.

That's not to say it's not possible. Someone who needs something like LINQ
to XML but in python could write something probably similar to what
sqlalchemy does and it would require no changes to the python language or
runtime. If it was written then maybe people would invent ways to use XML in
python that are better than what other things do. Hooray for the free market
of ideas. :)


This is all my opinion , I have no idea what the conventional wisdom of the
python community is on XML, but in my experience in other languages, it
seems to be the answer to a question that no one asked, or the wrong answer,
more often than it is the appropriate answer. Maybe it's only that way on
OHIO. :)




-- 
Thomas G. Willis
--
http://mail.python.org/mailman/listinfo/python-list

Re: Linq to Python

2008-09-24 Thread sturlamolden
On Sep 24, 5:22 pm, hrishy [EMAIL PROTECTED] wrote:

 Well wouldn't it be a lot easier to query and join a xml source with a 
 relational source with LINQ capabilites in Python.

 Hmm what am i missing here is there a site that takes all LINQ examples and 
 does them using list comprehensions and makes them sound easy ?

 wasn't python supposed to make everything easy ?


Most Python programmers use other languages as well. I have working
knowledge of C, Matlab, C++, Java, Fortran 95, and C#. Those that
responded to your post know what LINQ are and what LINQ does.

Put simply, Python has dynamic typing, list comprehensions, and is
scriptable (i.e. Python source is structured text). That's why Python
don't
need LINQ and XML the same way as C#.  LINQ and XML is needed because
C#
is a statically typed compiled language. That is:

LINQ: Python has list comprehensions and dynamic typing (C# do not)
XML: Python is structured text (compiled .NET assemblies are not)

Second, minimalistic syntax is a virtue. That is why C is still
around.
Python don't add new syntax sugar every time Redmond invents a new
buzzword. Functions like XML processing are delegated to libraries --
where they belong.

Nobody was saying LINQ is a bad idea for a language like C#.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread bearophileHUGS
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Tim Golden

[EMAIL PROTECTED] 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Chris Mellon
On Wed, Sep 24, 2008 at 2:11 PM,  [EMAIL PROTECTED] 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.


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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread r0g
[EMAIL PROTECTED] 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.Collections.Generic;

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

IEnumerablestring query = from s in names
   where s.Length == 5
   orderby s
   select s.ToUpper();

foreach (string item in query)
  Console.WriteLine(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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Bruno Desthuilliers

hrishy a écrit :
(snip)



I apologise
(I thought Python programmers were smart and they did know what LINQ was)


Is there really any relation between being smart and knowing anything 
about the latest MS fad ?

--
http://mail.python.org/mailman/listinfo/python-list

Re: Linq to Python

2008-09-24 Thread sturlamolden
On Sep 24, 9:11 pm, [EMAIL PROTECTED] 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Duncan Booth
r0g [EMAIL PROTECTED] 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:

   ListCustomer 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 : IEqualityComparerstring
{
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.Sortchar(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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread sturlamolden
On Sep 24, 10:59 pm, Duncan Booth [EMAIL PROTECTED]
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.





--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Thomas G. Willis
On Sep 24, 4:59 pm, Duncan Booth [EMAIL PROTECTED] wrote:
...
 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(System.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.



--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread Grant Edwards
On 2008-09-24, Bruno Desthuilliers [EMAIL PROTECTED] wrote:
 hrishy a écrit :
 (snip)


 I apologise
 (I thought Python programmers were smart and they did know what LINQ was)

 Is there really any relation between being smart and knowing anything 
 about the latest MS fad ?

God, I hope not -- or I'd rather be stupid.

-- 
Grant

--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-24 Thread r0g
Duncan Booth wrote:
 r0g [EMAIL PROTECTED] 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.

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.
--
http://mail.python.org/mailman/listinfo/python-list


Linq to Python

2008-09-23 Thread hrishy
Hi

Will LINQ be ported to Python ?

regards
Hrishy


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread Diez B. Roggisch
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread hrishy
Hi 

Thanks for those links however LINQ seems to be much more then ORM tool it can 
for example join an XML file with a relational datasource or create a XSD 

regards
Hrishy


--- On Tue, 23/9/08, Diez B. Roggisch [EMAIL PROTECTED] wrote:

 From: Diez B. Roggisch [EMAIL PROTECTED]
 Subject: Re: Linq to Python
 To: python-list@python.org
 Date: Tuesday, 23 September, 2008, 4:06 PM
 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
 --
 http://mail.python.org/mailman/listinfo/python-list


  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread Jason Scheirer
On Sep 23, 7:48 am, hrishy [EMAIL PROTECTED] wrote:
 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread namekuseijin
On Sep 23, 2:07 pm, Jason Scheirer [EMAIL PROTECTED] wrote:
 On Sep 23, 7:48 am, hrishy [EMAIL PROTECTED] wrote:

  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...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread Thomas G. Willis

 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_cols,

and_(table_relationship.c.accept_user_id==user.id,

table_relationship.c.start_date==None

),

from_obj=join(

table_relationship,table_user,

onclause=table_user.c.id==table_relationship.c.init_user_id

).outerjoin(table_profile)

)

session.execute(query).fetchall()






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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread sturlamolden
On Sep 23, 4:48 pm, hrishy [EMAIL PROTECTED] wrote:

 Will LINQ be ported to Python ?

No, because Python already has list comprehensions and we don't need
the XML buzzword.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Linq to Python

2008-09-23 Thread Terry Reedy

 Will LINQ be ported to Python ?

I have three suggestions:

1. When starting a new thread, start a *new* thread.  Don't tack a new, 
unrelated subject onto an existing thread.  Your post will not be seen 
by people with readers that collapse thread and who do not happen to 
read the 'Python is slow?' thread.


2. Also, give enough informaton that people can understand your question 
without searching the web and guessing.  In particular, that do *you* 
mean by LINQ?  The game?  The .NET component? Or something else?


3. Before posting, search the Python manuals or the web a bit for an 
answer.  If you mean the .NET component, googling 'Python LINQ' should 
partly answer your question.


tjr

--
http://mail.python.org/mailman/listinfo/python-list