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