Skip to content

Commit 5f1d94a

Browse files
committed
add helper func to replace docstring params
1 parent 1a768e5 commit 5f1d94a

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

specparam/core/modutils.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Utility functions & decorators for the module."""
22

3-
from importlib import import_module
3+
from copy import deepcopy
44
from functools import wraps
5+
from importlib import import_module
56

67
###################################################################################################
78
###################################################################################################
@@ -138,6 +139,42 @@ def docs_drop_param(docstring):
138139
return front + back
139140

140141

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+
141178
def docs_append_to_section(docstring, section, add):
142179
"""Append extra information to a specified section of a docstring.
143180

specparam/tests/core/test_modutils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ def test_docs_drop_param(tdocstring):
3939
assert 'first' not in out
4040
assert 'second' in out
4141

42+
def test_docs_replace_param(tdocstring):
43+
44+
new_param = 'updated : other\n This description has been dropped in.'
45+
46+
ndocstring = docs_replace_param(tdocstring, 'first', new_param)
47+
assert 'updated' in ndocstring
48+
assert 'first' not in ndocstring
49+
assert 'second' in ndocstring
50+
51+
ndocstring = docs_replace_param(tdocstring, 'second', new_param)
52+
assert 'updated' in ndocstring
53+
assert 'first' in ndocstring
54+
assert 'second' not in ndocstring
55+
4256
def test_docs_append_to_section(tdocstring):
4357

4458
section = 'Parameters'

0 commit comments

Comments
 (0)