5
5
"""
6
6
Functions and classes for pretty printing.
7
7
8
- .. versionadded:: 1.0 .0
8
+ .. versionadded:: 0.10 .0
9
9
"""
10
10
#
11
11
# Copyright © 2020 Dominic Davis-Foster <[email protected] >
34
34
#
35
35
36
36
# stdlib
37
- import re
38
37
from io import StringIO
39
- from pprint import PrettyPrinter , _recursion , _safe_key , _safe_tuple # type: ignore
40
- from typing import Any , Callable , Iterator , MutableMapping , Optional , Tuple
38
+ from pprint import PrettyPrinter , _safe_key # type: ignore
39
+ from typing import Any , Callable , Iterator , MutableMapping , Tuple
41
40
42
- # this package
43
- from domdf_python_tools .stringlist import StringList
44
-
45
- __all__ = ["FancyPrinter" , "pformat_tabs" , "Attributes" , "ReprPrettyPrinter" , "simple_repr" ]
41
+ __all__ = ["FancyPrinter" , "simple_repr" ]
46
42
47
43
48
44
class FancyPrinter (PrettyPrinter ):
45
+ """
46
+ Subclass of :class:`~.pprint.PrettyPrinter` with different formatting.
47
+ """
48
+
49
49
# TODO: docs
50
50
_dispatch : MutableMapping [Callable , Callable ]
51
51
_indent_per_level : int
@@ -118,32 +118,6 @@ def _pprint_set(self, object, stream, indent, allowance, context, level):
118
118
_dispatch [frozenset .__repr__ ] = _pprint_set
119
119
120
120
121
- def pformat_tabs (
122
- object : object ,
123
- width : int = 80 ,
124
- depth : Optional [int ] = None ,
125
- * ,
126
- compact : bool = False ,
127
- ) -> str :
128
- """
129
- Format a Python object into a pretty-printed representation.
130
- Indentation is set at one tab.
131
-
132
- :param object:
133
- :param width:
134
- :param depth:
135
- :param compact:
136
- """
137
-
138
- prettyprinter = FancyPrinter (indent = 4 , width = width , depth = depth , compact = compact )
139
-
140
- buf = StringList ()
141
- for line in prettyprinter .pformat (object ).splitlines ():
142
- buf .append (re .sub ("^ {4}" , r"\t" , line ))
143
-
144
- return str (buf )
145
-
146
-
147
121
class Attributes :
148
122
149
123
def __init__ (self , obj : object , * attributes : str ):
@@ -169,18 +143,11 @@ def pformat(self, object):
169
143
stream = StringIO ()
170
144
context = {}
171
145
172
- objid = id (object )
173
- if objid in context :
174
- stream .write (_recursion (object ))
175
- self ._recursive = True
176
- self ._readable = False
177
- return
178
-
179
146
p = self ._dispatch .get (type (object ).__repr__ , None )
180
147
181
- context [objid ] = 1
148
+ context [id ( object ) ] = 1
182
149
p (self , object , stream , 0 , 0 , context , 1 )
183
- del context [objid ]
150
+ del context [id ( object ) ]
184
151
return stream .getvalue ()
185
152
186
153
def _pprint_attributes (self , object , stream , indent , allowance , context , level ):
@@ -225,12 +192,11 @@ def _format_attribute_items(self, items, stream, indent, allowance, context, lev
225
192
226
193
def simple_repr (* attributes : str , show_module : bool = False , ** kwargs ):
227
194
r"""
228
- Adds a simple ``__repr__` method to the decorated class.
195
+ Adds a simple ``__repr__`` method to the decorated class.
229
196
230
197
:param attributes: The attributes to include in the ``__repr__``.
231
198
:param show_module: Whether to show the name of the module in the ``__repr__``.
232
199
:param \*\*kwargs: Keyword arguments passed on to :class:`pprint.PrettyPrinter`.
233
- Has no effect if `multiline` is :py:obj:`False`.
234
200
"""
235
201
236
202
def deco (obj ):
0 commit comments