<div dir="ltr"><br><br><div class="gmail_quote">On Thu, May 21, 2015 at 10:10 PM Ben Hoyt <<a href="mailto:benhoyt@gmail.com">benhoyt@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Huh, interesting idea. I've never used import hooks. Looks like the<br>
relevant macropy source code is here:<br>
<br>
<a href="https://github.com/lihaoyi/macropy/blob/master/macropy/core/import_hooks.py" target="_blank">https://github.com/lihaoyi/macropy/blob/master/macropy/core/import_hooks.py</a><br>
<br>
So basically you would do the following:<br>
<br>
1) intercept the import<br>
2) find the source code file yourself and read it<br>
3) call ast.parse() on the source string<br>
4) do anything you want to the AST, for example turn the "select(c for<br>
c in Customer if sum(c.orders.price) > 1000" into whatever SQL or<br>
other function calls<br>
5) pass the massaged AST to compile(), execute it and return the module<br>
<br>
Hmmm, yeah, I think you're basically suggesting macro-like processing<br>
of the AST. Pretty cool, but not quite what I was thinking of ... I<br>
was thinking select() would get an AST object at runtime and do stuff<br>
with it.<br></blockquote><div><br></div><div>Depending on what version of Python you are targeting, it's actually simpler than that even to get it into the import system:</div><div><ol><li>Subclass <a href="https://docs.python.org/3/library/importlib.html#importlib.machinery.SourceFileLoader">importlib.machinery.SourceFileLoader</a> and override <a href="https://docs.python.org/3/library/importlib.html#importlib.abc.InspectLoader.source_to_code">source_to_code()</a> to do your AST transformation and return your changed code object (basically your steps 3-5 above)</li><li>Set a path hook that uses an instance of <a href="https://docs.python.org/3/library/importlib.html#importlib.machinery.FileFinder">importlib.machinery.FileFinder</a> which utilizes your custom loader</li><li>There is no step 3</li></ol><div>I know this isn't what you're after, but I just wanted to let you know importlib has made this sort of thing fairly trivial to implement.</div><div><br></div><div>-Brett</div></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
-Ben<br>
<br>
On Thu, May 21, 2015 at 9:51 PM, Andrew Barnert <<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>> wrote:<br>
> On May 21, 2015, at 18:18, Ben Hoyt <<a href="mailto:benhoyt@gmail.com" target="_blank">benhoyt@gmail.com</a>> wrote:<br>
>><br>
>> (I know that there's the "ast" module and ast.parse(), which can give<br>
>> you an AST given a *source string*, but that's not very convenient<br>
>> here.)<br>
><br>
> Why not? Python modules are distributed as source. You can pretty easily write an import hook to intercept module loading at the AST level and transform it however you want. Or just use MacroPy, which wraps up all the hard stuff (especially 2.x compatibility) and provides a huge framework of useful tools. What do you want to do that can't be done that way?<br>
><br>
> For many uses, you don't even have to go that far--code objects remember their source file and line number, which you can usually use to retrieve the text and regenerate the AST.<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div>