# ast-comments
An extension to the built-in `ast` module.
Finds comments in source code and adds them to the parsed tree.
## Installation
```
pip install ast-comments
```
## Usage
There is no difference in usage between `ast` and `ast-comments`
```
>>> from ast_comments import *
>>> tree = parse("hello = 'hello' # comment to hello")
```
Parsed tree is an instance of the original `ast.Module` object.
The only difference is that there is a new type of tree node: `Comment`
```
>>> tree
<_ast.Module object at 0x7ffba52322e0>
>>> tree.body
[<ast.Assign object at 0x10a01d5b0>, <ast_comments.Comment object at 0x10a09e0a0>]
>>> tree.body[1].value
'# comment to hello'
>>> dump(tree)
"Module(body=[Assign(targets=[Name(id='hello', ctx=Store())], value=Constant(value='hello')), Comment(value='# comment to hello', inline=True)], type_ignores=[])"
```
If you have python3.9 or above it's also possible to unparse the tree object with its comments preserved.
```
>>> print(unparse(tree))
hello = 'hello' # comment to hello
```
**Note**: Python `compile()` cannot be run on the tree output from parse. The included `pre_compile_fixer()` function can be used to fix the tree (stripping
comment nodes) if it will be necessary to compile the output.
More examples can be found in test_parse.py and test_unparse.py.
## Contributing
You are welcome to open an issue or create a pull request
Raw data
{
"_id": null,
"home_page": "https://github.com/t3rn0/ast-comments",
"name": "ast-comments",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Dmitry Makarov",
"author_email": "dmtern0vnik@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/67/1a/ffd2e7109201d84698a0148207a4f7c5d9f16f5d0a32505e09c0bfbd3959/ast_comments-1.2.1.tar.gz",
"platform": null,
"description": "# ast-comments\n\nAn extension to the built-in `ast` module. \nFinds comments in source code and adds them to the parsed tree.\n\n## Installation\n```\npip install ast-comments\n```\n\n## Usage\n\nThere is no difference in usage between `ast` and `ast-comments`\n```\n>>> from ast_comments import *\n>>> tree = parse(\"hello = 'hello' # comment to hello\")\n```\nParsed tree is an instance of the original `ast.Module` object.\nThe only difference is that there is a new type of tree node: `Comment`\n```\n>>> tree\n<_ast.Module object at 0x7ffba52322e0>\n>>> tree.body\n[<ast.Assign object at 0x10a01d5b0>, <ast_comments.Comment object at 0x10a09e0a0>]\n>>> tree.body[1].value\n'# comment to hello'\n>>> dump(tree)\n\"Module(body=[Assign(targets=[Name(id='hello', ctx=Store())], value=Constant(value='hello')), Comment(value='# comment to hello', inline=True)], type_ignores=[])\"\n```\nIf you have python3.9 or above it's also possible to unparse the tree object with its comments preserved.\n```\n>>> print(unparse(tree))\nhello = 'hello' # comment to hello\n```\n**Note**: Python `compile()` cannot be run on the tree output from parse. The included `pre_compile_fixer()` function can be used to fix the tree (stripping \ncomment nodes) if it will be necessary to compile the output.\n\nMore examples can be found in test_parse.py and test_unparse.py.\n\n## Contributing\nYou are welcome to open an issue or create a pull request\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "",
"version": "1.2.1",
"project_urls": {
"Homepage": "https://github.com/t3rn0/ast-comments",
"Repository": "https://github.com/t3rn0/ast-comments"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6d93b0aa067b5ebe041a76550e94efff7386506e686a62db136ea5ef9b388823",
"md5": "bcb7cf493da9f15ca533bbdd89458e2f",
"sha256": "c6124e9b3b95a837b506d05a5439fe7014cb576bd36a6f18d7ca6b10a5a0493b"
},
"downloads": -1,
"filename": "ast_comments-1.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "bcb7cf493da9f15ca533bbdd89458e2f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 5761,
"upload_time": "2023-12-26T08:33:53",
"upload_time_iso_8601": "2023-12-26T08:33:53.000182Z",
"url": "https://files.pythonhosted.org/packages/6d/93/b0aa067b5ebe041a76550e94efff7386506e686a62db136ea5ef9b388823/ast_comments-1.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "671affd2e7109201d84698a0148207a4f7c5d9f16f5d0a32505e09c0bfbd3959",
"md5": "36fb839f569ef11819bf4901732884c7",
"sha256": "28f356ed2d5943e39a25ba12038d6210b01ce9363c42101216fc61561d318d3a"
},
"downloads": -1,
"filename": "ast_comments-1.2.1.tar.gz",
"has_sig": false,
"md5_digest": "36fb839f569ef11819bf4901732884c7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 5288,
"upload_time": "2023-12-26T08:33:54",
"upload_time_iso_8601": "2023-12-26T08:33:54.951469Z",
"url": "https://files.pythonhosted.org/packages/67/1a/ffd2e7109201d84698a0148207a4f7c5d9f16f5d0a32505e09c0bfbd3959/ast_comments-1.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-26 08:33:54",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "t3rn0",
"github_project": "ast-comments",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "ast-comments"
}