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