| Conditions | 14 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | 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:
Complex classes like ExcelReader.set_values_for_variables() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """ |
||
| 191 | def set_values_for_variables(self, key, dictionary): |
||
| 192 | ''' Sets values for variables for a given key ''' |
||
| 193 | # Make sure the sheet has been specified |
||
| 194 | if not self._ws: |
||
| 195 | raise NameError("Sheet not specified") |
||
| 196 | # Check to make sure file was locked before allowing modifications |
||
| 197 | if not self._lock: |
||
| 198 | raise IOError("File not locked for modification") |
||
| 199 | |||
| 200 | # Build a dictionary of variables in the spreadsheet |
||
| 201 | variables = {} |
||
| 202 | col = self._variable_start_column |
||
| 203 | while True: |
||
| 204 | variable = self._ws.cell(column=col, row=self._var_name_row) |
||
| 205 | if variable.value: |
||
| 206 | variables[variable.value] = col |
||
| 207 | else: |
||
| 208 | self._variable_end_column = col |
||
| 209 | break |
||
| 210 | col += 1 |
||
| 211 | |||
| 212 | # Find row for key |
||
| 213 | row = self.get_row_for_key(key) |
||
| 214 | if row == -1: |
||
| 215 | row = self._data_end_row |
||
| 216 | self._ws.cell(column=self._key_column, row=row).value = key |
||
| 217 | self._data_end_row += 1 |
||
| 218 | self._keys[key] = row |
||
| 219 | |||
| 220 | # Fill in values |
||
| 221 | for k, value in dictionary.items(): |
||
| 222 | # is key or value greater the excel 256 character limit? |
||
| 223 | if len(k) > 255: |
||
| 224 | self._unlock_file() |
||
| 225 | raise ValueError("Variable name exceeds 255 characters") |
||
| 226 | if len(k) == 0: |
||
| 227 | self._unlock_file() |
||
| 228 | raise ValueError("Variable name is blank") |
||
| 229 | if len(value) > 32767: |
||
| 230 | self._unlock_file() |
||
| 231 | raise ValueError("Variable value exceeds 32,767 characters") |
||
| 232 | if not k == key: |
||
| 233 | if k in variables: |
||
| 234 | self._ws.cell(column=variables[k], row=row).value = value |
||
| 235 | else: |
||
| 236 | if not self._strict: |
||
| 237 | # Add a new column at the end with variable |
||
| 238 | self._ws.cell(column=self._variable_end_column, |
||
| 239 | row=self._var_name_row).value = k |
||
| 240 | self._ws.cell(column=self._variable_end_column, |
||
| 241 | row=row).value = value |
||
| 242 | self._variable_end_column += 1 |
||
| 243 | if self._variable_end_column > 16384: |
||
| 244 | raise ValueError("Exceeded max # of columns") |
||
| 245 | else: |
||
| 246 | self._unlock_file() |
||
| 247 | raise ValueError("Column '%s' missing in sheet '%s'" % |
||
| 248 | (k, self._sheet_name)) |
||
| 249 | |||
| 252 |