Live data from Hacker News

Macros in Python

github.com

51–60 of 71 posts

Re: Macros in Python

#51

I see the docs mention that these only work when importing modules. The dev version of IPython lets you transform the AST of code that is typed in the REPL. It would be really cool to see an IPython AST transformer based on this. Here is an example from the test files in IPython: https://github.com/ipython/ipython/blob/master/IPython/core/...

Yep, there's an issue for that! https://github.com/lihaoyi/macropy/issues/14

I've also gotten a basic implementation working in the CPython REPL, which requires you to run a function `macro_repl()` before you can use them. A Macro powered REPL would be super cool!

Re: Macros in Python

#52
Just a small note. In the definition of the Cons class, the iterate method is calling each time to len before proceeding, and that is terribly inefficient.

def __iter__(self): current = self

            while len(current) > 0:
                yield current.head
                current = current.tail

Re: Macros in Python

#54
post #49

Earlier quoted context omitted.

From the opening paragraph of the readme.md > MacroPy provides a mechanism for user-defined functions (macros) to perform transformations on the abstract syntax tree(AST) of Python code at module import time Basically it rewrites the code before it's compiled, so no, x and y don't need to exist (compilation hasn't finished when the macro runs)

Decorators can do this? I did not know this. I thought decorators just get handed the object after initialization, so something like: @blah class Lol: pass is equivalent to: class Lol: pass blah(Lol) What am I missing?

> What am I missing?

Import hooks.

Re: Macros in Python

#55
post #52

Just a small note. In the definition of the Cons class, the iterate method is calling each time to len before proceeding, and that is terribly inefficient. def __iter__(self): current = self while len(current) > 0: yield current.head current = current.tail

Yeah, we haven't looked at performance at all.

"The Wonderful Thing About A Dancing Bear Is Not How Well He Dances, But That He Dances At All"

We'll get to teaching him to dance well later!

Re: Macros in Python

#57
post #55
post #52

Just a small note. In the definition of the Cons class, the iterate method is calling each time to len before proceeding, and that is terribly inefficient. def __iter__(self): current = self while len(current) > 0: yield current.head current = current.tail

Yeah, we haven't looked at performance at all. "The Wonderful Thing About A Dancing Bear Is Not How Well He Dances, But That He Dances At All" We'll get to teaching him to dance well later!

Speaking about concepts perhaps Norvig post "(How to Write a (Lisp) Interpreter (in Python))" http://norvig.com/lispy.html can be useful. The concept of a dotted pair (the car and the cdr are interesting historical facts.

I wish you the best for your presentation for the Mit class, it seems interesting but perhaps it should have a link to some standard techniques like machine learning.

I am curious about the small projects, will they be available in the page of the course?

Re: Macros in Python

#58
post #49

Earlier quoted context omitted.

From the opening paragraph of the readme.md > MacroPy provides a mechanism for user-defined functions (macros) to perform transformations on the abstract syntax tree(AST) of Python code at module import time Basically it rewrites the code before it's compiled, so no, x and y don't need to exist (compilation hasn't finished when the macro runs)

Decorators can do this? I did not know this. I thought decorators just get handed the object after initialization, so something like: @blah class Lol: pass is equivalent to: class Lol: pass blah(Lol) What am I missing?

isn't it equivalent to

  class Lol:
    pass
  Lol = blah(Lol)

Re: Macros in Python

#59
Author here,

I just put up a Detailed Guide:

https://github.com/lihaoyi/macropy#detailed-guide

That goes into more detail of how macros are written, and walks you through writing a simple but useful macro (the quicklambda macro). This should greatly help anyone who wants to try his hand at some metaprogramming =)

Re: Macros in Python

#60
post #37

This is awesome. Everything here (except the anonymous arguments to lambdas, seriously, just use x y and z as default argument names) should be in Python 4 or whatever you'd want to call the successor to Python. I love love love the PEG implementation.

I agree nice clean work, so many try to simply "out do" with elegance and "smart code". Clean and simple is a gem hard to find. I will gladly contribute going forward.

If you're serious about wanting to contribute, my email is on my GitHub profile, so ping me and I can help you get up to speed!
Post reply on HN