Export a Python AST to a dictionary.
Project description
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": []
}
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
export-ast-1.0.6.tar.gz
(4.6 kB
view details)
Built Distribution
File details
Details for the file export-ast-1.0.6.tar.gz
.
File metadata
- Download URL: export-ast-1.0.6.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e15de765a0fec0ea8613f3124097d2336b14cd56107001a50efa911a56db160 |
|
MD5 | 537312e0324c07384df9db05a3abd1bd |
|
BLAKE2b-256 | bac1c8ab8694369b4ae6ac9bf764bd290b294e78100e91329165f3398a66f335 |
File details
Details for the file export_ast-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: export_ast-1.0.6-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4b79b322045f52324f99eb96f7d2dc6a05617e3cd0d7cbc5be33103d61a5b24 |
|
MD5 | 1e3ecfbe7554436575e219b21dd7dba3 |
|
BLAKE2b-256 | ad50ff58e1da334794a65f803d406058b0bfb6ad9f361b0f5bdb00cf371d4f81 |