reprlib.Repr attributes can be overriden in __init__()

This commit is contained in:
Rémi Lapeyre 2020-06-17 01:08:39 +02:00
parent c4862e333a
commit c3ca57d954
4 changed files with 75 additions and 58 deletions

View File

@ -17,7 +17,9 @@ debugger and may be useful in other contexts as well.
This module provides a class, an instance, and a function:
.. class:: Repr()
.. class:: Repr(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, \
maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, \
maxlong=40, maxother=30)
Class which provides formatting services useful in implementing functions
similar to the built-in :func:`repr`; size limits for different object types
@ -71,72 +73,75 @@ string instead.
Repr Objects
------------
:class:`Repr` instances provide several attributes which can be used to provide
size limits for the representations of different object types, and methods
which format specific object types.
.. class:: Repr(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, \
maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, \
maxlong=40, maxother=30)
:class:`Repr` instances provide several attributes which can be used to provide
size limits for the representations of different object types, and methods
which format specific object types.
.. attribute:: maxlevel
Depth limit on the creation of recursive representations. The default is ``6``.
.. attribute:: Repr.maxlevel
.. attribute:: maxdict
maxlist
maxtuple
maxset
maxfrozenset
maxdeque
maxarray
Depth limit on the creation of recursive representations. The default is ``6``.
Limits on the number of entries represented for the named object type. The
default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
the others.
.. attribute:: Repr.maxdict
Repr.maxlist
Repr.maxtuple
Repr.maxset
Repr.maxfrozenset
Repr.maxdeque
Repr.maxarray
.. attribute:: maxlong
Limits on the number of entries represented for the named object type. The
default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for
the others.
Maximum number of characters in the representation for an integer. Digits
are dropped from the middle. The default is ``40``.
.. attribute:: Repr.maxlong
.. attribute:: maxstring
Maximum number of characters in the representation for an integer. Digits
are dropped from the middle. The default is ``40``.
Limit on the number of characters in the representation of the string. Note
that the "normal" representation of the string is used as the character source:
if escape sequences are needed in the representation, these may be mangled when
the representation is shortened. The default is ``30``.
.. attribute:: Repr.maxstring
.. attribute:: maxother
Limit on the number of characters in the representation of the string. Note
that the "normal" representation of the string is used as the character source:
if escape sequences are needed in the representation, these may be mangled when
the representation is shortened. The default is ``30``.
This limit is used to control the size of object types for which no specific
formatting method is available on the :class:`Repr` object. It is applied in a
similar manner as :attr:`maxstring`. The default is ``20``.
.. attribute:: Repr.maxother
.. method:: repr(obj)
This limit is used to control the size of object types for which no specific
formatting method is available on the :class:`Repr` object. It is applied in a
similar manner as :attr:`maxstring`. The default is ``20``.
The equivalent to the built-in :func:`repr` that uses the formatting imposed by
the instance.
.. method:: Repr.repr(obj)
.. method:: repr1(obj, level)
The equivalent to the built-in :func:`repr` that uses the formatting imposed by
the instance.
Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
determine which formatting method to call, passing it *obj* and *level*. The
type-specific methods should call :meth:`repr1` to perform recursive formatting,
with ``level - 1`` for the value of *level* in the recursive call.
.. method:: Repr.repr1(obj, level)
.. method:: repr_TYPE(obj, level)
:noindex:
Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
determine which formatting method to call, passing it *obj* and *level*. The
type-specific methods should call :meth:`repr1` to perform recursive formatting,
with ``level - 1`` for the value of *level* in the recursive call.
.. method:: Repr.repr_TYPE(obj, level)
:noindex:
Formatting methods for specific types are implemented as methods with a name
based on the type name. In the method name, **TYPE** is replaced by
``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is
handled by :meth:`repr1`. Type-specific methods which need to recursively
format a value should call ``self.repr1(subobj, level - 1)``.
Formatting methods for specific types are implemented as methods with a name
based on the type name. In the method name, **TYPE** is replaced by
``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is
handled by :meth:`repr1`. Type-specific methods which need to recursively
format a value should call ``self.repr1(subobj, level - 1)``.
.. _subclassing-reprs:

View File

@ -33,20 +33,23 @@ def recursive_repr(fillvalue='...'):
return decorating_function
class Repr:
def __init__(self):
self.maxlevel = 6
self.maxtuple = 6
self.maxlist = 6
self.maxarray = 5
self.maxdict = 4
self.maxset = 6
self.maxfrozenset = 6
self.maxdeque = 6
self.maxstring = 30
self.maxlong = 40
self.maxother = 30
def __init__(self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5,
maxdict=4, maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30,
maxlong=40, maxother=30):
self.maxlevel = maxlevel
self.maxtuple = maxtuple
self.maxlist = maxlist
self.maxarray = maxarray
self.maxdict = maxdict
self.maxset = maxset
self.maxfrozenset = maxfrozenset
self.maxdeque = maxdeque
self.maxstring = maxstring
self.maxlong = maxlong
self.maxother = maxother
def repr(self, x):
return self.repr1(x, self.maxlevel)

View File

@ -216,6 +216,13 @@ class ReprTests(unittest.TestCase):
r(y)
r(z)
def test___init__(self):
a = "a"*40
r = Repr()
self.assertEqual(r.repr(a), "'aaaaaaaaaaaa...aaaaaaaaaaaaa'")
r = Repr(maxstring=10)
self.assertEqual(r.repr(a), "'aa...aaa'")
def write_file(path, text):
with open(path, 'w', encoding='ASCII') as fp:
fp.write(text)

View File

@ -0,0 +1,2 @@
:class:`reprlib.Repr` attributes can now be set when creating an instance.
Patch contributed by Rémi Lapeyre.