[Python-ideas] Macros for Python

Haoyi Li haoyi.sg at gmail.com
Wed Apr 24 19:53:23 CEST 2013


I haven't tested in on various platforms, so hard to say for sure. MacroPy
basically relies on a few things:

- exec/eval
- PEP 302
- the ast module

All of these are pretty old pieces of python (almost 10 years old!) so it's
not some new-and-fancy functionality. Jython seems to have all of them, I
couldn't find any information about PyPy.

When the project is more mature and I have some time, I'll see if I can get
it to work cross platform. If anyone wants to fork the repo and try it out,
that'd be great too!

-Haoyi





On Wed, Apr 24, 2013 at 11:55 AM, Andrew Barnert <abarnert at yahoo.com> wrote:

> On Apr 24, 2013, at 8:05, Haoyi Li <haoyi.sg at gmail.com> wrote:
>
> You actually can get a syntax like that without macros, using
> stack-introspection, locals-trickery and lots of `eval`. The question is
> whether you consider macros more "extreme" than stack-introspection,
> locals-trickery and `eval`! A JIT compiler will probably be much happier
> with macros.
>
>
> That last point makes this approach seem particularly interesting to me,
> which makes me wonder: Is your code CPython specific, or does it also work
> with PyPy (or Jython or Iron)? While PyPy is obviously a whole lot easier
> to mess with in the first place than CPython, having macros at the same
> language level as your code is just as interesting in both implementations.
>
>
> On Wed, Apr 24, 2013 at 10:35 AM, Terry Jan Reedy <tjreedy at udel.edu>wrote:
>
>> On 4/23/2013 11:49 PM, Haoyi Li wrote:
>>
>>> I thought this may be of interest to some people on this list, even if
>>> not strictly an "idea".
>>>
>>> I'm working on MacroPy <https://github.com/lihaoyi/**macropy<https://github.com/lihaoyi/macropy>>,
>>> a little
>>>
>>> pure-python library that allows user-defined AST rewrites as part of the
>>> import process (using PEP 302).
>>>
>>
>> From the readme
>> '''
>> String Interpolation
>>
>> a, b = 1, 2
>> c = s%"%{a} apple and %{b} bananas"
>> print c
>> #1 apple and 2 bananas
>> '''
>> I am a little surprised that you would base a cutting edge extension on
>> Py 2. Do you have it working with 3.3 also?
>>
>> '''Unlike the normal string interpolation in Python, MacroPy's string
>> interpolation allows the programmer to specify the variables to be
>> interpolated inline inside the string.'''
>>
>> Not true as I read that.
>>
>> a, b = 1, 2
>> print("{a} apple and {b} bananas".format(**locals()))
>> print("%(a)s apple and %(b)s bananas" % locals())
>> #1 apple and 2 bananas
>> #1 apple and 2 bananas
>>
>> I rather like the anon funcs with anon params. That only works when each
>> param is only used once in the expression, but that restriction is the
>> normal case.
>>
>> I am interested to see what you do with pattern matching.
>>
>> tjr
>>
>> ______________________________**_________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> http://mail.python.org/**mailman/listinfo/python-ideas<http://mail.python.org/mailman/listinfo/python-ideas>
>>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130424/2b64e007/attachment.html>


More information about the Python-ideas mailing list