Skip to main content

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


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.2.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ast_comments-1.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file ast_comments-1.2.0.tar.gz.

File metadata

  • Download URL: ast_comments-1.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.13 Darwin/22.6.0

File hashes

Hashes for ast_comments-1.2.0.tar.gz
Algorithm Hash digest
SHA256 9f8a0194ae560e9407a4ce5b46c9e67ab02e2266d27c6305f0d4c4585229b5ad
MD5 c4e3bbd550944c9ce6dd25e47307025e
BLAKE2b-256 9723ada5cce786a5ba3238ddb7baef2a0a5cbb1f5ce833041c0eae2b7f886e0e

See more details on using hashes here.

File details

Details for the file ast_comments-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: ast_comments-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.13 Darwin/22.6.0

File hashes

Hashes for ast_comments-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b76e3fbf7dfabad244ca543ccdb27dcadbde860408dadc7d4dc944f0fbb6dab0
MD5 f75c0207cca814d738643ccb3b44a864
BLAKE2b-256 d73536212f5ed05d334540c8cc6c86672095003cebdefb1405646ac7f348d378

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page