<div dir="ltr">Hey Ben, this is probably a better topic for python-ideas. I'll warn you that a hurdle for ideas like this is that ideally you don't want to support this just for CPython. It's definitely cool though! (Using movie poster style quotes you can turn this into a ringing endorsement: "definitely cool" -- The BDFL. :-)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 20, 2015 at 7:26 PM, Ben Hoyt <span dir="ltr"><<a href="mailto:benhoyt@gmail.com" target="_blank">benhoyt@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Python devs,<br>
<br>
Enabling access to the AST for compiled code would make some cool<br>
things possible (C# LINQ-style ORMs, for example), and not knowing too<br>
much about this part of Python internals, I'm wondering how possible<br>
and practical this would be.<br>
<br>
Context: PonyORM (<a href="http://ponyorm.com/" target="_blank">http://ponyorm.com/</a>) allows you to write regular<br>
Python generator expressions like this:<br>
<br>
    select(c for c in Customer if sum(c.orders.price) > 1000)<br>
<br>
which compile into and run SQL like this:<br>
<br>
    SELECT "c"."id"<br>
    FROM "Customer" "c"<br>
    LEFT JOIN "Order" "order-1" ON "c"."id" = "order-1"."customer"<br>
    GROUP BY "c"."id"<br>
    HAVING coalesce(SUM("order-1"."total_price"), 0) > 1000<br>
<br>
I think the Pythonic syntax here is beautiful. But the tricks PonyORM<br>
has to go to get it are ... not quite so beautiful. Because the AST is<br>
not available, PonyORM decompiles Python bytecode into an AST first,<br>
and then converts that to SQL. (More details on all that from author's<br>
EuroPython talk at <a href="http://pyvideo.org/video/2968" target="_blank">http://pyvideo.org/video/2968</a>)<br>
<br>
I believe PonyORM needs the AST just for generator expressions and<br>
lambda functions, but obviously if this kind of AST access feature<br>
were in Python it'd probably be more general.<br>
<br>
I believe C#'s LINQ provides something similar, where if you're<br>
developing a LINQ converter library (say LINQ to SQL), you essentially<br>
get the AST of the code ("expression tree") and the library can do<br>
what it wants with that.<br>
<br>
What would it take to enable this kind of AST access in Python? Is it<br>
possible? Is it a good idea?<br>
<br>
-Ben<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>