Conditions | 11 |
Total Lines | 68 |
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 isomer.database.initialize() 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 |
||
213 | def initialize( |
||
214 | address="127.0.0.1:27017", |
||
215 | database_name="isomer-default", |
||
216 | instance_name="default", |
||
217 | reload=False, |
||
218 | ignore_fail=False, |
||
219 | ): |
||
220 | """Initializes the database connectivity, schemata and finally object models""" |
||
221 | |||
222 | global objectmodels |
||
223 | global collections |
||
224 | global dbhost |
||
225 | global dbport |
||
226 | global dbname |
||
227 | global instance |
||
228 | global initialized |
||
229 | |||
230 | if initialized and not reload: |
||
231 | isolog( |
||
232 | "Already initialized and not reloading.", |
||
233 | lvl=warn, |
||
234 | emitter="DB", |
||
235 | frame_ref=2, |
||
236 | ) |
||
237 | return |
||
238 | |||
239 | dbhost = address.split(":")[0] |
||
240 | dbport = int(address.split(":")[1]) if ":" in address else 27017 |
||
241 | dbname = database_name |
||
242 | |||
243 | db_log("Using database:", dbname, "@", dbhost, ":", dbport) |
||
244 | |||
245 | if dbname == "" and not ignore_fail: |
||
246 | abort(EXIT_NO_DATABASE_DEFINED) |
||
247 | if dbhost == "" and not ignore_fail: |
||
248 | abort(EXIT_NO_DATABASE_HOST) |
||
249 | |||
250 | try: |
||
251 | client = pymongo.MongoClient(host=dbhost, port=dbport) |
||
252 | db = client[dbname] |
||
253 | db_log("Database: ", db.command("buildinfo"), lvl=debug) |
||
254 | except Exception as e: |
||
255 | log_level = warn if ignore_fail else critical |
||
256 | db_log( |
||
257 | "No database available! Check if you have mongodb > 2.2 " |
||
258 | "installed and running as well as listening on port %i " |
||
259 | "of %s and check if you specified the correct " |
||
260 | "instance and environment. Detailed Error:\n%s" % (dbport, dbhost, e), |
||
261 | lvl=log_level, |
||
262 | ) |
||
263 | if not ignore_fail: |
||
264 | abort(EXIT_NO_DATABASE) |
||
265 | else: |
||
266 | return False |
||
267 | |||
268 | formal.connect(database_name, host=dbhost, port=dbport) |
||
269 | formal.connect_sql(database_name, database_type="sql_memory") |
||
270 | |||
271 | schemastore.schemastore = schemastore.build_schemastore_new() |
||
272 | schemastore.l10n_schemastore = schemastore.build_l10n_schemastore( |
||
273 | schemastore.schemastore |
||
274 | ) |
||
275 | objectmodels = _build_model_factories(schemastore.schemastore) |
||
276 | collections = _build_collections(schemastore.schemastore) |
||
277 | instance = instance_name |
||
278 | initialized = True |
||
279 | |||
280 | return True |
||
281 |