Piyush Divyanakar
Piyush Divyanakar

Reputation: 241

Extract docstring form a python file using grep or awk

I want to extract all docstrings from my python file using grep or awk. I tried

cat test.py | grep """[\w\W]*?"""

But I see no output. Say the the test test.py looks like this.

import libraries

class MyClass(object):
    """Docstring to this class. 
       second line of docstring."""

    def myClassMethod(a,b):
        """Docstring of the method. 
           another line in docstring of the method."""
        return a + b

Then the output should be all that is enclosed in triple quotes.

"""Docstring to this class. 
second line of docstring."""
"""Docstring of the method. 
another line in docstring of the method."""

Upvotes: 2

Views: 1669

Answers (2)

randomir
randomir

Reputation: 18687

The proper way to extract docstrings from Python code is via actual Python parser (the ast module):

#!/usr/bin/env python
import ast

with open('/path/to/file') as f:
    code = ast.parse(f.read())

for node in ast.walk(code):
    if isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Module)):
        docstring = ast.get_docstring(node)
        if docstring:
            print(repr(docstring))

Run on your sample will output:

'Docstring to this class. \nsecond line of docstring.'
'Docstring of the method. \nanother line in docstring of the method.'

Just for fun, we can do also do it with GNU awk:

$ awk -v RS= -v FPAT="'''.*'''|"'""".*"""' '{print $1}' file
"""Docstring to this class. 
       second line of docstring."""
"""Docstring of the method. 
           another line in docstring of the method."""

Upvotes: 2

making_code
making_code

Reputation: 159

With P(perl) grep you can do the following:

grep -Poz '"""[^"]+"""' test.py

Output:

"""Docstring to this class. 
       second line of docstring.""""""Docstring of the method. 
           another line in docstring of the method."""

Upvotes: 1

Related Questions