Skip to content

Commit 1ec3f5f

Browse files
committed
Removed pformat_tabs and changed target version.
1 parent 49b55d8 commit 1ec3f5f

File tree

5 files changed

+135
-51
lines changed

5 files changed

+135
-51
lines changed

domdf_python_tools/bases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class UserList(MutableSequence[_T]):
126126
methods supported by this class will need to be overridden; please consult the
127127
sources for information about the methods which need to be provided in that case.
128128
129-
.. versionadded:: 1.0.0
129+
.. versionadded:: 0.10.0
130130
"""
131131

132132
#: A real list object used to store the contents of the :class:`~domdf_python_tools.bases.UserList`.
@@ -350,7 +350,7 @@ class NamedList(UserList[_T]):
350350
351351
The name of the list is taken from the name of the subclass.
352352
353-
.. versionchanged:: 1.0.0
353+
.. versionchanged:: 0.10.0
354354
355355
:class:`~.NamedList` now subclasses :class:`.UserList` rather than :class:`collections.UserList`.
356356
"""

domdf_python_tools/delegators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Decorators for functions that delegate parts of their functionality
77
to other functions.
88
9-
.. versionadded:: 1.0.0
9+
.. versionadded:: 0.10.0
1010
"""
1111
#
1212
# Copyright © 2020 Dominic Davis-Foster <[email protected]>

domdf_python_tools/pretty_print.py

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
Functions and classes for pretty printing.
77
8-
.. versionadded:: 1.0.0
8+
.. versionadded:: 0.10.0
99
"""
1010
#
1111
# Copyright © 2020 Dominic Davis-Foster <[email protected]>
@@ -34,18 +34,18 @@
3434
#
3535

3636
# stdlib
37-
import re
3837
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
4140

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"]
4642

4743

4844
class FancyPrinter(PrettyPrinter):
45+
"""
46+
Subclass of :class:`~.pprint.PrettyPrinter` with different formatting.
47+
"""
48+
4949
# TODO: docs
5050
_dispatch: MutableMapping[Callable, Callable]
5151
_indent_per_level: int
@@ -118,32 +118,6 @@ def _pprint_set(self, object, stream, indent, allowance, context, level):
118118
_dispatch[frozenset.__repr__] = _pprint_set
119119

120120

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-
147121
class Attributes:
148122

149123
def __init__(self, obj: object, *attributes: str):
@@ -169,18 +143,11 @@ def pformat(self, object):
169143
stream = StringIO()
170144
context = {}
171145

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-
179146
p = self._dispatch.get(type(object).__repr__, None)
180147

181-
context[objid] = 1
148+
context[id(object)] = 1
182149
p(self, object, stream, 0, 0, context, 1)
183-
del context[objid]
150+
del context[id(object)]
184151
return stream.getvalue()
185152

186153
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
225192

226193
def simple_repr(*attributes: str, show_module: bool = False, **kwargs):
227194
r"""
228-
Adds a simple ``__repr__` method to the decorated class.
195+
Adds a simple ``__repr__`` method to the decorated class.
229196
230197
:param attributes: The attributes to include in the ``__repr__``.
231198
:param show_module: Whether to show the name of the module in the ``__repr__``.
232199
:param \*\*kwargs: Keyword arguments passed on to :class:`pprint.PrettyPrinter`.
233-
Has no effect if `multiline` is :py:obj:`False`.
234200
"""
235201

236202
def deco(obj):

tests/test_pretty_print.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
from pytest_regressions.file_regression import FileRegressionFixture
1919

2020
# this package
21-
from domdf_python_tools.pretty_print import FancyPrinter
22-
# list, tuple and dict subclasses that do or don't overwrite __repr__
21+
from domdf_python_tools.pretty_print import FancyPrinter, simple_repr
2322
from domdf_python_tools.stringlist import StringList
2423

2524

25+
# list, tuple and dict subclasses that do or don't overwrite __repr__
26+
2627
class list2(list):
2728
pass
2829

@@ -143,7 +144,6 @@ def test_init(self):
143144
FancyPrinter()
144145
FancyPrinter(indent=4, width=40, depth=5, stream=io.StringIO(), compact=True)
145146
FancyPrinter(4, 40, 5, io.StringIO())
146-
FancyPrinter(sort_dicts=False)
147147
with pytest.raises(TypeError):
148148
FancyPrinter(4, 40, 5, io.StringIO(), True)
149149
with pytest.raises(ValueError):
@@ -927,3 +927,14 @@ def format(self, object, context, maxlevels, level):
927927
return object, 0, 0
928928
else:
929929
return FancyPrinter.format(self, object, context, maxlevels, level)
930+
931+
932+
def test_simple_repr(file_regression: FileRegressionFixture):
933+
@simple_repr("a", "b", "c", "d", width=10)
934+
class F:
935+
a = "apple"
936+
b = "banana"
937+
c = "cherry"
938+
d = list(range(100))
939+
940+
file_regression.check(repr(F()), encoding="UTF-8", extension=".txt")
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
F(
2+
a='apple',
3+
b='banana',
4+
c='cherry',
5+
d=[
6+
0,
7+
1,
8+
2,
9+
3,
10+
4,
11+
5,
12+
6,
13+
7,
14+
8,
15+
9,
16+
10,
17+
11,
18+
12,
19+
13,
20+
14,
21+
15,
22+
16,
23+
17,
24+
18,
25+
19,
26+
20,
27+
21,
28+
22,
29+
23,
30+
24,
31+
25,
32+
26,
33+
27,
34+
28,
35+
29,
36+
30,
37+
31,
38+
32,
39+
33,
40+
34,
41+
35,
42+
36,
43+
37,
44+
38,
45+
39,
46+
40,
47+
41,
48+
42,
49+
43,
50+
44,
51+
45,
52+
46,
53+
47,
54+
48,
55+
49,
56+
50,
57+
51,
58+
52,
59+
53,
60+
54,
61+
55,
62+
56,
63+
57,
64+
58,
65+
59,
66+
60,
67+
61,
68+
62,
69+
63,
70+
64,
71+
65,
72+
66,
73+
67,
74+
68,
75+
69,
76+
70,
77+
71,
78+
72,
79+
73,
80+
74,
81+
75,
82+
76,
83+
77,
84+
78,
85+
79,
86+
80,
87+
81,
88+
82,
89+
83,
90+
84,
91+
85,
92+
86,
93+
87,
94+
88,
95+
89,
96+
90,
97+
91,
98+
92,
99+
93,
100+
94,
101+
95,
102+
96,
103+
97,
104+
98,
105+
99,
106+
]
107+
)

0 commit comments

Comments
 (0)