1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
"""Tools to generate __repr__ strings.
"""
from __future__ import unicode_literals
import typing
if typing.TYPE_CHECKING:
from typing import Text, Tuple
def make_repr(class_name, *args, **kwargs):
# type: (Text, *object, **Tuple[object, object]) -> Text
"""Generate a repr string.
Positional arguments should be the positional arguments used to
construct the class. Keyword arguments should consist of tuples of
the attribute value and default. If the value is the default, then
it won't be rendered in the output.
Example:
>>> class MyClass(object):
... def __init__(self, name=None):
... self.name = name
... def __repr__(self):
... return make_repr('MyClass', 'foo', name=(self.name, None))
>>> MyClass('Will')
MyClass('foo', name='Will')
>>> MyClass(None)
MyClass('foo')
"""
arguments = [repr(arg) for arg in args]
arguments.extend(
[
"{}={!r}".format(name, value)
for name, (value, default) in sorted(kwargs.items())
if value != default
]
)
return "{}({})".format(class_name, ", ".join(arguments))
|