Conditions | 19 |
Total Lines | 74 |
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 create_version() 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 | """Versioned mixin class and other utilities.""" |
||
135 | def create_version(obj, session, deleted=False): |
||
136 | obj_mapper = object_mapper(obj) |
||
137 | history_mapper = obj.__history_mapper__ |
||
138 | history_cls = history_mapper.class_ |
||
139 | |||
140 | obj_state = attributes.instance_state(obj) |
||
141 | |||
142 | attr = {} |
||
143 | |||
144 | obj_changed = False |
||
145 | |||
146 | for om, hm in zip(obj_mapper.iterate_to_root(), history_mapper.iterate_to_root()): |
||
147 | if hm.single: |
||
148 | continue |
||
149 | |||
150 | for hist_col in hm.local_table.c: |
||
151 | if _is_versioning_col(hist_col): |
||
152 | continue |
||
153 | |||
154 | obj_col = om.local_table.c[hist_col.key] |
||
155 | |||
156 | # get the value of the |
||
157 | # attribute based on the MapperProperty related to the |
||
158 | # mapped column. this will allow usage of MapperProperties |
||
159 | # that have a different keyname than that of the mapped column. |
||
160 | try: |
||
161 | prop = obj_mapper.get_property_by_column(obj_col) |
||
162 | except UnmappedColumnError: |
||
163 | # in the case of single table inheritance, there may be |
||
164 | # columns on the mapped table intended for the subclass only. |
||
165 | # the "unmapped" status of the subclass column on the |
||
166 | # base class is a feature of the declarative module as of sqla 0.5.2. |
||
167 | continue |
||
168 | |||
169 | # expired object attributes and also deferred cols might not be in the |
||
170 | # dict. force it to load no matter what by using getattr(). |
||
171 | if prop.key not in obj_state.dict: |
||
172 | getattr(obj, prop.key) |
||
173 | |||
174 | a, u, d = attributes.get_history(obj, prop.key) |
||
175 | |||
176 | if d: |
||
177 | attr[hist_col.key] = d[0] |
||
178 | obj_changed = True |
||
179 | elif u: |
||
180 | attr[hist_col.key] = u[0] |
||
181 | else: |
||
182 | # if the attribute had no value. |
||
183 | attr[hist_col.key] = a[0] |
||
184 | obj_changed = True |
||
185 | |||
186 | if not obj_changed: |
||
187 | # not changed, but we have relationships. OK |
||
188 | # check those too |
||
189 | for prop in obj_mapper.iterate_properties: |
||
190 | if isinstance(prop, RelationshipProperty) and \ |
||
191 | attributes.get_history(obj, prop.key, |
||
192 | passive=attributes.PASSIVE_NO_INITIALIZE).has_changes(): |
||
193 | for p in prop.local_columns: |
||
194 | if p.foreign_keys: |
||
195 | obj_changed = True |
||
196 | break |
||
197 | if obj_changed is True: |
||
198 | break |
||
199 | |||
200 | if not obj_changed and not deleted: |
||
201 | return |
||
202 | |||
203 | attr['version'] = obj.version |
||
204 | hist = history_cls() |
||
205 | for key, value in attr.items(): |
||
206 | setattr(hist, key, value) |
||
207 | session.add(hist) |
||
208 | obj.version += 1 |
||
209 | |||
218 |