Skip to main content

Pretty print the output of python stdlib `ast.parse`.

Project description

astprettier

PyPI Package Unit Tests Python >=3.8 License

Pretty print the output of Python AST ast.parse.

Note: This is a fork of astpretty. The goals of this fork are:

  • Adding more advanced features that go beyond the REPL scope of astpretty
  • Refactoring and cleanup to make the very small code base easier to understand and maintain
  • Experimentation with Python packaging to make the project directory more beautiful and have less boilerplate

Installation

pip install astprettier

Usage

REPL

astprettier provides two main functions:

astprettier.format(node, indent_level=0, indent=' ', show_offsets=True, ns_prefix='') astprettier.print(node, indent_level=0, indent=' ', show_offsets=True, ns_prefix='')

astprettier.print is a thin wrapper around astprettier.format that just writes the output of format to sys.stdout

>>> import ast, astprettier
>>> astprettier.print(ast.parse('if x == y: y += 4').body[0])
If(
    lineno=1,
    col_offset=0,
    test=Compare(
        lineno=1,
        col_offset=3,
        left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),
        ops=[Eq()],
        comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],
    ),
    body=[
        AugAssign(
            lineno=1,
            col_offset=11,
            target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),
            op=Add(),
            value=Num(lineno=1, col_offset=16, n=4),
        ),
    ],
    orelse=[],
)

>>> # Comparison with ast.dump()
>>> ast.dump(ast.parse('if x == y: y += 4').body[0])
"If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Name(id='y', ctx=Load())]), body=[AugAssign(target=Name(id='y', ctx=Store()), op=Add(), value=Constant(value=4))], orelse=[])"

ns_prefix controls whether objects from the ast module will be prefixed with your ast namespace:

>>> astprettier.print(ast.parse('x += 5').body[0], show_offsets=False, ns_prefix='ast')
ast.AugAssign(
    target=ast.Name(id='x', ctx=ast.Store()),
    op=ast.Add(),
    value=ast.Num(n=5),
)

Script

$ python -m astprettier ./setup.py
Module(
    body=[
        ImportFrom(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=28,
            module='setuptools',
            names=[alias(name='setup', asname=None)],
            level=0,
        ),
        Expr(
            lineno=2,
            col_offset=0,
            end_lineno=2,
            end_col_offset=7,
            value=Call(
                lineno=2,
                col_offset=0,
                end_lineno=2,
                end_col_offset=7,
                func=Name(lineno=2, col_offset=0, end_lineno=2, end_col_offset=5, id='setup', ctx=Load()),
                args=[],
                keywords=[],
            ),
        ),
    ],
    type_ignores=[],
)

Contributing

We love contributions!

astprettier is open source, built on open source, and we'd love to have you hang out in our community.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

astprettier-0.1.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

astprettier-0.1-py3-none-any.whl (4.7 kB view hashes)

Uploaded Python 3

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