{"id":13595711,"url":"https://github.com/asottile/astpretty","last_synced_at":"2025-05-15T09:08:22.604Z","repository":{"id":18892505,"uuid":"85516039","full_name":"asottile/astpretty","owner":"asottile","description":"Pretty print the output of python stdlib `ast.parse`.","archived":false,"fork":false,"pushed_at":"2025-03-31T21:00:51.000Z","size":265,"stargazers_count":192,"open_issues_count":0,"forks_count":19,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-14T15:01:41.778Z","etag":null,"topics":["ast","pretty-print","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/asottile.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"asottile"}},"created_at":"2017-03-19T23:15:34.000Z","updated_at":"2025-03-31T21:00:54.000Z","dependencies_parsed_at":"2023-02-19T03:00:46.305Z","dependency_job_id":"61852dda-e14e-4f18-9c85-72652447af3b","html_url":"https://github.com/asottile/astpretty","commit_stats":{"total_commits":156,"total_committers":5,"mean_commits":31.2,"dds":0.4935897435897436,"last_synced_commit":"a009343e32e81d3a4f598d7ae25cfcf0506efa39"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Fastpretty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Fastpretty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Fastpretty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/asottile%2Fastpretty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/asottile","download_url":"https://codeload.github.com/asottile/astpretty/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310520,"owners_count":22049470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","pretty-print","python"],"created_at":"2024-08-01T16:01:56.116Z","updated_at":"2025-05-15T09:08:17.595Z","avatar_url":"https://github.com/asottile.png","language":"Python","funding_links":["https://github.com/sponsors/asottile"],"categories":["Python","Tools"],"sub_categories":[],"readme":"[![build status](https://github.com/asottile/astpretty/actions/workflows/main.yml/badge.svg)](https://github.com/asottile/astpretty/actions/workflows/main.yml)\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```bash\npip install astpretty\n```\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\u003e\u003e\u003e 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\u003e\u003e\u003e 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\u003e\u003e\u003e 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\u003e\u003e\u003e 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\u003e\u003e\u003e 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasottile%2Fastpretty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fasottile%2Fastpretty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fasottile%2Fastpretty/lists"}