Conditions | 15 |
Total Lines | 62 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 database.backup.internal_restore() 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 | #!/usr/bin/env python |
||
202 | def internal_restore( |
||
203 | schema, uuid, object_filter, import_format, filename, all_schemata, dry |
||
204 | ): |
||
205 | """Foobar""" |
||
206 | |||
207 | from isomer.database import objectmodels |
||
208 | |||
209 | import_format = import_format.upper() |
||
210 | |||
211 | if import_format == "JSON": |
||
212 | with open(filename, "r") as f: |
||
213 | json_data = f.read() |
||
214 | data = json.loads(json_data) # , parse_float=True, parse_int=True) |
||
215 | else: |
||
216 | backup_log("Importing non json data is WiP!", lvl=error) |
||
217 | return |
||
218 | |||
219 | if schema is None: |
||
220 | if all_schemata is False: |
||
221 | backup_log("No schema given. Read the help", lvl=warn) |
||
222 | return |
||
223 | else: |
||
224 | schemata = data.keys() |
||
225 | else: |
||
226 | schemata = [schema] |
||
227 | |||
228 | if object_filter is not None: |
||
229 | backup_log("Object filtering on import is WiP! Ignoring for now.", lvl=warn) |
||
230 | |||
231 | all_items = {} |
||
232 | total = 0 |
||
233 | |||
234 | for schema_item in schemata: |
||
235 | model = objectmodels[schema_item] |
||
236 | |||
237 | objects = data[schema_item] |
||
238 | items = [] |
||
239 | if uuid: |
||
240 | for item in objects: |
||
241 | if item["uuid"] == uuid: |
||
242 | items = [model(item)] |
||
243 | else: |
||
244 | for item in objects: |
||
245 | thing = model(item) |
||
246 | items.append(thing) |
||
247 | |||
248 | schema_total = len(items) |
||
249 | total += schema_total |
||
250 | |||
251 | if dry: |
||
252 | backup_log("Would import", schema_total, "items of", schema_item) |
||
253 | all_items[schema_item] = items |
||
254 | |||
255 | if dry: |
||
256 | backup_log("Would import", total, "objects.") |
||
257 | else: |
||
258 | backup_log("Importing", total, "objects.") |
||
259 | for schema_name, item_list in all_items.items(): |
||
260 | backup_log("Importing", len(item_list), "objects of type", schema_name) |
||
261 | for item in item_list: |
||
262 | item._fields["_id"] = bson.objectid.ObjectId(item._fields["_id"]) |
||
263 | item.save() |
||
264 |