export-ast


Nameexport-ast JSON
Version 1.0.6 PyPI version JSON
download
home_pagehttps://github.com/tom-draper/ast-export
SummaryExport a Python AST to a dictionary.
upload_time2022-12-24 00:43:45
maintainer
docs_urlNone
authorTom Draper
requires_python>=3.6
licenseMIT 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.
keywords ast abstract syntax trees dict export json
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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": []
  }
}
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tom-draper/ast-export",
    "name": "export-ast",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "ast,abstract syntax trees,dict,export,json",
    "author": "Tom Draper",
    "author_email": "Tom Draper <tomjdraper1@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/ba/c1/c8ab8694369b4ae6ac9bf764bd290b294e78100e91329165f3398a66f335/export-ast-1.0.6.tar.gz",
    "platform": null,
    "description": "# Export AST\r\n\r\nExport a Python abstract syntax tree to a dictionary/json.\r\n\r\n## Usage\r\n\r\n```bash\r\npip install export-ast\r\n```\r\n\r\n```py\r\nimport ast\r\nfrom export_ast import ast_to_dict\r\n\r\ncode = \"print('Hello World!')\"\r\ntree = ast.parse(code)\r\n\r\nd = ast_to_dict(tree)\r\n\r\n# Or as AST string (indent must be >0)...\r\ntree_str = ast.dump(tree, indent=2)\r\nd = ast_to_dict(tree_str)\r\n```\r\n\r\n## Example\r\n\r\n### Python Code\r\n\r\n```py\r\ndef hello_world():\r\n    print('Hello World!')\r\n\r\nif __name__ == '__main__':\r\n    hello_world()\r\n```\r\n\r\n### Abstract Syntax Tree\r\n\r\n```py\r\nModule(\r\n  body=[\r\n    FunctionDef(\r\n      name='hello_world',\r\n      args=arguments(\r\n        posonlyargs=[],\r\n        args=[],\r\n        kwonlyargs=[],\r\n        kw_defaults=[],\r\n        defaults=[]),\r\n      body=[\r\n        Expr(\r\n          value=Call(\r\n            func=Name(id='print', ctx=Load()),\r\n            args=[\r\n              Constant(value='Hello World!')],\r\n            keywords=[]))],\r\n      decorator_list=[]),\r\n    If(\r\n      test=Compare(\r\n        left=Name(id='__name__', ctx=Load()),\r\n        ops=[\r\n          Eq()],\r\n        comparators=[\r\n          Constant(value='__main__')]),\r\n      body=[\r\n        Expr(\r\n          value=Call(\r\n            func=Name(id='hello_world', ctx=Load()),\r\n            args=[],\r\n            keywords=[]))],\r\n      orelse=[])],\r\n  type_ignores=[])\r\n```\r\n\r\n### Dictionary\r\n\r\n```json\r\n{\r\n  \"Module\": {\r\n    \"body\": [\r\n      {\r\n        \"FunctionDef\": {\r\n          \"name\": \"hello_world\",\r\n          \"args\": {\r\n            \"arguments\": {\r\n              \"posonlyargs\": [],\r\n              \"args\": [],\r\n              \"kwonlyargs\": [],\r\n              \"kw_defaults\": [],\r\n              \"defaults\": []\r\n            }\r\n          },\r\n          \"body\": [\r\n            {\r\n              \"Expr\": {\r\n                \"value\": {\r\n                  \"Call\": {\r\n                    \"func\": {\r\n                      \"Name\": {\r\n                        \"id\": \"print\",\r\n                        \"ctx\": \"Load\"\r\n                      }\r\n                    },\r\n                    \"args\": [\r\n                      {\r\n                        \"Constant\": {\r\n                          \"value\": \"Hello World!\"\r\n                        }\r\n                      }\r\n                    ],\r\n                    \"keywords\": []\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          ],\r\n          \"decorator_list\": []\r\n        }\r\n      },\r\n      {\r\n        \"If\": {\r\n          \"test\": {\r\n            \"Compare\": {\r\n              \"left\": {\r\n                \"Name\": {\r\n                  \"id\": \"__name__\",\r\n                  \"ctx\": \"Load\"\r\n                }\r\n              },\r\n              \"ops\": [\r\n                \"Eq\"\r\n              ],\r\n              \"comparators\": [\r\n                {\r\n                  \"Constant\": {\r\n                    \"value\": \"__main__\"\r\n                  }\r\n                }\r\n              ]\r\n            }\r\n          },\r\n          \"body\": [\r\n            {\r\n              \"Expr\": {\r\n                \"value\": {\r\n                  \"Call\": {\r\n                    \"func\": {\r\n                      \"Name\": {\r\n                        \"id\": \"hello_world\",\r\n                        \"ctx\": \"Load\"\r\n                      }\r\n                    },\r\n                    \"args\": [],\r\n                    \"keywords\": []\r\n                  }\r\n                }\r\n              }\r\n            }\r\n          ],\r\n          \"orelse\": []\r\n        }\r\n      }\r\n    ],\r\n    \"type_ignores\": []\r\n  }\r\n}\r\n```\r\n",
    "bugtrack_url": null,
    "license": "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.",
    "summary": "Export a Python AST to a dictionary.",
    "version": "1.0.6",
    "split_keywords": [
        "ast",
        "abstract syntax trees",
        "dict",
        "export",
        "json"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "1e3ecfbe7554436575e219b21dd7dba3",
                "sha256": "f4b79b322045f52324f99eb96f7d2dc6a05617e3cd0d7cbc5be33103d61a5b24"
            },
            "downloads": -1,
            "filename": "export_ast-1.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1e3ecfbe7554436575e219b21dd7dba3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 5166,
            "upload_time": "2022-12-24T00:43:44",
            "upload_time_iso_8601": "2022-12-24T00:43:44.121582Z",
            "url": "https://files.pythonhosted.org/packages/ad/50/ff58e1da334794a65f803d406058b0bfb6ad9f361b0f5bdb00cf371d4f81/export_ast-1.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "537312e0324c07384df9db05a3abd1bd",
                "sha256": "9e15de765a0fec0ea8613f3124097d2336b14cd56107001a50efa911a56db160"
            },
            "downloads": -1,
            "filename": "export-ast-1.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "537312e0324c07384df9db05a3abd1bd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 4590,
            "upload_time": "2022-12-24T00:43:45",
            "upload_time_iso_8601": "2022-12-24T00:43:45.477938Z",
            "url": "https://files.pythonhosted.org/packages/ba/c1/c8ab8694369b4ae6ac9bf764bd290b294e78100e91329165f3398a66f335/export-ast-1.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-12-24 00:43:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "tom-draper",
    "github_project": "ast-export",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "export-ast"
}
        
Elapsed time: 0.02107s