Socket
Socket
Sign inDemoInstall

commie

Package Overview
Dependencies
0
Maintainers
1
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    commie

Extracts comments from source code in different programming languages


Maintainers
1

Readme

commie

Python package for extracting comments from source code.

Multiple programming and markup languages are supported: see list.

Install

$ pip3 install commie

Find comments in a file

from pathlib import Path
import commie

for comment in commie.iter_comments(Path("/path/to/source.cpp")):

    # something like "/* sample */"
    print("Comment code:", comment.code)
    print("Comment code location:", comment.code_span.start, comment.code_span.end)
    
    # something like " sample " 
    print("Comment inner text:", comment.text)
    print("Comment text location:", comment.text_span.start, comment.text_span.end)

Find comments in a string

MethodWorks for
commie.iter_comments_cC, C++, C#, Java, Objective-C, JavaScript, Dart, TypeScript
commie.iter_comments_goGo
commie.iter_comments_rubyRuby
commie.iter_comments_pythonPython
commie.iter_comments_shellBash, Sh
commie.iter_comments_htmlHTML, XML, SGML
commie.iter_comments_cssCSS
commie.iter_comments_sassSASS
import commie

source_code_in_golang:str = ...

for comment in commie.iter_comments_go(source_code_in_golang):
    # ... process comment ...
    pass

Find comments in a string with a known filename

Method commie.iter_comments will try to guess the file format from the provided filename.

from pathlib import Path
import commie

filename: str = "/path/to/mycode.go"
source_code: str = Path(filename).read_text()

for comment in commie.iter_comments(source_code, filename=filename):
    # ... process comment ...
    pass

Group single line comments

When single-line comments are adjacent, it makes sense to consider them together:

// Group A: A short comment

// Group B: It consists of three
// single-line comments with 
// no empty lines between them

// Group C: This paragraph loosely 
// stretched into two lines  

The comments from the example above can be combined into three groups as follows:

from commie import iter_comments, group_singleline_comments

for group in group_singleline_comments(iter_comments(...)):
    # ... each group is a list of Comment objects ...
    pass

Multi-line comments will also be returned. They will not be grouped with their neighbors.

History

This project was forked from comment_parser in 2021. Motivation:

comment_parsercommie
Returns only a line numberReturns positions where the comment starts and ends. Just like regular string search
Returns only the text of a commentRespects markup as well, making it possible to remove or replace the entire comment
Depends on python-magic that requires an optional installation of binariesPure Python. Easy to install with pip

Keywords

FAQs


Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc