Python Forum
Algorithm for extracting comments from Python source code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm for extracting comments from Python source code
#1
I never worked with Python before, but now there is a task. There are several Python projects and I need to extract comments from the source code of these projects.

For comments in Python, either # or strings that are not used anywhere are used. If everything is clear with #, then about strings it's not so simple. Since it is necessary to distinguish those strings that are used (for example, for variables or in expressions) from unused strings.

After conducting several experiments in an online compiler, I think about the following algorithm.

1. IF there are no characters (except whitespaces) before the opening quotes (on the line of code where these quotes are)
2. AND IF there are no characters (except whitespaces) after the closing quotes (on the line where these quotes are)
3. AND IF the line is not between parentheses ()
4. AND IF the previous line of code does not end with \
then this is a comment string.

Example:
a = "This is NOT a comment!  "

b = (a 
    + 
    
    """ This is NOT a comment! """
    )

c = a + \
    """ This is NOT a comment!! """ 

'''
And this is already 
 a comment
'''
Please tell me, is this algorithm correct or not? Maybe it needs to be adjusted in some way?"
Reply


Messages In This Thread
Algorithm for extracting comments from Python source code - by Pavel1982 - Feb-28-2024, 10:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  delivery exe without source code py loky62 2 485 Apr-04-2024, 05:47 PM
Last Post: loky62
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 8,107 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,922 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  python move specific files from source to destination including duplicates mg24 3 1,220 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  Python Snippets Source kucingkembar 0 701 Oct-18-2022, 12:50 AM
Last Post: kucingkembar
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,310 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Correct the algorithm of image filter code saoko 6 2,197 May-08-2022, 05:06 PM
Last Post: saoko
  Rock paper scissors in python with "algorithm" Agat0 23 6,559 Mar-01-2022, 03:20 PM
Last Post: Agat0
  Long-term stable source to get news headlines with Python? sandufi 4 2,098 Dec-23-2021, 09:48 AM
Last Post: sandufi
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,419 Dec-04-2021, 08:33 PM
Last Post: Ascalon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020