export-ast-1.0.6
توضیحات
Export a Python AST to a dictionary.
| ویژگی | مقدار |
|---|---|
| سیستم عامل | - |
| نام فایل | export-ast-1.0.6 |
| نام | export-ast |
| نسخه کتابخانه | 1.0.6 |
| نگهدارنده | [] |
| ایمیل نگهدارنده | [] |
| نویسنده | Tom Draper |
| ایمیل نویسنده | Tom Draper <tomjdraper1@gmail.com> |
| آدرس صفحه اصلی | https://github.com/tom-draper/ast-export |
| آدرس اینترنتی | https://pypi.org/project/export-ast/ |
| مجوز | MIT License Copyright (c) 2022 Tom Draper Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
# Export AST
Export a Python abstract syntax tree to a dictionary/json.
## Usage
```bash
pip install export-ast
```
```py
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
```py
def hello_world():
print('Hello World!')
if __name__ == '__main__':
hello_world()
```
### Abstract Syntax Tree
```py
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
```json
{
"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": []
}
}
```
نیازمندی
| مقدار | نام |
|---|---|
| - | build |
| - | twine |
| - | pytest |
زبان مورد نیاز
| مقدار | نام |
|---|---|
| >=3.6 | Python |
نحوه نصب
نصب پکیج whl export-ast-1.0.6:
pip install export-ast-1.0.6.whl
نصب پکیج tar.gz export-ast-1.0.6:
pip install export-ast-1.0.6.tar.gz