Create ASTs from `ast.dump()` output.
Project description
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=[],
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
astload-0.1.1.tar.gz
(3.0 kB
view details)
Built Distribution
File details
Details for the file astload-0.1.1.tar.gz
.
File metadata
- Download URL: astload-0.1.1.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd39248f1f46428b3344c017b98e2f755fda9405b7d1352349331006dae357c0 |
|
MD5 | 4e1e793c51fd1793ec1de44f49c0cd57 |
|
BLAKE2b-256 | 1fe7eee8fcafabcdd61e8f509a3344e81b4f35d82e3cd06e6594186568dda5f4 |
File details
Details for the file astload-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: astload-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c148ac204794cccc6d6d83572d4152280c755fe0766bc16fb0c10c42a8fb3eb |
|
MD5 | 4ad11e430117b4c5aa0a33d9132bb6c6 |
|
BLAKE2b-256 | a12013d88757cec3a33272b0a79b7aa0df64a2c18f312453ff909f33ec4c32a9 |