| Conditions | 8 |
| Total Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import re |
||
| 111 | def extract_documentation_with_markers(content, markers): |
||
| 112 | """ |
||
| 113 | Extracts all documentation texts inside the given source-code-string. |
||
| 114 | |||
| 115 | :param content: The source-code-string where to extract documentation from. |
||
| 116 | Needs to be a list or tuple where each string item is a |
||
| 117 | single line (including ending whitespaces like `\\n`). |
||
| 118 | :param markers: The list/tuple of marker-sets that identify a |
||
| 119 | documentation-comment. Low-index markers have higher |
||
| 120 | priority than high-index markers. |
||
| 121 | :return: An iterator returning each DocumentationComment found in |
||
| 122 | the content. |
||
| 123 | """ |
||
| 124 | # Prepare marker-tuple dict that maps a begin pattern to the corresponding |
||
| 125 | # marker_set(s). This makes it faster to retrieve a marker-set from a |
||
| 126 | # begin sequence we initially want to search for in source code. Then |
||
| 127 | # the possible found documentation match is processed further with the |
||
| 128 | # rest markers. |
||
| 129 | marker_dict = {} |
||
| 130 | for marker_set in markers: |
||
| 131 | if marker_set[0] not in marker_dict: |
||
| 132 | marker_dict[marker_set[0]] = [marker_set] |
||
| 133 | else: |
||
| 134 | marker_dict[marker_set[0]].append(marker_set) |
||
| 135 | |||
| 136 | # Using regexes to perform a variable match is faster than finding each |
||
| 137 | # substring with `str.find()` choosing the lowest match. |
||
| 138 | begin_regex = _compile_multi_match_regex( |
||
| 139 | marker_set[0] for marker_set in markers) |
||
| 140 | |||
| 141 | line = 0 |
||
| 142 | column = 0 |
||
| 143 | while line < len(content): |
||
| 144 | begin_match = begin_regex.search(content[line], column) |
||
| 145 | if begin_match: |
||
| 146 | column = begin_match.end() |
||
| 147 | for marker in marker_dict[begin_match.group()]: |
||
| 148 | doccomment = _extract_doccomment(content, line, column, marker) |
||
| 149 | if doccomment is not None: |
||
| 150 | start_position = TextPosition(line + 1, |
||
| 151 | begin_match.start() + 1) |
||
| 152 | line, column, doccomment = doccomment |
||
| 153 | end_position = TextPosition(line + 1, column + 1) |
||
| 154 | |||
| 155 | yield DocumentationComment(doccomment, |
||
| 156 | marker, |
||
| 157 | TextRange(start_position, |
||
| 158 | end_position)) |
||
| 159 | break |
||
| 160 | else: |
||
| 161 | line += 1 |
||
| 162 | column = 0 |
||
| 163 | |||
| 195 |