xotl.tools.clipping
- Object string representation protocol¶
Complements for object string representation protocol.
There are contexts that using str
or repr
protocol would be inadequate
because shorter string representations are expected (e.g. formatting recursive
objects in pprint
standard module that they have a new Boolean
parameter in Python 3 named compact
).
There is a protocol to complement operators used by standard string
representation functions (__str__
, __repr__
) by defining a new one
with name __crop__
. This operator will receive some extra parameters with
default values, see crop()
function for details.
-
xotl.tools.clipping.
crop
(obj, max_width=None, canonical=False)[source]¶ Return a reduced string representation of obj.
Classes can now define a new special attribute
__crop__
. It can be astring
(orunicode
in Python 2). Or a method:def __crop__(self, max_width=None, canonical=False): pass
If the obj does not implement the
__crop__
protocol, a standard one is computed.Parameters: - max_width – Maximum length for the resulting string. If is not
given, defaults to
DEFAULT_MAX_WIDTH
. - canonical – If True
repr()
protocol must be used insteadstr()
(the default).
New in version 1.8.0.
- max_width – Maximum length for the resulting string. If is not
given, defaults to