| Conditions | 17 |
| Total Lines | 117 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 1 |
| CRAP Score | 288.3388 |
| Changes | 5 | ||
| 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 _generate_triggers() 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 ~*~ |
||
| 42 | 1 | def _generate_triggers(osmalchemy, maxage=60*60*24): |
|
| 43 | """ Generates the triggers for online functionality. |
||
| 44 | |||
| 45 | osmalchemy - reference to the OSMAlchemy instance to be configured |
||
| 46 | maxage - maximum age of objects before they are updated online, in seconds |
||
| 47 | """ |
||
| 48 | |||
| 49 | _visited_queries = WeakSet() |
||
| 50 | |||
| 51 | @listens_for(Query, "before_compile") |
||
| 52 | def _query_compiling(query): |
||
| 53 | # Get the session associated with the query: |
||
| 54 | session = query.session |
||
| 55 | |||
| 56 | # Prevent recursion by skipping already-seen queries |
||
| 57 | if query in _visited_queries: |
||
| 58 | return |
||
| 59 | else: |
||
| 60 | _visited_queries.add(query) |
||
| 61 | |||
| 62 | # Check whether this query affects our model |
||
| 63 | affected_models = set([c["type"] for c in query.column_descriptions]) |
||
| 64 | our_models = set([osmalchemy.Node, osmalchemy.Way, osmalchemy.Relation, |
||
| 65 | osmalchemy.Element]) |
||
| 66 | if affected_models.isdisjoint(our_models): |
||
| 67 | # None of our models is affected |
||
| 68 | return |
||
| 69 | |||
| 70 | # Check whether this query filters elements |
||
| 71 | # Online update will only run on a specified set, not all data |
||
| 72 | if query.whereclause is None: |
||
| 73 | # No filters |
||
| 74 | return |
||
| 75 | |||
| 76 | # Define operator to string mapping |
||
| 77 | _ops = {operator.eq: "==", |
||
| 78 | operator.ne: "!=", |
||
| 79 | operator.lt: "<", |
||
| 80 | operator.gt: ">", |
||
| 81 | operator.le: "<=", |
||
| 82 | operator.ge: ">=", |
||
| 83 | operator.and_: "&&", |
||
| 84 | operator.or_: "||"} |
||
| 85 | |||
| 86 | # Traverse whereclause recursively |
||
| 87 | def _analyse_clause(clause, target): |
||
| 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 | # Only use if we are looking for this model |
||
| 101 | if model is target: |
||
| 102 | # Store field name |
||
| 103 | left = 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, target) |
||
| 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 | # Analyse where clause looking for all looked-up fields |
||
| 156 | tree = {} |
||
| 157 | for target in our_models.intersection(affected_models): |
||
| 158 | tree[target.__name__] = _analyse_clause(query.whereclause, target) |
||
| 159 |