All Projects → asottile → Astpretty

asottile / Astpretty

Licence: mit
Pretty print the output of python stdlib `ast.parse`.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Astpretty

Ast Pretty Print
A pretty printer for AST-like structures
Stars: ✭ 129 (+38.71%)
Mutual labels:  pretty-print, ast
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (-2.15%)
Mutual labels:  ast
Colorjson
Fast Color JSON Marshaller + Pretty Printer for Golang
Stars: ✭ 71 (-23.66%)
Mutual labels:  pretty-print
Prettier Package Json
Prettier formatter for package.json files
Stars: ✭ 86 (-7.53%)
Mutual labels:  pretty-print
Ts Transform Css Modules
Extract css class names from required css module files for TypeScript
Stars: ✭ 75 (-19.35%)
Mutual labels:  ast
Diffsitter
A tree-sitter based AST difftool to get meaningful semantic diffs
Stars: ✭ 89 (-4.3%)
Mutual labels:  ast
Charly Vm
Fibers, Closures, C-Module System | NaN-boxing, bytecode-VM written in C++
Stars: ✭ 66 (-29.03%)
Mutual labels:  ast
Pyast64
Compile a subset of the Python AST to x64-64 assembler
Stars: ✭ 93 (+0%)
Mutual labels:  ast
Ts Type Info
TypeScript AST and code generator [Deprecated]
Stars: ✭ 90 (-3.23%)
Mutual labels:  ast
I Pascal
A free Object Pascal language plugin for IntelliJ IDEA
Stars: ✭ 85 (-8.6%)
Mutual labels:  ast
Hippo
PHP standards checker.
Stars: ✭ 82 (-11.83%)
Mutual labels:  ast
Deps Report
Generate reports about dependencies and dependents of your JavaScript/TypeScript files through an AST. It supports import and require statements.
Stars: ✭ 76 (-18.28%)
Mutual labels:  ast
Jsx Ast Utils
AST utility module for statically analyzing JSX
Stars: ✭ 89 (-4.3%)
Mutual labels:  ast
Method log
Trace the history of an individual method in a git repository (experimental)
Stars: ✭ 73 (-21.51%)
Mutual labels:  ast
Libdparse
Library for lexing and parsing D source code
Stars: ✭ 91 (-2.15%)
Mutual labels:  ast
Xcode Ast Dump
Dump the AST of your Swift Xcode project
Stars: ✭ 71 (-23.66%)
Mutual labels:  ast
Lang C
Lightweight C parser for Rust
Stars: ✭ 77 (-17.2%)
Mutual labels:  ast
Uaiso
A multi-language parsing infrastructure with an unified AST
Stars: ✭ 86 (-7.53%)
Mutual labels:  ast
Redux Boilerplate Helpers
AST-based tool for automating Redux boilerplate
Stars: ✭ 94 (+1.08%)
Mutual labels:  ast
Gorm2sql
auto generate sql from gorm model struct
Stars: ✭ 92 (-1.08%)
Mutual labels:  ast

Build Status Azure DevOps coverage pre-commit.ci status

astpretty

Pretty print the output of python stdlib ast.parse.

astpretty is intended to be a replacement for ast.dump.

Installation

pip install astpretty

Usage

astpretty provides two api functions:

astpretty.pprint(node, indent=FOUR_SPACE_INDENT, show_offsets=True)

Print a representation of the ast node.

>>> astpretty.pprint(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=[],
)

indent allows control over the indentation string:

>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0], indent='  ')
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=[],
)

show_offsets controls whether the output includes line / column information:

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

astpretty.pformat(node, indent=FOUR_SPACE_INDENT, show_offsets=True)

Return a string representation of the ast node.

Arguments are identical to astpretty.pprint.

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

Comparison with stdlib ast.dump

>>> print(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=Num(n=4))], orelse=[])

typed-ast support

astpretty works with typed-ast!

For usage with typed-ast make sure you have typed-ast installed, a convenient way to do this is with the typed extra to astpretty:

pip install astpretty[typed]

The apis above work equally well with the return values from the ast modules provided by typed_ast:

>>> import astpretty
>>> from typed_ast import ast3
>>> astpretty.pprint(ast3.parse('x = 4  # type: int'))
Module(
    body=[
        Assign(
            lineno=1,
            col_offset=0,
            targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
            value=Num(lineno=1, col_offset=4, n=4),
            type_comment='int',
        ),
    ],
    type_ignores=[],
)

With typed-ast installed, the commandline interface adds --typed-27 and --typed-3 options for using the alternative ast parsers:

$ astpretty --typed-3 t.py
Module(
    body=[
        Assign(
            lineno=1,
            col_offset=0,
            targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
            value=Num(lineno=1, col_offset=4, n=4),
            type_comment='int',
        ),
    ],
    type_ignores=[],
)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].