Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import inspect
- from importlib import import_module
- fp = r"scratch_1.py"
- module_name = inspect.getmodulename(fp)
- module = import_module(module_name)
- funcs = tuple((k, v) for k, v in inspect.getmembers(module) if inspect.isfunction(v))
- for func_name, func in funcs:
- lines, offset = inspect.getsourcelines(func)
- doc_string = inspect.getdoc(func)
- print(f'function: {func_name}, id: {func} start at line {offset}.')
- print(f'doc string is: {repr(doc_string)}')
- def_index = None
- doc_index_s = None
- doc_index_e = None
- for sub_index, line in enumerate(lines):
- # detect def line ends
- if def_index is None:
- m = re.search(r'\):\s*(#.+)?\n', line)
- if m:
- def_index = sub_index
- continue
- # detect no docstring
- if doc_index_s is None and re.search(r'^\s*\w', line):
- break
- # detect docstring start
- if doc_index_s is None:
- if re.search(r'^\s+["\']{3}.*(#.+)?\n', line):
- doc_index_s = sub_index
- continue
- elif re.search(r'^\s+["\']', line):
- doc_index_s = sub_index
- doc_index_e = sub_index
- break
- # detect docstring end
- if doc_index_s:
- if re.search(r'(?!=\\)[\'"]\s*(#.+)?\n', line):
- doc_index_e = sub_index
- # break if doc string collected
- if doc_index_s and doc_index_e:
- break
- print('Doc string lines are:')
- print(''.join(f'[{i:>2}] {line}' for i, line in enumerate(lines[doc_index_s:doc_index_e+1], start=offset+doc_index_s)))
- pass
- if __name__ == '__main__':
- pass
Add Comment
Please, Sign In to add comment