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