Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 1 | :mod:`reprlib` --- Alternate :func:`repr` implementation |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 2 | ======================================================== |
| 3 | |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 4 | .. module:: reprlib |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 5 | :synopsis: Alternate repr() implementation with size limits. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 8 | |
Raymond Hettinger | 98b140c | 2011-01-23 21:05:46 +0000 | [diff] [blame] | 9 | **Source code:** :source:`Lib/reprlib.py` |
| 10 | |
| 11 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 13 | The :mod:`reprlib` module provides a means for producing object representations |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | with limits on the size of the resulting strings. This is used in the Python |
| 15 | debugger and may be useful in other contexts as well. |
| 16 | |
| 17 | This module provides a class, an instance, and a function: |
| 18 | |
| 19 | |
| 20 | .. class:: Repr() |
| 21 | |
| 22 | Class which provides formatting services useful in implementing functions |
| 23 | similar to the built-in :func:`repr`; size limits for different object types |
| 24 | are added to avoid the generation of representations which are excessively long. |
| 25 | |
| 26 | |
| 27 | .. data:: aRepr |
| 28 | |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 29 | This is an instance of :class:`Repr` which is used to provide the |
| 30 | :func:`.repr` function described below. Changing the attributes of this |
| 31 | object will affect the size limits used by :func:`.repr` and the Python |
| 32 | debugger. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
| 34 | |
| 35 | .. function:: repr(obj) |
| 36 | |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 37 | This is the :meth:`~Repr.repr` method of ``aRepr``. It returns a string |
| 38 | similar to that returned by the built-in function of the same name, but with |
| 39 | limits on most sizes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 41 | In addition to size-limiting tools, the module also provides a decorator for |
| 42 | detecting recursive calls to :meth:`__repr__` and substituting a placeholder |
| 43 | string instead. |
| 44 | |
| 45 | .. decorator:: recursive_repr(fillvalue="...") |
| 46 | |
| 47 | Decorator for :meth:`__repr__` methods to detect recursive calls within the |
| 48 | same thread. If a recursive call is made, the *fillvalue* is returned, |
| 49 | otherwise, the usual :meth:`__repr__` call is made. For example: |
| 50 | |
Marco Buttu | e65fcde | 2017-04-27 14:23:34 +0200 | [diff] [blame] | 51 | >>> from reprlib import recursive_repr |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 52 | >>> class MyList(list): |
| 53 | ... @recursive_repr() |
Raymond Hettinger | 400daed | 2014-08-02 22:32:10 -0700 | [diff] [blame] | 54 | ... def __repr__(self): |
| 55 | ... return '<' + '|'.join(map(repr, self)) + '>' |
Raymond Hettinger | 98a5f3f | 2010-09-13 21:36:00 +0000 | [diff] [blame] | 56 | ... |
| 57 | >>> m = MyList('abc') |
| 58 | >>> m.append(m) |
| 59 | >>> m.append('x') |
| 60 | >>> print(m) |
| 61 | <'a'|'b'|'c'|...|'x'> |
| 62 | |
| 63 | .. versionadded:: 3.2 |
| 64 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
| 66 | .. _repr-objects: |
| 67 | |
| 68 | Repr Objects |
| 69 | ------------ |
| 70 | |
Senthil Kumaran | a6bac95 | 2011-07-04 11:28:30 -0700 | [diff] [blame] | 71 | :class:`Repr` instances provide several attributes which can be used to provide |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | size limits for the representations of different object types, and methods |
| 73 | which format specific object types. |
| 74 | |
| 75 | |
| 76 | .. attribute:: Repr.maxlevel |
| 77 | |
| 78 | Depth limit on the creation of recursive representations. The default is ``6``. |
| 79 | |
| 80 | |
| 81 | .. attribute:: Repr.maxdict |
| 82 | Repr.maxlist |
| 83 | Repr.maxtuple |
| 84 | Repr.maxset |
| 85 | Repr.maxfrozenset |
| 86 | Repr.maxdeque |
| 87 | Repr.maxarray |
| 88 | |
| 89 | Limits on the number of entries represented for the named object type. The |
| 90 | default is ``4`` for :attr:`maxdict`, ``5`` for :attr:`maxarray`, and ``6`` for |
| 91 | the others. |
| 92 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 93 | |
| 94 | .. attribute:: Repr.maxlong |
| 95 | |
Mark Dickinson | bf5c6a9 | 2009-01-17 10:21:23 +0000 | [diff] [blame] | 96 | Maximum number of characters in the representation for an integer. Digits |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | are dropped from the middle. The default is ``40``. |
| 98 | |
| 99 | |
| 100 | .. attribute:: Repr.maxstring |
| 101 | |
| 102 | Limit on the number of characters in the representation of the string. Note |
| 103 | that the "normal" representation of the string is used as the character source: |
| 104 | if escape sequences are needed in the representation, these may be mangled when |
| 105 | the representation is shortened. The default is ``30``. |
| 106 | |
| 107 | |
| 108 | .. attribute:: Repr.maxother |
| 109 | |
| 110 | This limit is used to control the size of object types for which no specific |
| 111 | formatting method is available on the :class:`Repr` object. It is applied in a |
| 112 | similar manner as :attr:`maxstring`. The default is ``20``. |
| 113 | |
| 114 | |
| 115 | .. method:: Repr.repr(obj) |
| 116 | |
| 117 | The equivalent to the built-in :func:`repr` that uses the formatting imposed by |
| 118 | the instance. |
| 119 | |
| 120 | |
| 121 | .. method:: Repr.repr1(obj, level) |
| 122 | |
Georg Brandl | 502d9a5 | 2009-07-26 15:02:41 +0000 | [diff] [blame] | 123 | Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | determine which formatting method to call, passing it *obj* and *level*. The |
| 125 | type-specific methods should call :meth:`repr1` to perform recursive formatting, |
| 126 | with ``level - 1`` for the value of *level* in the recursive call. |
| 127 | |
| 128 | |
| 129 | .. method:: Repr.repr_TYPE(obj, level) |
| 130 | :noindex: |
| 131 | |
| 132 | Formatting methods for specific types are implemented as methods with a name |
| 133 | based on the type name. In the method name, **TYPE** is replaced by |
Berker Peksag | 2d510e3 | 2014-09-18 06:05:14 +0300 | [diff] [blame] | 134 | ``'_'.join(type(obj).__name__.split())``. Dispatch to these methods is |
| 135 | handled by :meth:`repr1`. Type-specific methods which need to recursively |
| 136 | format a value should call ``self.repr1(subobj, level - 1)``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | .. _subclassing-reprs: |
| 140 | |
| 141 | Subclassing Repr Objects |
| 142 | ------------------------ |
| 143 | |
| 144 | The use of dynamic dispatching by :meth:`Repr.repr1` allows subclasses of |
| 145 | :class:`Repr` to add support for additional built-in object types or to modify |
| 146 | the handling of types already supported. This example shows how special support |
| 147 | for file objects could be added:: |
| 148 | |
Benjamin Peterson | aa30669 | 2008-10-15 03:09:45 +0000 | [diff] [blame] | 149 | import reprlib |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | import sys |
| 151 | |
Benjamin Peterson | aa30669 | 2008-10-15 03:09:45 +0000 | [diff] [blame] | 152 | class MyRepr(reprlib.Repr): |
Berker Peksag | edd6ec2 | 2014-10-12 05:11:16 +0300 | [diff] [blame] | 153 | |
| 154 | def repr_TextIOWrapper(self, obj, level): |
| 155 | if obj.name in {'<stdin>', '<stdout>', '<stderr>'}: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | return obj.name |
Berker Peksag | edd6ec2 | 2014-10-12 05:11:16 +0300 | [diff] [blame] | 157 | return repr(obj) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 158 | |
| 159 | aRepr = MyRepr() |
Georg Brandl | ede6c2a | 2010-01-05 10:22:04 +0000 | [diff] [blame] | 160 | print(aRepr.repr(sys.stdin)) # prints '<stdin>' |