astpretty


Nameastpretty JSON
Version 3.0.0 PyPI version JSON
download
home_pagehttps://github.com/asottile/astpretty
SummaryPretty print the output of python stdlib `ast.parse`.
upload_time2022-05-16 23:41:24
maintainer
docs_urlNone
authorAnthony Sottile
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.astpretty?branchName=main)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=35&branchName=main)
[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/35/main.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=35&branchName=main)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/astpretty/main.svg)](https://results.pre-commit.ci/latest/github/asottile/astpretty/main)

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.

```python
>>> 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:

```python
>>> 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:

```python
>>> 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`.

```python
>>> 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`

```python
>>> 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](https://github.com/python/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`:

```bash
pip install astpretty[typed]
```

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

```pycon
>>> 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:

```console
$ 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=[],
)
```



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/asottile/astpretty",
    "name": "astpretty",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Anthony Sottile",
    "author_email": "asottile@umich.edu",
    "download_url": "https://files.pythonhosted.org/packages/2d/b6/ffec7edce2a8315ef1acd4ae2e334ba428db2b2e8d7577a9aaf1e434034d/astpretty-3.0.0.tar.gz",
    "platform": null,
    "description": "[![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.astpretty?branchName=main)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=35&branchName=main)\n[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/35/main.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=35&branchName=main)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/astpretty/main.svg)](https://results.pre-commit.ci/latest/github/asottile/astpretty/main)\n\nastpretty\n=========\n\nPretty print the output of python stdlib `ast.parse`.\n\nastpretty is intended to be a replacement for `ast.dump`.\n\n## Installation\n\n`pip install astpretty`\n\n\n## Usage\n\n`astpretty` provides two api functions:\n\n\n### `astpretty.pprint(node, indent=FOUR_SPACE_INDENT, show_offsets=True)`\n\nPrint a representation of the ast node.\n\n```python\n>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0])\nIf(\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)\n```\n\n`indent` allows control over the indentation string:\n\n```python\n>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0], indent='  ')\nIf(\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)\n```\n\n`show_offsets` controls whether the output includes line / column information:\n\n```python\n>>> astpretty.pprint(ast.parse('x += 5').body[0], show_offsets=False)\nAugAssign(\n    target=Name(id='x', ctx=Store()),\n    op=Add(),\n    value=Num(n=5),\n)\n```\n\n### `astpretty.pformat(node, indent=FOUR_SPACE_INDENT, show_offsets=True)`\n\nReturn a string representation of the ast node.\n\nArguments are identical to `astpretty.pprint`.\n\n```python\n>>> astpretty.pformat(ast.parse('if x == y: y += 4').body[0])\n\"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)\"\n```\n\n### Comparison with stdlib `ast.dump`\n\n```python\n>>> print(ast.dump(ast.parse('if x == y: y += 4').body[0]))\nIf(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=[])\n```\n\n### `typed-ast` support\n\n`astpretty` works with [typed-ast](https://github.com/python/typed_ast)!\n\nFor usage with `typed-ast` make sure you have `typed-ast` installed, a\nconvenient way to do this is with the `typed` extra to `astpretty`:\n\n```bash\npip install astpretty[typed]\n```\n\nThe apis above work equally well with the return values from the `ast` modules\nprovided by `typed_ast`:\n\n```pycon\n>>> import astpretty\n>>> from typed_ast import ast3\n>>> astpretty.pprint(ast3.parse('x = 4  # type: int'))\nModule(\n    body=[\n        Assign(\n            lineno=1,\n            col_offset=0,\n            targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],\n            value=Num(lineno=1, col_offset=4, n=4),\n            type_comment='int',\n        ),\n    ],\n    type_ignores=[],\n)\n```\n\nWith `typed-ast` installed, the commandline interface adds `--typed-27` and\n`--typed-3` options for using the alternative ast parsers:\n\n```console\n$ astpretty --typed-3 t.py\nModule(\n    body=[\n        Assign(\n            lineno=1,\n            col_offset=0,\n            targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],\n            value=Num(lineno=1, col_offset=4, n=4),\n            type_comment='int',\n        ),\n    ],\n    type_ignores=[],\n)\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Pretty print the output of python stdlib `ast.parse`.",
    "version": "3.0.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "66e31d7ab88a11c1f78360d8c6c091e8",
                "sha256": "15bfd47593667169485a1fa7938b8de9445b11057d6f2b6e214b2f70667f94b6"
            },
            "downloads": -1,
            "filename": "astpretty-3.0.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "66e31d7ab88a11c1f78360d8c6c091e8",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.8",
            "size": 4928,
            "upload_time": "2022-05-16T23:41:23",
            "upload_time_iso_8601": "2022-05-16T23:41:23.422340Z",
            "url": "https://files.pythonhosted.org/packages/9f/0a/79fff71a08bc0cc427f0dfbd4cca62b60f7f277aae81b89b79e9b04d526d/astpretty-3.0.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "6af044f722f2f23de86d927e5ddf09ba",
                "sha256": "b08c95f32e5994454ea99882ff3c4a0afc8254c38998a0ed4b479dba448dc581"
            },
            "downloads": -1,
            "filename": "astpretty-3.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6af044f722f2f23de86d927e5ddf09ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4675,
            "upload_time": "2022-05-16T23:41:24",
            "upload_time_iso_8601": "2022-05-16T23:41:24.712315Z",
            "url": "https://files.pythonhosted.org/packages/2d/b6/ffec7edce2a8315ef1acd4ae2e334ba428db2b2e8d7577a9aaf1e434034d/astpretty-3.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-05-16 23:41:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "asottile",
    "github_project": "astpretty",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "astpretty"
}
        
Elapsed time: 0.03031s