[Python-ideas] AST Transformation Hooks for Domain Specific Languages

Eric Snow ericsnowcurrently at gmail.com
Sat Apr 9 08:06:34 CEST 2011


On Fri, Apr 8, 2011 at 11:35 PM, Carl M. Johnson <
cmjohnson.mailinglist at gmail.com> wrote:

> > I did not realize you're proposing to skip parsing of the body of dsl
> > function and just tuck all the code into Str literal.
>
> If I can ask a noobish question, in that case, what's the advantage of
> the new syntax over the currently existing ability to have a decorator
> parse a docstring? Eg., an SQL DSL could be done today as
>
>
> import dsl.sql
>
> @dsl.sql
> def lookup_address(name : dsl.sql.char, dob : dsl.sql.date):
>    """select address
>   from people
>   where name = {name} and dob = {dob}"""
>    pass
>
> If you can't bear to lose the docstring, you could instead write something
> like,
>
>
> import dsl.sql
>
> @dsl.sql
> def lookup_address(name : dsl.sql.char, dob : dsl.sql.date):
>   "Looks up the address of a name and DOB."
>   return """select address
>    from people
>   where name = {name} and dob = {dob}"""
>
>
>
> What's the advantage of the "def from" over this? Bear in mind that
> this way of doing has the advantage of working with today's Python
> syntax highlighters. ;-)
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>

My understanding is that the AST transformation is done at compile-time,
while the decorator happens at run-time.  So your .pyc file gets your
transformed code.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110409/b5e06128/attachment.html>


More information about the Python-ideas mailing list