Toolbox for working with the Python AST
Project description
Toolbox for working with the Python AST
pip install ast_tools
Useful References
Macros
Loop Unrolling
Unroll loops using the pattern
for <var> in ast_tools.macros.unroll(<iter>):
...
<iter>
should be an iterable object that produces integers (e.g. range(8)
)
that can be evaluated at definition time (can refer to variables in the scope
of the function definition)
For example,
from ast_tools.passes import begin_rewrite, loop_unroll, end_rewrite
@end_rewrite()
@loop_unroll()
@begin_rewrite()
def foo():
for i in ast_tools.macros.unroll(range(8)):
print(i)
is rewritten into
def foo():
print(0)
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
You can also use a list of int
s, here's an example that also uses a reference
to a variable defined in the outer scope:
from ast_tools.passes import begin_rewrite, loop_unroll, end_rewrite
j = [1, 2, 3]
@end_rewrite()
@loop_unroll()
@begin_rewrite()
def foo():
for i in ast_tools.macros.unroll(j):
print(i)
becomes
def foo():
print(1)
print(2)
print(3)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
File details
Details for the file ast_tools-0.0.14-py3-none-any.whl
.
File metadata
- Download URL: ast_tools-0.0.14-py3-none-any.whl
- Upload date:
- Size: 24.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90dc208935fbff3c209b8f8f3e75f17c6220d5bdaee5532c504dc555c2311a94 |
|
MD5 | 4d05078b3fc276e5e68216ceaca3a683 |
|
BLAKE2b-256 | 66bb71d1417c36245585decd2d981afa232c3429f45f645ee3eacb3a8a88f348 |