| Total Complexity | 45 |
| Total Lines | 164 |
| Duplicated Lines | 0 % |
Complex classes like pyspider.libs.PrettyPrinter 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. |
||
| 85 | class PrettyPrinter: |
||
| 86 | |||
| 87 | def __init__(self, indent=1, width=80, depth=None, stream=None): |
||
| 88 | """Handle pretty printing operations onto a stream using a set of |
||
| 89 | configured parameters. |
||
| 90 | |||
| 91 | indent |
||
| 92 | Number of spaces to indent for each level of nesting. |
||
| 93 | |||
| 94 | width |
||
| 95 | Attempted maximum number of columns in the output. |
||
| 96 | |||
| 97 | depth |
||
| 98 | The maximum depth to print out nested structures. |
||
| 99 | |||
| 100 | stream |
||
| 101 | The desired output stream. If omitted (or false), the standard |
||
| 102 | output stream available at construction will be used. |
||
| 103 | |||
| 104 | """ |
||
| 105 | indent = int(indent) |
||
| 106 | width = int(width) |
||
| 107 | assert indent >= 0, "indent must be >= 0" |
||
| 108 | assert depth is None or depth > 0, "depth must be > 0" |
||
| 109 | assert width, "width must be != 0" |
||
| 110 | self._depth = depth |
||
| 111 | self._indent_per_level = indent |
||
| 112 | self._width = width |
||
| 113 | if stream is not None: |
||
| 114 | self._stream = stream |
||
| 115 | else: |
||
| 116 | self._stream = _sys.stdout |
||
| 117 | |||
| 118 | def pprint(self, object): |
||
| 119 | self._format(object, self._stream, 0, 0, {}, 0) |
||
| 120 | self._stream.write("\n") |
||
| 121 | |||
| 122 | def pformat(self, object): |
||
| 123 | sio = BytesIO() |
||
| 124 | self._format(object, sio, 0, 0, {}, 0) |
||
| 125 | return sio.getvalue() |
||
| 126 | |||
| 127 | def isrecursive(self, object): |
||
| 128 | return self.format(object, {}, 0, 0)[2] |
||
| 129 | |||
| 130 | def isreadable(self, object): |
||
| 131 | s, readable, recursive = self.format(object, {}, 0, 0) |
||
| 132 | return readable and not recursive |
||
| 133 | |||
| 134 | def _format(self, object, stream, indent, allowance, context, level): |
||
| 135 | level = level + 1 |
||
| 136 | objid = _id(object) |
||
| 137 | if objid in context: |
||
| 138 | stream.write(_recursion(object)) |
||
| 139 | self._recursive = True |
||
| 140 | self._readable = False |
||
| 141 | return |
||
| 142 | rep = self._repr(object, context, level - 1) |
||
| 143 | typ = _type(object) |
||
| 144 | sepLines = _len(rep) > (self._width - 1 - indent - allowance) |
||
| 145 | write = stream.write |
||
| 146 | |||
| 147 | if self._depth and level > self._depth: |
||
| 148 | write(rep) |
||
| 149 | return |
||
| 150 | |||
| 151 | r = getattr(typ, "__repr__", None) |
||
| 152 | if issubclass(typ, dict) and r is dict.__repr__: |
||
| 153 | write('{') |
||
| 154 | if self._indent_per_level > 1: |
||
| 155 | write((self._indent_per_level - 1) * ' ') |
||
| 156 | length = _len(object) |
||
| 157 | if length: |
||
| 158 | context[objid] = 1 |
||
| 159 | indent = indent + self._indent_per_level |
||
| 160 | items = _sorted(object.items()) |
||
| 161 | key, ent = items[0] |
||
| 162 | rep = self._repr(key, context, level) |
||
| 163 | write(rep) |
||
| 164 | write(': ') |
||
| 165 | self._format(ent, stream, indent + _len(rep) + 2, |
||
| 166 | allowance + 1, context, level) |
||
| 167 | if length > 1: |
||
| 168 | for key, ent in items[1:]: |
||
| 169 | rep = self._repr(key, context, level) |
||
| 170 | if sepLines: |
||
| 171 | write(',\n%s%s: ' % (' ' * indent, rep)) |
||
| 172 | else: |
||
| 173 | write(', %s: ' % rep) |
||
| 174 | self._format(ent, stream, indent + _len(rep) + 2, |
||
| 175 | allowance + 1, context, level) |
||
| 176 | indent = indent - self._indent_per_level |
||
| 177 | del context[objid] |
||
| 178 | write('}') |
||
| 179 | return |
||
| 180 | |||
| 181 | if ( |
||
| 182 | (issubclass(typ, list) and r is list.__repr__) or |
||
| 183 | (issubclass(typ, tuple) and r is tuple.__repr__) or |
||
| 184 | (issubclass(typ, set) and r is set.__repr__) or |
||
| 185 | (issubclass(typ, frozenset) and r is frozenset.__repr__) |
||
| 186 | ): |
||
| 187 | length = _len(object) |
||
| 188 | if issubclass(typ, list): |
||
| 189 | write('[') |
||
| 190 | endchar = ']' |
||
| 191 | elif issubclass(typ, set): |
||
| 192 | if not length: |
||
| 193 | write('set()') |
||
| 194 | return |
||
| 195 | write('set([') |
||
| 196 | endchar = '])' |
||
| 197 | object = _sorted(object) |
||
| 198 | indent += 4 |
||
| 199 | elif issubclass(typ, frozenset): |
||
| 200 | if not length: |
||
| 201 | write('frozenset()') |
||
| 202 | return |
||
| 203 | write('frozenset([') |
||
| 204 | endchar = '])' |
||
| 205 | object = _sorted(object) |
||
| 206 | indent += 10 |
||
| 207 | else: |
||
| 208 | write('(') |
||
| 209 | endchar = ')' |
||
| 210 | if self._indent_per_level > 1 and sepLines: |
||
| 211 | write((self._indent_per_level - 1) * ' ') |
||
| 212 | if length: |
||
| 213 | context[objid] = 1 |
||
| 214 | indent = indent + self._indent_per_level |
||
| 215 | self._format(object[0], stream, indent, allowance + 1, |
||
| 216 | context, level) |
||
| 217 | if length > 1: |
||
| 218 | for ent in object[1:]: |
||
| 219 | if sepLines: |
||
| 220 | write(',\n' + ' ' * indent) |
||
| 221 | else: |
||
| 222 | write(', ') |
||
| 223 | self._format(ent, stream, indent, |
||
| 224 | allowance + 1, context, level) |
||
| 225 | indent = indent - self._indent_per_level |
||
| 226 | del context[objid] |
||
| 227 | if issubclass(typ, tuple) and length == 1: |
||
| 228 | write(',') |
||
| 229 | write(endchar) |
||
| 230 | return |
||
| 231 | |||
| 232 | write(rep) |
||
| 233 | |||
| 234 | def _repr(self, object, context, level): |
||
| 235 | repr, readable, recursive = self.format(object, context.copy(), |
||
| 236 | self._depth, level) |
||
| 237 | if not readable: |
||
| 238 | self._readable = False |
||
| 239 | if recursive: |
||
| 240 | self._recursive = True |
||
| 241 | return repr |
||
| 242 | |||
| 243 | def format(self, object, context, maxlevels, level): |
||
| 244 | """Format object for a specific context, returning a string |
||
| 245 | and flags indicating whether the representation is 'readable' |
||
| 246 | and whether the object represents a recursive construct. |
||
| 247 | """ |
||
| 248 | return _safe_repr(object, context, maxlevels, level) |
||
| 249 | |||
| 379 |