Same as the builtin _safe_repr, with added support for Estimator objects.
(object, context, maxlevels, level, changed_only=False)
| 352 | |
| 353 | |
| 354 | def _safe_repr(object, context, maxlevels, level, changed_only=False): |
| 355 | """Same as the builtin _safe_repr, with added support for Estimator |
| 356 | objects.""" |
| 357 | typ = type(object) |
| 358 | |
| 359 | if typ in pprint._builtin_scalars: |
| 360 | return repr(object), True, False |
| 361 | |
| 362 | r = getattr(typ, "__repr__", None) |
| 363 | if issubclass(typ, dict) and r is dict.__repr__: |
| 364 | if not object: |
| 365 | return "{}", True, False |
| 366 | objid = id(object) |
| 367 | if maxlevels and level >= maxlevels: |
| 368 | return "{...}", False, objid in context |
| 369 | if objid in context: |
| 370 | return pprint._recursion(object), False, True |
| 371 | context[objid] = 1 |
| 372 | readable = True |
| 373 | recursive = False |
| 374 | components = [] |
| 375 | append = components.append |
| 376 | level += 1 |
| 377 | saferepr = _safe_repr |
| 378 | items = sorted(object.items(), key=pprint._safe_tuple) |
| 379 | for k, v in items: |
| 380 | krepr, kreadable, krecur = saferepr( |
| 381 | k, context, maxlevels, level, changed_only=changed_only |
| 382 | ) |
| 383 | vrepr, vreadable, vrecur = saferepr( |
| 384 | v, context, maxlevels, level, changed_only=changed_only |
| 385 | ) |
| 386 | append("%s: %s" % (krepr, vrepr)) |
| 387 | readable = readable and kreadable and vreadable |
| 388 | if krecur or vrecur: |
| 389 | recursive = True |
| 390 | del context[objid] |
| 391 | return "{%s}" % ", ".join(components), readable, recursive |
| 392 | |
| 393 | if (issubclass(typ, list) and r is list.__repr__) or ( |
| 394 | issubclass(typ, tuple) and r is tuple.__repr__ |
| 395 | ): |
| 396 | if issubclass(typ, list): |
| 397 | if not object: |
| 398 | return "[]", True, False |
| 399 | format = "[%s]" |
| 400 | elif len(object) == 1: |
| 401 | format = "(%s,)" |
| 402 | else: |
| 403 | if not object: |
| 404 | return "()", True, False |
| 405 | format = "(%s)" |
| 406 | objid = id(object) |
| 407 | if maxlevels and level >= maxlevels: |
| 408 | return format % "...", False, objid in context |
| 409 | if objid in context: |
| 410 | return pprint._recursion(object), False, True |
| 411 | context[objid] = 1 |
no test coverage detected
searching dependent graphs…