Conditions | 16 |
Total Lines | 82 |
Code Lines | 51 |
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.backup() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | #!/usr/bin/env python |
||
118 | def backup( |
||
119 | schema, uuid, export_filter, export_format, filename, pretty, export_all, omit |
||
120 | ): |
||
121 | """Exports all collections to (JSON-) files.""" |
||
122 | |||
123 | from isomer.database import objectmodels |
||
124 | |||
125 | export_format = export_format.upper() |
||
126 | |||
127 | if pretty: |
||
128 | indent = 4 |
||
129 | else: |
||
130 | indent = 0 |
||
131 | |||
132 | f = None |
||
133 | |||
134 | if filename: |
||
135 | try: |
||
136 | f = open(filename, "w") |
||
137 | except (IOError, PermissionError) as e: |
||
138 | backup_log("Could not open output file for writing:", exc=True, lvl=error) |
||
139 | return |
||
140 | |||
141 | def output(what, convert=False): |
||
142 | """Output the backup in a specified format.""" |
||
143 | |||
144 | if convert: |
||
145 | if export_format == "JSON": |
||
146 | data = json.dumps(what, indent=indent) |
||
147 | else: |
||
148 | data = "" |
||
149 | else: |
||
150 | data = what |
||
151 | |||
152 | if not filename: |
||
153 | # Do not use logger here! This data must go immediately to stdout. |
||
154 | print(data) |
||
155 | else: |
||
156 | f.write(data) |
||
157 | |||
158 | if schema is None: |
||
159 | if export_all is False: |
||
160 | backup_log("No schema given.", lvl=warn) |
||
161 | return |
||
162 | else: |
||
163 | schemata = objectmodels.keys() |
||
164 | else: |
||
165 | schemata = [schema] |
||
166 | |||
167 | all_items = {} |
||
168 | |||
169 | for schema_item in schemata: |
||
170 | model = objectmodels[schema_item] |
||
171 | |||
172 | if uuid: |
||
173 | obj = model.find({"uuid": uuid}) |
||
174 | elif export_filter: |
||
175 | obj = model.find(literal_eval(export_filter)) |
||
176 | else: |
||
177 | obj = model.find() |
||
178 | |||
179 | items = [] |
||
180 | for item in obj: |
||
181 | fields = item.serializablefields() |
||
182 | for field in omit: |
||
183 | try: |
||
184 | fields.pop(field) |
||
185 | except KeyError: |
||
186 | pass |
||
187 | items.append(fields) |
||
188 | |||
189 | all_items[schema_item] = items |
||
190 | |||
191 | # if pretty is True: |
||
192 | # output('\n// Objectmodel: ' + schema_item + '\n\n') |
||
193 | # output(schema_item + ' = [\n') |
||
194 | |||
195 | output(all_items, convert=True) |
||
196 | |||
197 | if f is not None: |
||
198 | f.flush() |
||
199 | f.close() |
||
200 | |||
264 |