I am new to Python though I have many, many years of programming experience. Using Python 3.6.5 on Fedora 28, I am investigating automatic generation of docstrings. To start with, I have created the following script just to work out some of the details:
Here is the output from running the script:
import ast
import astor
import inspect
source = '''
class cl:
"""This is doc for cl class"""
pass'''
tree = ast.parse(source)
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.ClassDef):
body = node.body[0]
body.value.s += "\n\n More documentation."
doc = ast.get_docstring(node)
print(doc)
print()
src = ast.dump(tree)
print(src)
print()
newsource = astor.to_source(tree)
print(newsource)
print()
newSrc = inspect.cleandoc(newsource)
print(newSrc)At line 15, I am adding text to the docstring for the class and have included 4 spaces at the beginning of the text because that fits the existing source code. How do I determine the indentation in general?Here is the output from running the script:
Output:This is doc for cl class
More documentation.
Module(body=[ClassDef(name='cl', bases=[], keywords=[], body=[Expr(value=Str(s='This is doc for cl class\n\n More documentation.')), Pass()], decorator_list=[])])
class cl:
"""This is doc for cl class
More documentation."""
pass
class cl:
"""This is doc for cl class
More documentation."""
pass
