| Conditions | 54 |
| Total Lines | 109 |
| Lines | 24 |
| Ratio | 22.02 % |
| Changes | 8 | ||
| 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 _tree_to_overpassql_recursive() 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 ~*~ |
||
| 156 | def _tree_to_overpassql_recursive(tree, type_, op): |
||
| 157 | result = "" |
||
| 158 | |||
| 159 | # Test if we got a tree or an atom |
||
| 160 | if isinstance(tree[1], list): |
||
| 161 | # Tree |
||
| 162 | |||
| 163 | # Store operation of subtree |
||
| 164 | op = tree[0] |
||
| 165 | |||
| 166 | # Empty bounding box |
||
| 167 | bbox = [None, None, None, None] |
||
| 168 | |||
| 169 | # List of genrated set names |
||
| 170 | set_names = [] |
||
| 171 | |||
| 172 | for t in tree[1]: |
||
| 173 | # Check if element is a tree or an atom |
||
| 174 | if isinstance(t[1], list): |
||
| 175 | # Recurse into inner tree |
||
| 176 | result_inner_tree = _tree_to_overpassql_recursive(tree[1], op) |
||
| 177 | result += "%s" % result_inner_tree[1] |
||
| 178 | set_names.append(result_inner_tree[0]) |
||
| 179 | else: |
||
| 180 | # Parse atom |
||
| 181 | if t[1] == "latitude" and t[0] == ">": |
||
| 182 | View Code Duplication | if bbox[0] is None: |
|
| 183 | bbox[0] = float(t[2]) |
||
| 184 | elif op == "&&" and bbox[0] <= t[2]: |
||
| 185 | bbox[0] = float(t[2]) |
||
| 186 | elif op == "||" and bbox[0] >= t[2]: |
||
| 187 | bbox[0] = float(t[2]) |
||
| 188 | elif t[1] == "latitude" and t[0] == "<": |
||
| 189 | View Code Duplication | if bbox[2] is None: |
|
| 190 | bbox[2] = float(t[2]) |
||
| 191 | elif op == "&&" and bbox[2] >= t[2]: |
||
| 192 | bbox[2] = float(t[2]) |
||
| 193 | elif op == "||" and bbox[2] <= t[2]: |
||
| 194 | bbox[2] = float(t[2]) |
||
| 195 | elif t[1] == "longitude" and t[0] == ">": |
||
| 196 | View Code Duplication | if bbox[1] is None: |
|
| 197 | bbox[1] = float(t[2]) |
||
| 198 | elif op == "&&" and bbox[1] <= t[2]: |
||
| 199 | bbox[1] = float(t[2]) |
||
| 200 | elif op == "||" and bbox[1] >= t[2]: |
||
| 201 | bbox[1] = float(t[2]) |
||
| 202 | elif t[1] == "longitude" and t[0] == "<": |
||
| 203 | View Code Duplication | if bbox[3] is None: |
|
| 204 | bbox[3] = float(t[2]) |
||
| 205 | elif op == "&&" and bbox[3] >= t[2]: |
||
| 206 | bbox[3] = float(t[2]) |
||
| 207 | elif op == "||" and bbox[3] <= t[2]: |
||
| 208 | bbox[3] = float(t[2]) |
||
| 209 | elif t[1] == "id" and t[0] == "==": |
||
| 210 | idquery = "%s(%i)" % (type_, t[2]) |
||
| 211 | set_name = "s%i" % id(idquery) |
||
| 212 | result += "%s->.%s;" % (idquery, set_name) |
||
| 213 | set_names.append(set_name) |
||
| 214 | elif t[1] == "id": |
||
| 215 | # We got an id query, but not with equality comparison |
||
| 216 | raise ValueError("id can only be queried with equality") |
||
| 217 | else: |
||
| 218 | tagquery = "%s[\"%s\"=\"%s\"]" % (type_, t[1], t[2]) |
||
| 219 | set_name = "s%i" % id(tagquery) |
||
| 220 | result += "%s->.%s;" % (tagquery, set_name) |
||
| 221 | set_names.append(set_name) |
||
| 222 | |||
| 223 | if bbox != [None, None, None, None]: |
||
| 224 | if bbox[0] is None: |
||
| 225 | bbox[0] = -90.0 |
||
| 226 | if bbox[1] is None: |
||
| 227 | bbox[1] = -180.0 |
||
| 228 | if bbox[2] is None: |
||
| 229 | bbox[2] = 90.0 |
||
| 230 | if bbox[3] is None: |
||
| 231 | bbox[3] = 180.0 |
||
| 232 | bboxquery = "%s(%s,%s,%s,%s)" % (type_, bbox[0], bbox[1], bbox[2], bbox[3]) |
||
| 233 | set_name = "s%i" % id(bboxquery) |
||
| 234 | result += "%s->.%s;" % (bboxquery, set_name) |
||
| 235 | set_names.append(set_name) |
||
| 236 | |||
| 237 | if op == "&&": |
||
| 238 | result += "%s." % type_ |
||
| 239 | result += ".".join(set_names) |
||
| 240 | elif op == "||": |
||
| 241 | result += "(" |
||
| 242 | for s in set_names: |
||
| 243 | result += ".%s;" % s |
||
| 244 | result += ")" |
||
| 245 | else: |
||
| 246 | if tree[1] == "latitude" and tree[0] == ">": |
||
| 247 | result = "%s(%s,-180.0,90.0,180.0)" % (type_, tree[2]) |
||
| 248 | elif tree[1] == "latitude" and tree[0] == "<": |
||
| 249 | result = "%s(-90.0,-180.0,%s,180.0)" % (type_, tree[2]) |
||
| 250 | elif tree[1] == "longitude" and tree[0] == ">": |
||
| 251 | result = "%s(-90.0,%s,-90.0,180.0)" % (type_, tree[2]) |
||
| 252 | elif tree[1] == "longitude" and tree[0] == "<": |
||
| 253 | result = "%s(-90.0,-180.0,-90.0,%s)" % (type_, tree[2]) |
||
| 254 | elif tree[1] == "id" and tree[0] == "==": |
||
| 255 | result = "%s(%i)" % (type_, tree[2]) |
||
| 256 | elif tree[1] == "id": |
||
| 257 | # We got an id query, but not with equality comparison |
||
| 258 | raise ValueError("id can only be queried with equality") |
||
| 259 | else: |
||
| 260 | result = "%s[\"%s\"=\"%s\"]" % (type_, tree[1], tree[2]) |
||
| 261 | |||
| 262 | set_name = id(result) |
||
| 263 | result += "->.s%i;" % set_name |
||
| 264 | return (set_name, result) |
||
| 265 | |||
| 279 |