Giter Club home page Giter Club logo

ast-comments's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.