python macros karnickel

Solutions on MaxInterview for python macros karnickel by the best coders in the world

showing results for - "python macros karnickel"
Germaine
27 Sep 2020
1Python 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.
2
3If 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:
4
5pip install karnickel
6
7In macros.py:
8
9from karnickel import macro
10@macro
11def custom_loop(i):
12    for __x in range(i):
13        print __x
14        if __x < i-1:
15            __body__
16
17In example.py:
18import karnickel
19karnickel.install_hook()
20from macros.__macros__ import custom_loop
21with custom_loop(10):
22    print "I'm looping!"