Hello everyone!
In this task I had to create docstring generator. I'm just wondering how you see this problem, I have to "take data" from the dictionary
{'Args': None, 'Returns': None, 'Raises': None, 'Attributes': None, 'Summary': None, 'Description': None}
and then generate docstring in the correct order.
This is my solution:
import textwrap
def gendoc(config):
docstring = []
if ("Summary" in config) and (config["Summary"] is not None):
docstring.append(config["Summary"])
if ("Args" in config) and (config["Args"] is not None):
args = ["Args:"] + [f"\t{arg}: Function argument" for arg in config["Args"]]
docstring += args
if ("Attributes" in config) and (config["Attributes"] is not None):
attr = ["Attributes:"] + [f"\t{attr}: Information about parameter {attr}" for attr in config["Attributes"]]
docstring += attr
if ("Raises" in config) and (config["Raises"] is not None):
raises = ["Raises: "] + [f"\t {config.get('Raises')}"]
docstring += raises
if ("Returns" in config) and (config["Returns"] is not None):
returns = ["Returns: "] + [f"\t {config.get('Returns')}"]
docstring += returns
if ("Description" in config) and (config["Description"] is not None):
value = config.get('Description')
wrapper=textwrap.TextWrapper(subsequent_indent= '\t', width = 120)
description = ["Description: "] + [f"\t{wrapper.fill(value)}"]
docstring += description
if ("Todo" in config) and (config["Todo"] is not None):
star = "*"
todo = ["Todo:"] + [f"\t{star} {todo}" for todo in config["Todo"]]
docstring += todo
docstring = "\n\n".join(docstring)
return docstring
My output:
The Pear object describes the properties of pears.
Args:
a: Function argument
b: Function argument
Attributes:
a: Information about parameter a
b: Information about parameter b
Raises:
AttributeError: The ``Raises`` section is a list of all exceptions that are relevant to the interface.
Returns:
The return value. True for success, False otherwise.
Description:
The description may span multiple lines. Following lines should be indented. The type is optional. The description may
span multiple lines. Following lines should be indented. The type is optional.The description may span multiple lines.
Following lines should be indented. The type is optional.The description may span multiple lines. Following lines
should be indented. The type is optional.
Todo:
* Do something
* Something else
Any tips how to make this SHORTER and more neat, closer to a advanced or just better solution will be definitely on point. Thanks in advance!
Have a great day!