| Conditions | 12 |
| Total Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 156 |
| Changes | 13 | ||
| 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 _query_compiling() 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 ~*~ |
||
| 91 | @listens_for(Query, "before_compile") |
||
| 92 | def _query_compiling(query): |
||
| 93 | # Get the session associated with the query: |
||
| 94 | session = query.session |
||
| 95 | |||
| 96 | # Skip if the session was in a trigger before |
||
| 97 | # Prevents recursion in import code |
||
| 98 | if hasattr(session, "_osmalchemy_in_trigger"): |
||
| 99 | return |
||
| 100 | |||
| 101 | # Prevent recursion by skipping already-seen queries |
||
| 102 | if query in _visited_queries: |
||
| 103 | return |
||
| 104 | else: |
||
| 105 | _visited_queries.add(query) |
||
| 106 | |||
| 107 | oa.logger.debug("Analysing new ORM query.") |
||
| 108 | |||
| 109 | # Check whether this query affects our model |
||
| 110 | affected_models = set([c["type"] for c in query.column_descriptions]) |
||
| 111 | our_models = set([oa.node, oa.way, oa.relation, |
||
| 112 | oa.element]) |
||
| 113 | if affected_models.isdisjoint(our_models): |
||
| 114 | # None of our models is affected |
||
| 115 | oa.logger.debug("None of our models are affected.") |
||
| 116 | return |
||
| 117 | |||
| 118 | # Check whether this query filters elements |
||
| 119 | # Online update will only run on a specified set, not all data |
||
| 120 | if query.whereclause is None: |
||
| 121 | # No filters |
||
| 122 | oa.logger.debug("No filters found in query.") |
||
| 123 | return |
||
| 124 | |||
| 125 | oa.logger.debug("Building query tree from ORM query structure.") |
||
| 126 | |||
| 127 | # Analyse where clause looking for all looked-up fields |
||
| 128 | trees = {} |
||
| 129 | for target in our_models.intersection(affected_models): |
||
| 130 | # Build expression trees first |
||
| 131 | tree = _where_to_tree(query.whereclause, target) |
||
| 132 | if not tree is None: |
||
| 133 | trees[target.__name__] = tree |
||
| 134 | |||
| 135 | # Bail out if no relevant trees were built |
||
| 136 | if not trees: |
||
| 137 | oa.logger.debug("No relevant query trees built.") |
||
| 138 | return |
||
| 139 | |||
| 140 | # Compile to OverpassQL |
||
| 141 | oql = _trees_to_overpassql(trees) |
||
| 142 | oa.logger.debug("Compiled OverpassQL: %s" % oql) |
||
| 143 | |||
| 144 | # Look up query in cache |
||
| 145 | hashed_oql = md5(_normalise_overpassql(oql).encode()).hexdigest() |
||
| 146 | cached_query = session.query(oa.cached_query).filter_by(oql_hash=hashed_oql).scalar() |
||
| 147 | # Check age if cached query was found |
||
| 148 | if cached_query: |
||
| 149 | timediff = datetime.datetime.now() - cached_query.oql_queried |
||
| 150 | if timediff.total_seconds() < oa.maxage: |
||
| 151 | # Return and do nothing if query was run recently |
||
| 152 | oa.logger.debug("Query was run online only %i seconds ago, not running." % timediff.total_seconds()) |
||
| 153 | return |
||
| 154 | |||
| 155 | # Run query online |
||
| 156 | oa.logger.debug("Running Overpass query.") |
||
| 157 | xml = _get_elements_by_query(oa.overpass, oql) |
||
| 158 | |||
| 159 | # Import data |
||
| 160 | session._osmalchemy_in_trigger = True |
||
| 161 | _import_osm_xml(oa, xml, session=session) |
||
| 162 | del session._osmalchemy_in_trigger |
||
| 163 | |||
| 164 | # Store or update query time |
||
| 165 | if not cached_query: |
||
| 166 | cached_query = oa.cached_query() |
||
| 167 | cached_query.oql_hash = hashed_oql |
||
| 168 | cached_query.oql_queried = datetime.datetime.now() |
||
| 169 | session.add(cached_query) |
||
| 170 | session.commit() |
||
| 171 | oa.logger.debug("Query cached.") |
||
| 172 |