# 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/zmievsa/better-ast-comments",
"name": "better-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/86/cc/6b5f687b2fe673f6ccda93d158d6f49552cb9fd44d87b8997f0b5cc9ea77/better_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/zmievsa/better-ast-comments",
"Repository": "https://github.com/zmievsa/better-ast-comments"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5fa2226f4ba92a4661a5c5d9f8a98ef3fd3799aa6c35afd164c5e2db732dd19b",
"md5": "7b1f2e95e791de3c01d1a282430d8f69",
"sha256": "c4993af37ac2655f9997d3751a95bd8a14306d4de17d03d1a89e62d18bea9836"
},
"downloads": -1,
"filename": "better_ast_comments-1.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "7b1f2e95e791de3c01d1a282430d8f69",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 5854,
"upload_time": "2023-12-25T15:37:00",
"upload_time_iso_8601": "2023-12-25T15:37:00.889380Z",
"url": "https://files.pythonhosted.org/packages/5f/a2/226f4ba92a4661a5c5d9f8a98ef3fd3799aa6c35afd164c5e2db732dd19b/better_ast_comments-1.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "86cc6b5f687b2fe673f6ccda93d158d6f49552cb9fd44d87b8997f0b5cc9ea77",
"md5": "ca055283e092e4996b18ae29ef4d9adc",
"sha256": "97c0d174b062f06696b4ba63c3bb9782c95a71a02ad505f2cde54c9c7e391d8c"
},
"downloads": -1,
"filename": "better_ast_comments-1.2.1.tar.gz",
"has_sig": false,
"md5_digest": "ca055283e092e4996b18ae29ef4d9adc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 5701,
"upload_time": "2023-12-25T15:37:02",
"upload_time_iso_8601": "2023-12-25T15:37:02.814025Z",
"url": "https://files.pythonhosted.org/packages/86/cc/6b5f687b2fe673f6ccda93d158d6f49552cb9fd44d87b8997f0b5cc9ea77/better_ast_comments-1.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-25 15:37:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zmievsa",
"github_project": "better-ast-comments",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "better-ast-comments"
}