Skip to main content

astload

Create ASTs from ast.dump() output.

It's possible to serialize an AST node from Python's ast module to a string, using ast.dump. However, it's not possible to convert the said string back to an AST node. This simple module adds that functionality.

Examples

We will take a simple Python AST, serialize it and then deserialize it:

>>> import ast
>>> import astload
>>> tree1 = ast.parse('1 == 2')
>>> ast_string = ast.dump(tree1, indent=2)
>>> print(ast_string)
Module(
  body=[
    Expr(
      value=Compare(
        left=Constant(value=1),
        ops=[
          Eq()],
        comparators=[
          Constant(value=2)]))],
  type_ignores=[])
>>> tree2 = astload.load(ast_string)
>>> print(ast.dump(tree2, indent=2))
Module(
  body=[
    Expr(
      value=Compare(
        left=Constant(value=1),
        ops=[
          Eq()],
        comparators=[
          Constant(value=2)]))],
  type_ignores=[])
>>> ast.dump(tree1) == ast.dump(tree2)
True
>>> ast.unparse(tree2)
'1 == 2'

Adding load() to the ast module

If you prefer ast.load() instead of astload.load(), you can do the following to add a load function to the ast module directly:

>>> import astload
>>> astload.install()
>>> import ast
>>> tree = ast.parse('print(2 + 2)')
>>> copy_tree = ast.load(ast.dump(tree))
>>> ast.unparse(copy_tree)
'print(2 + 2)'

astpretty support

astload supports astpretty output also:

>>> ast_string = astpretty.pformat(ast.parse('1 == 2'))
>>> print(ast_string)
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=6,
            value=Compare(
                lineno=1,
                col_offset=0,
                end_lineno=1,
                end_col_offset=6,
                left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=1, kind=None),
                ops=[Eq()],
                comparators=[Constant(lineno=1, col_offset=5, end_lineno=1, end_col_offset=6, value=2, kind=None)],
            ),
        ),
    ],
    type_ignores=[],
)
>>> tree = astload.load(ast_string)
>>> astpretty.pprint(tree)
Module(
    body=[
        Expr(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=6,
            value=Compare(
                lineno=1,
                col_offset=0,
                end_lineno=1,
                end_col_offset=6,
                left=Constant(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, value=1, kind=None),
                ops=[Eq()],
                comparators=[Constant(lineno=1, col_offset=5, end_lineno=1, end_col_offset=6, value=2, kind=None)],
            ),
        ),
    ],
    type_ignores=[],
)

Metadata

Release files for astload 0.1.1

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

Source distribution (sdist)

Source distribution for astload 0.1.1
File Size Uploaded
astload-0.1.1.tar.gz 3.0 kB Details

Built distribution (wheel)

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

Total release size: 6.1 kB

Release files / astload-0.1.1.tar.gz

Download URL astload-0.1.1.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cd39248f1f46428b3344c017b98e2f755fda9405b7d1352349331006dae357c0
BLAKE2b-256 checksum
How to use checksums
1fe7eee8fcafabcdd61e8f509a3344e81b4f35d82e3cd06e6594186568dda5f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.7

Release files / astload-0.1.1-py3-none-any.whl

Download URL astload-0.1.1-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c148ac204794cccc6d6d83572d4152280c755fe0766bc16fb0c10c42a8fb3eb
BLAKE2b-256 checksum
How to use checksums
a12013d88757cec3a33272b0a79b7aa0df64a2c18f312453ff909f33ec4c32a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.9.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

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