Skip to main content

Toolbox for working with the Python AST

Project description

Build Status Coverage Status

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 ints, 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


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

ast_tools-0.0.12-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file ast_tools-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: ast_tools-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for ast_tools-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 02c815c80c18474dbb47d830cd6f108588cc69622cb750bcd23296ba2e850f4d
MD5 e22a0b92cf2a9a9590c108a4167db934
BLAKE2b-256 bdbf0692017f1c818c96360797b7a4ae3d94363b25dba9180afbe3181fca6d17

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page