Answers for "python macros karnickel"

0

python macros karnickel

Python itself does not have macros, but you can kind of fake them by using the built-in AST module to parse and reload code on the fly. This is Python magic to the extreme.

If you don't have the patience or interest to do this yourself, karnickel (http://pypi.python.org/pypi/karnickel) is a small library that defines macros in just the way I described. Using karnickel, you can define a macro like so:

pip install karnickel

In macros.py:

from karnickel import macro
@macro
def custom_loop(i):
    for __x in range(i):
        print __x
        if __x < i-1:
            __body__

In example.py:
import karnickel
karnickel.install_hook()
from macros.__macros__ import custom_loop
with custom_loop(10):
    print "I'm looping!"
Posted by: Guest on June-09-2020

Python Answers by Framework

Browse Popular Code Answers by Language