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
Metadata
Release files for better-ast-comments 1.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| better_ast_comments-1.2.1.tar.gz | 5.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| better_ast_comments-1.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.6 kB
Release files / better_ast_comments-1.2.1.tar.gz
| Download URL | better_ast_comments-1.2.1.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97c0d174b062f06696b4ba63c3bb9782c95a71a02ad505f2cde54c9c7e391d8c
|
|
BLAKE2b-256 checksum How to use checksums |
86cc6b5f687b2fe673f6ccda93d158d6f49552cb9fd44d87b8997f0b5cc9ea77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.11.6 Linux/6.0.19-4-MANJARO
|
Release files / better_ast_comments-1.2.1-py3-none-any.whl
| Download URL | better_ast_comments-1.2.1-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c4993af37ac2655f9997d3751a95bd8a14306d4de17d03d1a89e62d18bea9836
|
|
BLAKE2b-256 checksum How to use checksums |
5fa2226f4ba92a4661a5c5d9f8a98ef3fd3799aa6c35afd164c5e2db732dd19b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.11.6 Linux/6.0.19-4-MANJARO
|