| Conditions | 10 |
| Total Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
| Changes | 10 | ||
| 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 ~*~ |
||
| 48 | @listens_for(Query, "before_compile") |
||
| 49 | def _query_compiling(query): |
||
| 50 | # Get the session associated with the query: |
||
| 51 | session = query.session |
||
| 52 | |||
| 53 | # Skip if the session was in a trigger before |
||
| 54 | # Prevents recursion in import code |
||
| 55 | if hasattr(session, "_osmalchemy_in_trigger"): |
||
| 56 | return |
||
| 57 | |||
| 58 | # Prevent recursion by skipping already-seen queries |
||
| 59 | if query in _visited_queries: |
||
| 60 | return |
||
| 61 | else: |
||
| 62 | _visited_queries.add(query) |
||
| 63 | |||
| 64 | # Check whether this query affects our model |
||
| 65 | affected_models = set([c["type"] for c in query.column_descriptions]) |
||
| 66 | our_models = set([osmalchemy.node, osmalchemy.way, osmalchemy.relation, |
||
| 67 | osmalchemy.element]) |
||
| 68 | if affected_models.isdisjoint(our_models): |
||
| 69 | # None of our models is affected |
||
| 70 | return |
||
| 71 | |||
| 72 | # Check whether this query filters elements |
||
| 73 | # Online update will only run on a specified set, not all data |
||
| 74 | if query.whereclause is None: |
||
| 75 | # No filters |
||
| 76 | return |
||
| 77 | |||
| 78 | # Analyse where clause looking for all looked-up fields |
||
| 79 | trees = {} |
||
| 80 | for target in our_models.intersection(affected_models): |
||
| 81 | # Build expression trees first |
||
| 82 | trees[target.__name__] = _where_to_tree(query.whereclause, target) |
||
| 83 | |||
| 84 | # Compile to OverpassQL |
||
| 85 | oql = _trees_to_overpassql(trees) |
||
| 86 | |||
| 87 | # Look up query in cache |
||
| 88 | hashed_oql = hash(_normalise_overpassql(oql)) |
||
| 89 | cached_query = session.query(osmcachedquery).filter_by(oql_hash=hashed_oql).scalar() |
||
| 90 | # Check age if cached query was found |
||
| 91 | if cached_query: |
||
| 92 | timediff = datetime.datetime.now() - cached_query.oql_queried |
||
| 93 | if timediff.seconds < maxage: |
||
| 94 | # Return and do nothing if query was run recently |
||
| 95 | return |
||
| 96 | |||
| 97 | # Run query online |
||
| 98 | xml = _get_elements_by_query(osmalchemy._overpass, oql) |
||
| 99 | |||
| 100 | # Import data |
||
| 101 | session._osmalchemy_in_trigger = True |
||
| 102 | _import_osm_xml(osmalchemy, session, xml) |
||
| 103 | del session._osmalchemy_in_trigger |
||
| 104 | |||
| 105 | # Store or update query time |
||
| 106 | if not cached_query: |
||
| 107 | cached_query = osmcachedquery() |
||
| 108 | cached_query.oql_hash = hashed_oql |
||
| 109 | cached_query.oql_queried = datetime.datetime.now() |
||
| 110 | session.add(cached_query) |
||
| 111 | session.commit() |
||
| 112 |