| 57 | |
| 58 | |
| 59 | class PrettyPrinter: |
| 60 | def __init__( |
| 61 | self, |
| 62 | indent: int = 4, |
| 63 | width: int = 80, |
| 64 | depth: int | None = None, |
| 65 | ) -> None: |
| 66 | class="st">"""Handle pretty printing operations onto a stream using a set of |
| 67 | configured parameters. |
| 68 | |
| 69 | indent |
| 70 | Number of spaces to indent for each level of nesting. |
| 71 | |
| 72 | width |
| 73 | Attempted maximum number of columns in the output. |
| 74 | |
| 75 | depth |
| 76 | The maximum depth to print out nested structures. |
| 77 | |
| 78 | class="st">""" |
| 79 | if indent < 0: |
| 80 | raise ValueError(class="st">"indent must be >= 0") |
| 81 | if depth is not None and depth <= 0: |
| 82 | raise ValueError(class="st">"depth must be > 0") |
| 83 | if not width: |
| 84 | raise ValueError(class="st">"width must be != 0") |
| 85 | self._depth = depth |
| 86 | self._indent_per_level = indent |
| 87 | self._width = width |
| 88 | |
| 89 | def pformat(self, object: Any) -> str: |
| 90 | sio = _StringIO() |
| 91 | self._format(object, sio, 0, 0, set(), 0) |
| 92 | return sio.getvalue() |
| 93 | |
| 94 | def _format( |
| 95 | self, |
| 96 | object: Any, |
| 97 | stream: IO[str], |
| 98 | indent: int, |
| 99 | allowance: int, |
| 100 | context: set[int], |
| 101 | level: int, |
| 102 | ) -> None: |
| 103 | objid = id(object) |
| 104 | if objid in context: |
| 105 | stream.write(_recursion(object)) |
| 106 | return |
| 107 | |
| 108 | p = self._dispatch.get(type(object).__repr__, None) |
| 109 | if p is not None: |
| 110 | context.add(objid) |
| 111 | p(self, object, stream, indent, allowance, context, level + 1) |
| 112 | context.remove(objid) |
| 113 | elif ( |
| 114 | _dataclasses.is_dataclass(object) |
| 115 | and not isinstance(object, type) |
| 116 | and object.__dataclass_params__.repr class="cm"># type:ignore[attr-defined] |
no outgoing calls