Conditions | 10 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 110 |
Changes | 1 | ||
Bugs | 0 | Features | 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 _analyse_clause() 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 | # ~*~ coding: utf-8 ~*~ |
||
87 | def _analyse_clause(clause): |
||
88 | if type(clause) is BinaryExpression: |
||
89 | # This is something like "latitude >= 51.0" |
||
90 | left = clause.left |
||
91 | right = clause.right |
||
92 | op = clause.operator |
||
93 | |||
94 | # Left part should be a column |
||
95 | if type(left) is AnnotatedColumn: |
||
96 | # Get table class and field |
||
97 | model = left._annotations["parentmapper"].class_ |
||
98 | field = left |
||
99 | |||
100 | # Double-check this model belongs to us |
||
101 | if model in our_models: |
||
102 | # Convert model class and field to string names |
||
103 | left = (model.__name__, field.name) |
||
104 | else: |
||
105 | return None |
||
106 | else: |
||
107 | # Right now, we cannot cope with anything but a column on the left |
||
108 | return None |
||
109 | |||
110 | # Right part should be a literal value |
||
111 | if type(right) is BindParameter: |
||
112 | # Extract literal value |
||
113 | right = right.value |
||
114 | else: |
||
115 | # Right now, we cannot cope with something else here |
||
116 | return None |
||
117 | |||
118 | # Look for a known operator |
||
119 | if op in _ops.keys(): |
||
120 | # Get string representation |
||
121 | op = _ops[op] |
||
122 | else: |
||
123 | # Right now, we cannot cope with other operators |
||
124 | return None |
||
125 | |||
126 | # Return polish notation tuple of this clause |
||
127 | return (op, left, right) |
||
128 | elif type(clause) is BooleanClauseList: |
||
129 | # This is an AND or OR operation |
||
130 | op = clause.operator |
||
131 | clauses = [] |
||
132 | |||
133 | # Iterate over all the clauses in this operation |
||
134 | for clause in clause.clauses: |
||
135 | # Recursively analyse clauses |
||
136 | res = _analyse_clause(clause) |
||
137 | # None is returned for unsupported clauses or operations |
||
138 | if res is not None: |
||
139 | # Append polish notation result to clauses list |
||
140 | clauses.append(res) |
||
141 | |||
142 | # Look for a known operator |
||
143 | if op in _ops.keys(): |
||
144 | # Get string representation |
||
145 | op = _ops[op] |
||
146 | else: |
||
147 | # Right now, we cannot cope with anything else |
||
148 | return None |
||
149 | |||
150 | # Return polish notation tuple of this clause |
||
151 | return (op, clauses) |
||
152 | else: |
||
153 | # We hit an unsupported type of clause |
||
154 | return None |
||
155 | tree = _analyse_clause(query.whereclause) |
||
156 |