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 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
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
Built Distribution
Hashes for ast_comments-1.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6468c6b91d971b3fca2e3cd6f7a6d52e7385b6e4d25809444372960c74a8cbc2 |
|
MD5 | 1f9b7158c74e1d83a02bcdfc50e63511 |
|
BLAKE2b-256 | 85abe7f191cd70fe861367a55449381832d6703b475ab320a9d96f3d61ea672b |