Conditions | 11 |
Total Lines | 80 |
Code Lines | 59 |
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._build_collections() 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 |
||
131 | def _build_collections(store): |
||
132 | """Generate database collections with indices from the schemastore""" |
||
133 | |||
134 | result = {} |
||
135 | |||
136 | client = pymongo.MongoClient(host=dbhost, port=dbport) |
||
137 | db = client[dbname] |
||
138 | |||
139 | for schemaname in store: |
||
140 | |||
141 | schema = None |
||
142 | indices = None |
||
143 | |||
144 | try: |
||
145 | schema = store[schemaname]["schema"] |
||
146 | indices = store[schemaname].get("indices", None) |
||
147 | except KeyError: |
||
148 | db_log("No schema found for ", schemaname, lvl=critical) |
||
149 | |||
150 | try: |
||
151 | result[schemaname] = db[schemaname] |
||
152 | except Exception: |
||
153 | db_log( |
||
154 | "Could not get collection for schema ", |
||
155 | schemaname, |
||
156 | schema, |
||
157 | lvl=critical, |
||
158 | exc=True, |
||
159 | ) |
||
160 | |||
161 | if indices is None: |
||
162 | continue |
||
163 | |||
164 | col = db[schemaname] |
||
165 | db_log("Adding indices to", schemaname, lvl=debug) |
||
166 | i = 0 |
||
167 | keys = list(indices.keys()) |
||
168 | |||
169 | while i < len(indices): |
||
170 | index_name = keys[i] |
||
171 | index = indices[index_name] |
||
172 | |||
173 | index_type = index.get("type", None) |
||
174 | index_unique = index.get("unique", False) |
||
175 | index_sparse = index.get("sparse", True) |
||
176 | index_reindex = index.get("reindex", False) |
||
177 | |||
178 | if index_type in (None, "text"): |
||
179 | index_type = pymongo.TEXT |
||
180 | elif index_type == "2dsphere": |
||
181 | index_type = pymongo.GEOSPHERE |
||
182 | |||
183 | def do_index(): |
||
184 | """Ensure index on a data class""" |
||
185 | col.ensure_index( |
||
186 | [(index_name, index_type)], unique=index_unique, sparse=index_sparse |
||
187 | ) |
||
188 | |||
189 | db_log("Enabling index of type", index_type, "on", index_name, lvl=debug) |
||
190 | try: |
||
191 | do_index() |
||
192 | i += 1 |
||
193 | except pymongo.errors.OperationFailure: |
||
194 | db_log(col.list_indexes().__dict__, pretty=True, lvl=verbose) |
||
195 | if not index_reindex: |
||
196 | db_log("Index was not created!", lvl=warn) |
||
197 | i += 1 |
||
198 | else: |
||
199 | try: |
||
200 | col.drop_index(index_name) |
||
201 | do_index() |
||
202 | i += 1 |
||
203 | except pymongo.errors.OperationFailure as e: |
||
204 | db_log("Index recreation problem:", exc=True, lvl=error) |
||
205 | col.drop_indexes() |
||
206 | i = 0 |
||
207 | |||
208 | # for index in col.list_indexes(): |
||
209 | # db_log("Index: ", index) |
||
210 | return result |
||
211 | |||
281 |