No project description provided
Project description
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 instance of the original ast.Module object. The only difference is there is a new type of tree node: Comment
>>> tree
<_ast.Module object at 0x7ffba52322e0>
>>> tree.body
[<ast_comments.Comment object at 0x10c1b6160>, <_ast.Assign object at 0x10bd217c0>]
>>> tree.body[0].value
'# comment to hello'
>>> dump(tree)
"Module(body=[Assign(targets=[Name(id='hello', ctx=Store())], value=Constant(value='hello', kind=None), type_comment=None), 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))
# comment to hello
hello = 'hello'
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ast_comments-1.1.0.tar.gz
(4.4 kB
view hashes)
Built Distribution
Close
Hashes for ast_comments-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b9ddff5bf268d74ff06d9a332504e072f9a5d1f873b87722e0a2732adaf693c |
|
MD5 | e8c60b25e762b3b821d9c020e3f03bd3 |
|
BLAKE2b-256 | ae70f0690cf10234318c6c1d925402fd63ccfead87d48644ddd3d48ca98dd4ed |