blob: 17a6ac4506074413cacdb6b3d6a7f1d46429eac9 [file] [log] [blame]
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +00001:mod:`reprlib` --- Alternate :func:`repr` implementation
Alexandre Vassalottibee32532008-05-16 18:15:12 +00002========================================================
3
Georg Brandl116aa622007-08-15 14:28:22 +00004
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +00005.. module:: reprlib
Georg Brandl116aa622007-08-15 14:28:22 +00006 :synopsis: Alternate repr() implementation with size limits.
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
9
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +000010The :mod:`reprlib` module provides a means for producing object representations
Georg Brandl116aa622007-08-15 14:28:22 +000011with limits on the size of the resulting strings. This is used in the Python
12debugger and may be useful in other contexts as well.
13
14This 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 Brandlc5605df2009-08-13 08:26:44 +000026 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 Brandl116aa622007-08-15 14:28:22 +000030
31
32.. function:: repr(obj)
33
Georg Brandlc5605df2009-08-13 08:26:44 +000034 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 Brandl116aa622007-08-15 14:28:22 +000037
38
39.. _repr-objects:
40
41Repr Objects
42------------
43
44:class:`Repr` instances provide several members which can be used to provide
45size limits for the representations of different object types, and methods
46which 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 Brandl116aa622007-08-15 14:28:22 +000066
67.. attribute:: Repr.maxlong
68
Mark Dickinsonbf5c6a92009-01-17 10:21:23 +000069 Maximum number of characters in the representation for an integer. Digits
Georg Brandl116aa622007-08-15 14:28:22 +000070 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 Brandlc5605df2009-08-13 08:26:44 +000096 Recursive implementation used by :meth:`.repr`. This uses the type of *obj* to
Georg Brandl116aa622007-08-15 14:28:22 +000097 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
114Subclassing Repr Objects
115------------------------
116
117The 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
119the handling of types already supported. This example shows how special support
120for file objects could be added::
121
Benjamin Petersonaa306692008-10-15 03:09:45 +0000122 import reprlib
Georg Brandl116aa622007-08-15 14:28:22 +0000123 import sys
124
Benjamin Petersonaa306692008-10-15 03:09:45 +0000125 class MyRepr(reprlib.Repr):
Georg Brandl116aa622007-08-15 14:28:22 +0000126 def repr_file(self, obj, level):
127 if obj.name in ['<stdin>', '<stdout>', '<stderr>']:
128 return obj.name
129 else:
Georg Brandl0df79792008-10-04 18:33:26 +0000130 return repr(obj)
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132 aRepr = MyRepr()
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000133 print aRepr.repr(sys.stdin) # prints '<stdin>'
Georg Brandl116aa622007-08-15 14:28:22 +0000134