Skip to main content

Export AST

Export a Python abstract syntax tree to a dictionary/json.

Usage

pip install export-ast
import ast
from export_ast import ast_to_dict

code = "print('Hello World!')"
tree = ast.parse(code)

d = ast_to_dict(tree)

# Or as AST string (indent must be >0)...
tree_str = ast.dump(tree, indent=2)
d = ast_to_dict(tree_str)

Example

Python Code

def hello_world():
    print('Hello World!')

if __name__ == '__main__':
    hello_world()

Abstract Syntax Tree

Module(
  body=[
    FunctionDef(
      name='hello_world',
      args=arguments(
        posonlyargs=[],
        args=[],
        kwonlyargs=[],
        kw_defaults=[],
        defaults=[]),
      body=[
        Expr(
          value=Call(
            func=Name(id='print', ctx=Load()),
            args=[
              Constant(value='Hello World!')],
            keywords=[]))],
      decorator_list=[]),
    If(
      test=Compare(
        left=Name(id='__name__', ctx=Load()),
        ops=[
          Eq()],
        comparators=[
          Constant(value='__main__')]),
      body=[
        Expr(
          value=Call(
            func=Name(id='hello_world', ctx=Load()),
            args=[],
            keywords=[]))],
      orelse=[])],
  type_ignores=[])

Dictionary

{
  "Module": {
    "body": [
      {
        "FunctionDef": {
          "name": "hello_world",
          "args": {
            "arguments": {
              "posonlyargs": [],
              "args": [],
              "kwonlyargs": [],
              "kw_defaults": [],
              "defaults": []
            }
          },
          "body": [
            {
              "Expr": {
                "value": {
                  "Call": {
                    "func": {
                      "Name": {
                        "id": "print",
                        "ctx": "Load"
                      }
                    },
                    "args": [
                      {
                        "Constant": {
                          "value": "Hello World!"
                        }
                      }
                    ],
                    "keywords": []
                  }
                }
              }
            }
          ],
          "decorator_list": []
        }
      },
      {
        "If": {
          "test": {
            "Compare": {
              "left": {
                "Name": {
                  "id": "__name__",
                  "ctx": "Load"
                }
              },
              "ops": [
                "Eq"
              ],
              "comparators": [
                {
                  "Constant": {
                    "value": "__main__"
                  }
                }
              ]
            }
          },
          "body": [
            {
              "Expr": {
                "value": {
                  "Call": {
                    "func": {
                      "Name": {
                        "id": "hello_world",
                        "ctx": "Load"
                      }
                    },
                    "args": [],
                    "keywords": []
                  }
                }
              }
            }
          ],
          "orelse": []
        }
      }
    ],
    "type_ignores": []
  }
}

Metadata

Release files for export-ast 1.0.6

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

Source distribution (sdist)

Source distribution for export-ast 1.0.6
File Size Uploaded Provenance Actions
export-ast-1.0.6.tar.gz 4.6 kB Without provenance Details for export-ast-1.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for export-ast 1.0.6
File Interpreter ABI Platform Actions
export_ast-1.0.6-py3-none-any.whl Python 3 none any Details for export_ast-1.0.6-py3-none-any.whl

Total release size: 9.8 kB

Release files / export-ast-1.0.6.tar.gz

Download URL export-ast-1.0.6.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9e15de765a0fec0ea8613f3124097d2336b14cd56107001a50efa911a56db160
BLAKE2b-256 checksum
How to use checksums
bac1c8ab8694369b4ae6ac9bf764bd290b294e78100e91329165f3398a66f335
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.3 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/5.0.0 keyring/23.9.3 rfc3986/1.5.0 colorama/0.4.4 CPython/3.10.6

Release files / export_ast-1.0.6-py3-none-any.whl

Download URL export_ast-1.0.6-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4b79b322045f52324f99eb96f7d2dc6a05617e3cd0d7cbc5be33103d61a5b24
BLAKE2b-256 checksum
How to use checksums
ad50ff58e1da334794a65f803d406058b0bfb6ad9f361b0f5bdb00cf371d4f81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.3 readme-renderer/37.3 requests/2.27.1 requests-toolbelt/0.10.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/5.0.0 keyring/23.9.3 rfc3986/1.5.0 colorama/0.4.4 CPython/3.10.6

Security for export-ast 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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