| Conditions | 7 |
| Total Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 | from collections import namedtuple |
||
| 79 | def _parse_documentation_with_symbols(self, param_identifiers, |
||
| 80 | return_identifiers): |
||
| 81 | """ |
||
| 82 | Parses documentation based on parameter and return symbols. |
||
| 83 | |||
| 84 | :param param_identifiers: |
||
| 85 | A tuple of two strings with which a parameter starts and ends. |
||
| 86 | :param return_identifiers: |
||
| 87 | The string with which a return description starts. |
||
| 88 | :return: |
||
| 89 | The list of all the parsed sections of the documentation. Every |
||
| 90 | section is a namedtuple of either ``Description`` or ``Parameter`` |
||
| 91 | or ``ReturnValue``. |
||
| 92 | """ |
||
| 93 | lines = self.documentation.splitlines(keepends=True) |
||
| 94 | |||
| 95 | parse_mode = self.Description |
||
| 96 | |||
| 97 | cur_param = "" |
||
| 98 | |||
| 99 | desc = "" |
||
| 100 | parsed = [] |
||
| 101 | |||
| 102 | for line in lines: |
||
| 103 | |||
| 104 | stripped_line = line.strip() |
||
| 105 | |||
| 106 | if stripped_line.startswith(param_identifiers[0]): |
||
| 107 | parse_mode = self.Parameter |
||
| 108 | param_offset = line.find( |
||
| 109 | param_identifiers[0]) + len(param_identifiers[0]) |
||
| 110 | splitted = line[param_offset:].split(param_identifiers[1], 1) |
||
| 111 | cur_param = splitted[0].strip() |
||
| 112 | |||
| 113 | param_desc = splitted[1] |
||
| 114 | parsed.append(self.Parameter(name=cur_param, desc=param_desc)) |
||
| 115 | |||
| 116 | elif stripped_line.startswith(return_identifiers): |
||
| 117 | parse_mode = self.ReturnValue |
||
| 118 | return_offset = line.find( |
||
| 119 | return_identifiers) + len(return_identifiers) |
||
| 120 | retval_desc = line[return_offset:] |
||
| 121 | parsed.append(self.ReturnValue(desc=retval_desc)) |
||
| 122 | |||
| 123 | elif parse_mode == self.ReturnValue: |
||
| 124 | retval_desc += line |
||
| 125 | parsed.pop() |
||
| 126 | parsed.append(self.ReturnValue(desc=retval_desc)) |
||
| 127 | |||
| 128 | elif parse_mode == self.Parameter: |
||
| 129 | param_desc += line |
||
| 130 | parsed.pop() |
||
| 131 | parsed.append(self.Parameter(name=cur_param, desc=param_desc)) |
||
| 132 | |||
| 133 | else: |
||
| 134 | desc += line |
||
| 135 | # This is inside a try-except for cases where the list |
||
| 136 | # is empty and has nothing to pop. |
||
| 137 | try: |
||
| 138 | parsed.pop() |
||
| 139 | except IndexError: |
||
| 140 | pass |
||
| 141 | parsed.append(self.Description(desc=desc)) |
||
| 142 | |||
| 143 | return parsed |
||
| 144 |