Created
May 5, 2020 12:41
-
-
Save jamescalam/03ec03bfa857e542ca40dc0244f24dd8 to your computer and use it in GitHub Desktop.
Code snippet demonstrating how to match parameter details from function docstrings formatted to numpy/scipy docstring standards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # first run parameter match regex | |
| param_re = re.compile(r"(?s)\w+ : .*?(?=\w+ :)") | |
| # the above will not find the final parameter, this will | |
| param_re2 = re.compile(r"(?s)\w+ : .*") | |
| params = [] # initialise parameter list | |
| while True: | |
| # find a parameter | |
| new_param = param_re.search(text) | |
| if new_param is None: | |
| # if no parameters have been found, try with modified regex | |
| new_param = param_re2.search(text) | |
| if new_param is None: | |
| # if we still cannot find any new parameters, break | |
| break | |
| # we have a parameter so add to list | |
| params.append(new_param.group()) | |
| # remove from text so that we don't keep matching the same parameter | |
| text = text.replace(new_param.group(), "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment