| 112 | |
| 113 | |
| 114 | class PrettyPrinter: |
| 115 | def __init__(self, indent=1, width=80, depth=None, stream=None, *, |
| 116 | compact=False, expand=False, sort_dicts=True, |
| 117 | underscore_numbers=False): |
| 118 | """Handle pretty printing operations onto a stream using a set of |
| 119 | configured parameters. |
| 120 | |
| 121 | indent |
| 122 | Number of spaces to indent for each level of nesting. |
| 123 | |
| 124 | width |
| 125 | Attempted maximum number of columns in the output. |
| 126 | |
| 127 | depth |
| 128 | The maximum depth to print out nested structures. |
| 129 | |
| 130 | stream |
| 131 | The desired output stream. If omitted (or false), the standard |
| 132 | output stream available at construction will be used. |
| 133 | |
| 134 | compact |
| 135 | If true, several items will be combined in one line. |
| 136 | Incompatible with expand mode. |
| 137 | |
| 138 | expand |
| 139 | If true, the output will be formatted similar to |
| 140 | pretty-printed json.dumps() when ``indent`` is supplied. |
| 141 | Incompatible with compact mode. |
| 142 | |
| 143 | sort_dicts |
| 144 | If true, dict keys are sorted. |
| 145 | |
| 146 | underscore_numbers |
| 147 | If true, digit groups are separated with underscores. |
| 148 | |
| 149 | """ |
| 150 | indent = int(indent) |
| 151 | width = int(width) |
| 152 | if indent < 0: |
| 153 | raise ValueError('indent must be >= 0') |
| 154 | if depth is not None and depth <= 0: |
| 155 | raise ValueError('depth must be > 0') |
| 156 | if not width: |
| 157 | raise ValueError('width must be != 0') |
| 158 | if compact and expand: |
| 159 | raise ValueError('compact and expand are incompatible') |
| 160 | self._depth = depth |
| 161 | self._indent_per_level = indent |
| 162 | self._width = width |
| 163 | if stream is not None: |
| 164 | self._stream = stream |
| 165 | else: |
| 166 | self._stream = _sys.stdout |
| 167 | self._compact = bool(compact) |
| 168 | self._expand = bool(expand) |
| 169 | self._sort_dicts = sort_dicts |
| 170 | self._underscore_numbers = underscore_numbers |
| 171 |
no test coverage detected
searching dependent graphs…