Skip to main content

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

Method Works for
commie.iter_comments_c C, C++, C#, Java, Objective-C, JavaScript, Dart, TypeScript
commie.iter_comments_go Go
commie.iter_comments_ruby Ruby
commie.iter_comments_python Python
commie.iter_comments_shell Bash, Sh
commie.iter_comments_html HTML, XML, SGML
commie.iter_comments_css CSS
commie.iter_comments_sass SASS
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_parser commie
Returns only a line number Returns positions where the comment starts and ends. Just like regular string search
Returns only the text of a comment Respects markup as well, making it possible to remove or replace the entire comment
Depends on python-magic that requires an optional installation of binaries Pure Python. Easy to install with pip

Metadata

Release files for commie 1.0.6.post2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for commie 1.0.6.post2
File Size Uploaded
commie-1.0.6.post2.tar.gz 14.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for commie 1.0.6.post2
File Interpreter ABI Platform
commie-1.0.6.post2-py3-none-any.whl Python 3 none any Details

Total release size: 38.4 kB

Release files / commie-1.0.6.post2.tar.gz

Download URL commie-1.0.6.post2.tar.gz
Size 14.6 kB
Tags Source
SHA-256 checksum
How to use checksums
189c428d96dc76e3cc386d4d05e1e2b27650a8e38eddb120baa0abe3dfdb4f6e
BLAKE2b-256 checksum
How to use checksums
30398d61756c0def4efa7f9a358093ad504975cbc048acf1073ff87caa3b3dee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / commie-1.0.6.post2-py3-none-any.whl

Download URL commie-1.0.6.post2-py3-none-any.whl
Size 23.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a51c6addf6827ac09c6bc723386daadedc35e31ade63411036f7398e1acede8d
BLAKE2b-256 checksum
How to use checksums
621da90c66ee3ca5f0b48e1f3dcf0820311e70e13affc43c7a6029a3d5ba2b44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release history Release notifications | RSS feed

This release

1.0.6.post2 This release

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page