We are using napolean style docstring for python modules. But there is a need to auto populate additional attributes in the docstring called Data Owner and DAL Owner so that the given function looks like this:
def func(self, arg1=None, arg2=None):
"""
Returns the timeseries for the specified arg1 and arg2.
Args:
arg1: argument 1
arg2: argument 2
Returns:
DataFrame containing timeseries of arg1 for arg2.
DAL Owner: Team IT
Data Owner: Team A
"""
These additional attributes and their values for a given function are provided in a separate csv file. The way I was thinking was to have a script (awk, sed?) that will
Now, this is the part which I havent figured out and dont know the best way forward. For a given function name and owners, I need to go back into the python file and add the owners to the docstring if it exists. I am thinking some sort of awk script but not quite sure
I know this is a lot of steps but can anyone provide insight with the previous 4 bullet points to insert the additional attributes to docstring given the function, attributes and the python file. Will a linux utility like sed, awk be more useful or should I go the python route. Is there some other option that's easier to implement.
The process for assigning a new docstring in an ast is:
ast.get_docstringNone, insert the new node at the start of the parent node's bodyHere's some example code:
$ cat fixdocstrings.py
import ast
import io
from unparse import Unparser
class DocstringWriter(ast.NodeTransformer):
def visit_FunctionDef(self, node):
docstring = ast.get_docstring(node)
new_docstring_node = make_docstring_node(docstring)
if docstring:
# Assumes the existing docstring is the first node
# in the function body.
node.body[0] = new_docstring_node
else:
node.body.insert(0, new_docstring_node)
return node
def make_docstring_node(docstring):
if docstring is None:
content = "A new docstring"
else:
content = docstring + " -- amended"
s = ast.Str(content)
return ast.Expr(value=s)
if __name__ == "__main__":
tree = ast.parse(open("docstringtest.py").read())
transformer = DocstringWriter()
new_tree = transformer.visit(tree)
ast.fix_missing_locations(new_tree)
buf = io.StringIO()
Unparser(new_tree, buf)
buf.seek(0)
print(buf.read())
$ cat docstringtest.py
def foo():
pass
def bar():
"""A docstring."""
$ python fixdocstrings.py
def foo():
'A new docstring'
pass
def bar():
'A docstring. -- amended'
(I answered something similar for myself for python2.7, here)
* As of Python 3.9, the ast module provides an unparse function that can be used instead of the unparse tool: src = ast.unparse(new_tree)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With