|
1 | 1 | """Utility functions & decorators for the module.""" |
2 | 2 |
|
3 | | -from importlib import import_module |
| 3 | +from copy import deepcopy |
4 | 4 | from functools import wraps |
| 5 | +from importlib import import_module |
5 | 6 |
|
6 | 7 | ################################################################################################### |
7 | 8 | ################################################################################################### |
@@ -138,6 +139,42 @@ def docs_drop_param(docstring): |
138 | 139 | return front + back |
139 | 140 |
|
140 | 141 |
|
| 142 | +def docs_replace_param(docstring, replace, new_param): |
| 143 | + """Replace a parameter description in a docstring. |
| 144 | +
|
| 145 | + Parameters |
| 146 | + ---------- |
| 147 | + docstring : str |
| 148 | + Docstring to replace parameter description within. |
| 149 | + replace : str |
| 150 | + The name of the parameter to switch out. |
| 151 | + new_param : str |
| 152 | + The new parameter description to replace into the docstring. |
| 153 | + This should be a string structured to be copied directly into the docstring. |
| 154 | +
|
| 155 | + Returns |
| 156 | + ------- |
| 157 | + new_docstring : str |
| 158 | + Update docstring, with parameter switched out. |
| 159 | + """ |
| 160 | + |
| 161 | + # Take a copy to make sure to avoid any potential aliasing |
| 162 | + docstring = deepcopy(docstring) |
| 163 | + |
| 164 | + # Find the index where the param to replace is |
| 165 | + p_ind = docstring.find(replace) |
| 166 | + |
| 167 | + # Find the second newline (end of to-replace param) |
| 168 | + ti = docstring[p_ind:].find('\n') |
| 169 | + n_ind = docstring[p_ind + ti + 1:].find('\n') |
| 170 | + end_ind = p_ind + ti + 1 + n_ind |
| 171 | + |
| 172 | + # Reconstitute docstring, replacing specified parameter |
| 173 | + new_docstring = docstring[:p_ind] + new_param + docstring[end_ind:] |
| 174 | + |
| 175 | + return new_docstring |
| 176 | + |
| 177 | + |
141 | 178 | def docs_append_to_section(docstring, section, add): |
142 | 179 | """Append extra information to a specified section of a docstring. |
143 | 180 |
|
|
0 commit comments