Get the identifiers and theirs docstring from python source code. :return dict:
(code)
| 24 | |
| 25 | |
| 26 | def identifiers_info(code): |
| 27 | """Get the identifiers and theirs docstring from python source code. |
| 28 | |
| 29 | :return dict: |
| 30 | """ |
| 31 | try: |
| 32 | tree = ast.parse(code) |
| 33 | except Exception: |
| 34 | return {} |
| 35 | |
| 36 | if not isinstance(tree, ast.Module): |
| 37 | return {} |
| 38 | |
| 39 | identifier2doc = {} |
| 40 | for node in tree.body: |
| 41 | if isinstance(node, ast.Assign): |
| 42 | for name in node.targets: |
| 43 | if hasattr(name, 'id'): |
| 44 | identifier2doc[name.id] = '' |
| 45 | elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): |
| 46 | doc_string = ast.get_docstring(node) or '' |
| 47 | title = doc_string.split('\n\n')[0] |
| 48 | identifier2doc[node.name] = title |
| 49 | |
| 50 | return identifier2doc |
| 51 | |
| 52 | |
| 53 | def valid_and_norm_path(base, subpath): |
no outgoing calls
no test coverage detected
searching dependent graphs…