Skip to content

Commit cd4a994

Browse files
committed
added remove_summary option to get_docstring
1 parent 52851f4 commit cd4a994

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/labthings/utilities.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,21 @@ def clean_url_string(url: str) -> str:
144144
return url
145145

146146

147-
def get_docstring(obj: Any, remove_newlines=True) -> str:
147+
def get_docstring(obj: Any, remove_newlines=True, remove_summary=False) -> str:
148148
"""Return the docstring of an object
149149
150+
If `remove_newlines` is `True` (default), newlines are removed from the string.
151+
If `remove_summary` is `True` (not default), and the docstring's second line
152+
is blank, the first two lines are removed. If the docstring follows the
153+
convention of a one-line summary, a blank line, and a description, this will
154+
get just the description.
155+
156+
If `remove_newlines` is `False`, the docstring is processed by
157+
`inspect.cleandoc()` to remove whitespace from the start of each line.
158+
150159
:param obj: Any Python object
151160
:param remove_newlines: bool (Default value = True)
161+
:param remove_summary: bool (Default value = False)
152162
:returns: str: Object docstring
153163
154164
"""
@@ -158,6 +168,10 @@ def get_docstring(obj: Any, remove_newlines=True) -> str:
158168
if remove_newlines:
159169
stripped = [line.strip() for line in ds.splitlines() if line]
160170
return " ".join(stripped).replace("\n", " ").replace("\r", "")
171+
if remove_summary:
172+
lines = ds.splitlines()
173+
if len(lines) > 2 and lines[1].strip() == "":
174+
ds = "\n".join(lines[2:])
161175
return inspect.cleandoc(ds) # Strip spurious indentation/newlines
162176

163177

0 commit comments

Comments
 (0)