| Conditions | 16 | 
| Total Lines | 95 | 
| Lines | 0 | 
| Ratio | 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 _history_mapper() 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."""  | 
            ||
| 23 | def _history_mapper(local_mapper):  | 
            ||
| 24 | cls = local_mapper.class_  | 
            ||
| 25 | |||
| 26 | # set the "active_history" flag  | 
            ||
| 27 | # on on column-mapped attributes so that the old version  | 
            ||
| 28 | # of the info is always loaded (currently sets it on all attributes)  | 
            ||
| 29 | for prop in local_mapper.iterate_properties:  | 
            ||
| 30 | getattr(local_mapper.class_, prop.key).impl.active_history = True  | 
            ||
| 31 | |||
| 32 | super_mapper = local_mapper.inherits  | 
            ||
| 33 | super_history_mapper = getattr(cls, '__history_mapper__', None)  | 
            ||
| 34 | |||
| 35 | polymorphic_on = None  | 
            ||
| 36 | super_fks = []  | 
            ||
| 37 | |||
| 38 | def _col_copy(col):  | 
            ||
| 39 | col = col.copy()  | 
            ||
| 40 | col.unique = False  | 
            ||
| 41 | col.default = col.server_default = None  | 
            ||
| 42 | return col  | 
            ||
| 43 | |||
| 44 | if not super_mapper or local_mapper.local_table is not super_mapper.local_table:  | 
            ||
| 45 | cols = []  | 
            ||
| 46 | for column in local_mapper.local_table.c:  | 
            ||
| 47 | if _is_versioning_col(column):  | 
            ||
| 48 | continue  | 
            ||
| 49 | |||
| 50 | col = _col_copy(column)  | 
            ||
| 51 | |||
| 52 | if super_mapper and col_references_table(column, super_mapper.local_table):  | 
            ||
| 53 | super_fks.append((col.key, list(super_history_mapper.local_table.primary_key)[0]))  | 
            ||
| 54 | |||
| 55 | cols.append(col)  | 
            ||
| 56 | |||
| 57 | if column is local_mapper.polymorphic_on:  | 
            ||
| 58 | polymorphic_on = col  | 
            ||
| 59 | |||
| 60 | if super_mapper:  | 
            ||
| 61 |             super_fks.append(('version', super_history_mapper.local_table.c.version)) | 
            ||
| 62 | |||
| 63 |         version_meta = {"version_meta": True}  # add column.info to identify | 
            ||
| 64 | # columns specific to versioning  | 
            ||
| 65 | |||
| 66 | # "version" stores the integer version id. This column is  | 
            ||
| 67 | # required.  | 
            ||
| 68 |         cols.append(Column('version', Integer, primary_key=True, | 
            ||
| 69 | autoincrement=False, info=version_meta))  | 
            ||
| 70 | |||
| 71 | # "changed" column stores the UTC timestamp of when the  | 
            ||
| 72 | # history row was created.  | 
            ||
| 73 | # This column is optional and can be omitted.  | 
            ||
| 74 | model_name = cls.__name__.lower()  | 
            ||
| 75 |         cols.append(Column('%s_changed_at' % model_name, ArrowType, | 
            ||
| 76 | default=datetime.datetime.utcnow,  | 
            ||
| 77 | info=version_meta))  | 
            ||
| 78 |         cols.append(Column('%s_changed_by' % model_name, Integer, ForeignKey("users.id"), | 
            ||
| 79 | info=version_meta))  | 
            ||
| 80 | |||
| 81 | if super_fks:  | 
            ||
| 82 | cols.append(ForeignKeyConstraint(*zip(*super_fks)))  | 
            ||
| 83 | |||
| 84 | table = Table(local_mapper.local_table.name + '_history',  | 
            ||
| 85 | local_mapper.local_table.metadata,  | 
            ||
| 86 | *cols,  | 
            ||
| 87 | schema=local_mapper.local_table.schema  | 
            ||
| 88 | )  | 
            ||
| 89 | else:  | 
            ||
| 90 | # single table inheritance. take any additional columns that may have  | 
            ||
| 91 | # been added and add them to the history table.  | 
            ||
| 92 | for column in local_mapper.local_table.c:  | 
            ||
| 93 | if column.key not in super_history_mapper.local_table.c:  | 
            ||
| 94 | col = _col_copy(column)  | 
            ||
| 95 | super_history_mapper.local_table.append_column(col)  | 
            ||
| 96 | table = None  | 
            ||
| 97 | |||
| 98 | if super_history_mapper:  | 
            ||
| 99 | bases = (super_history_mapper.class_,)  | 
            ||
| 100 | else:  | 
            ||
| 101 | bases = local_mapper.base_mapper.class_.__bases__  | 
            ||
| 102 |     versioned_cls = type.__new__(type, "%sHistory" % cls.__name__, bases, {}) | 
            ||
| 103 | |||
| 104 | m = mapper(  | 
            ||
| 105 | versioned_cls,  | 
            ||
| 106 | table,  | 
            ||
| 107 | inherits=super_history_mapper,  | 
            ||
| 108 | polymorphic_on=polymorphic_on,  | 
            ||
| 109 | polymorphic_identity=local_mapper.polymorphic_identity  | 
            ||
| 110 | )  | 
            ||
| 111 | cls.__history_mapper__ = m  | 
            ||
| 112 | |||
| 113 | if not super_history_mapper:  | 
            ||
| 114 | local_mapper.local_table.append_column(  | 
            ||
| 115 |             Column('version', Integer, default=1, nullable=False) | 
            ||
| 116 | )  | 
            ||
| 117 |         local_mapper.add_property("version", local_mapper.local_table.c.version) | 
            ||
| 118 | |||
| 218 |