Skip to main content

astprettier

PyPI Package Unit Tests Python >=3.8 License

Pretty print Python AST nodes, the return value of 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),
)

Console

On console, you can either pass Python syntax via STDIN or provide a filename.

$ astprettier -h
usage: astprettier.py [-h] [--show-offsets] [-n] [-i INDENT] [-l INDENT_LEVEL] [-p NS_PREFIX] [-c] filename

positional arguments:
  filename

optional arguments:
  -h, --help            show this help message and exit
  --show-offsets
  -n, --no-show-offsets
  -i INDENT, --indent INDENT
  -l INDENT_LEVEL, --level INDENT_LEVEL
  -p NS_PREFIX, --ns-prefix NS_PREFIX
  -c, --colorize

astprettier can optionally colorize the console output with pygments.

$ echo 'x=1' | astprettier -c

image

Contributing

We love contributions!

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

Metadata

Release files for astprettier 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astprettier 0.1.2
File Size Uploaded
astprettier-0.1.2.tar.gz 29.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for astprettier 0.1.2
File Interpreter ABI Platform
astprettier-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 35.0 kB

Release files / astprettier-0.1.2.tar.gz

Download URL astprettier-0.1.2.tar.gz
Size 29.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b1593797f4932c9cecd3ca385d6c98cecaf6aecbc5f45a878e1b7359f5c15a75
BLAKE2b-256 checksum
How to use checksums
0b98346688b772ac4b5d857a20b51bd1b37b1e93e748ee04fa04b9da0fe117ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / astprettier-0.1.2-py3-none-any.whl

Download URL astprettier-0.1.2-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67bf4c4afe50d5c56a2180c42e1f9ac8060b8e3fd5a545c5f2c70b6437cffd95
BLAKE2b-256 checksum
How to use checksums
1807f3c67b8881ebb7cac2595856c3417407bee3c989e1ff17ef18904a91dd61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page