| Conditions | 34 |
| Total Lines | 104 |
| Lines | 0 |
| Ratio | 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 pyspider.libs._safe_repr() 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 | # Author: Fred L. Drake, Jr. |
||
| 253 | def _safe_repr(object, context, maxlevels, level): |
||
| 254 | typ = _type(object) |
||
| 255 | if typ is str: |
||
| 256 | string = object |
||
| 257 | string = string.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t') |
||
| 258 | if 'locale' not in _sys.modules: |
||
| 259 | return repr(object), True, False |
||
| 260 | if "'" in object and '"' not in object: |
||
| 261 | closure = '"' |
||
| 262 | quotes = {'"': '\\"'} |
||
| 263 | string = string.replace('"', '\\"') |
||
| 264 | else: |
||
| 265 | closure = "'" |
||
| 266 | quotes = {"'": "\\'"} |
||
| 267 | string = string.replace("'", "\\'") |
||
| 268 | try: |
||
| 269 | string.decode('utf8').encode('gbk', 'replace') |
||
| 270 | return ("%s%s%s" % (closure, string, closure)), True, False |
||
| 271 | except: |
||
| 272 | pass |
||
| 273 | qget = quotes.get |
||
| 274 | sio = StringIO() |
||
| 275 | write = sio.write |
||
| 276 | for char in object: |
||
| 277 | if char.isalpha(): |
||
| 278 | write(char) |
||
| 279 | else: |
||
| 280 | write(qget(char, repr(char)[1:-1])) |
||
| 281 | return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False |
||
| 282 | |||
| 283 | if typ is six.text_type: |
||
| 284 | string = object.encode("utf8", 'replace') |
||
| 285 | string = string.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t') |
||
| 286 | if "'" in object and '"' not in object: |
||
| 287 | closure = '"' |
||
| 288 | quotes = {'"': '\\"'} |
||
| 289 | string = string.replace('"', '\\"') |
||
| 290 | else: |
||
| 291 | closure = "'" |
||
| 292 | quotes = {"'": "\\'"} |
||
| 293 | string = string.replace("'", "\\'") |
||
| 294 | return ("u%s%s%s" % (closure, string, closure)), True, False |
||
| 295 | |||
| 296 | r = getattr(typ, "__repr__", None) |
||
| 297 | if issubclass(typ, dict) and r is dict.__repr__: |
||
| 298 | if not object: |
||
| 299 | return "{}", True, False |
||
| 300 | objid = _id(object) |
||
| 301 | if maxlevels and level >= maxlevels: |
||
| 302 | return "{...}", False, objid in context |
||
| 303 | if objid in context: |
||
| 304 | return _recursion(object), False, True |
||
| 305 | context[objid] = 1 |
||
| 306 | readable = True |
||
| 307 | recursive = False |
||
| 308 | components = [] |
||
| 309 | append = components.append |
||
| 310 | level += 1 |
||
| 311 | saferepr = _safe_repr |
||
| 312 | for k, v in _sorted(object.items()): |
||
| 313 | krepr, kreadable, krecur = saferepr(k, context, maxlevels, level) |
||
| 314 | vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level) |
||
| 315 | append("%s: %s" % (krepr, vrepr)) |
||
| 316 | readable = readable and kreadable and vreadable |
||
| 317 | if krecur or vrecur: |
||
| 318 | recursive = True |
||
| 319 | del context[objid] |
||
| 320 | return "{%s}" % _commajoin(components), readable, recursive |
||
| 321 | |||
| 322 | if (issubclass(typ, list) and r is list.__repr__) or \ |
||
| 323 | (issubclass(typ, tuple) and r is tuple.__repr__): |
||
| 324 | if issubclass(typ, list): |
||
| 325 | if not object: |
||
| 326 | return "[]", True, False |
||
| 327 | format = "[%s]" |
||
| 328 | elif _len(object) == 1: |
||
| 329 | format = "(%s,)" |
||
| 330 | else: |
||
| 331 | if not object: |
||
| 332 | return "()", True, False |
||
| 333 | format = "(%s)" |
||
| 334 | objid = _id(object) |
||
| 335 | if maxlevels and level >= maxlevels: |
||
| 336 | return format % "...", False, objid in context |
||
| 337 | if objid in context: |
||
| 338 | return _recursion(object), False, True |
||
| 339 | context[objid] = 1 |
||
| 340 | readable = True |
||
| 341 | recursive = False |
||
| 342 | components = [] |
||
| 343 | append = components.append |
||
| 344 | level += 1 |
||
| 345 | for o in object: |
||
| 346 | orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level) |
||
| 347 | append(orepr) |
||
| 348 | if not oreadable: |
||
| 349 | readable = False |
||
| 350 | if orecur: |
||
| 351 | recursive = True |
||
| 352 | del context[objid] |
||
| 353 | return format % _commajoin(components), readable, recursive |
||
| 354 | |||
| 355 | rep = repr(object) |
||
| 356 | return rep, (rep and not rep.startswith('<')), False |
||
| 357 | |||
| 379 |