Exemplo n.º 1
0
def repr_ndarray(x, _helper):
    # noinspection PyPackageRequirements
    import numpy as np

    dims = len(x.shape)
    if (
            # Too many dimensions to be concise
            dims > 6 or
            # There's a bug with array_repr and matrices
            isinstance(x, np.matrix)
            and np.lib.NumpyVersion(np.__version__) < '1.14.0' or
            # and with masked arrays...
            isinstance(x, np.ma.MaskedArray)):
        name = type_name(x)
        if name == 'ndarray':
            name = 'array'
        return '%s(%r, shape=%r)' % (name, x.dtype, x.shape)

    edgeitems = repr_ndarray.maxparts // 2
    if dims == 3:
        edgeitems = min(edgeitems, 2)
    elif dims > 3:
        edgeitems = 1

    opts = np.get_printoptions()
    try:
        np.set_printoptions(threshold=repr_ndarray.maxparts,
                            edgeitems=edgeitems)
        return np.array_repr(x)
    finally:
        np.set_printoptions(**opts)
Exemplo n.º 2
0
def repr_OrderedDict(x, helper):
    if not x:
        return repr(x)
    helper.level += 1
    return helper.repr_iterable(iteritems(x),
                                type_name(x) + '([',
                                '])',
                                length=len(x))
Exemplo n.º 3
0
def repr_Counter(x, helper):
    length = len(x)
    if length <= repr_Counter.maxparts:
        return repr_Mapping(x, helper)
    else:
        # The default repr of Counter gives the items in decreasing order
        # of frequency. We don't do that because it would be expensive
        # to compute. We also don't show a sample of random keys
        # because someone familiar with the default repr might be misled
        # into thinking that they are the most common.
        return '{0}({1} keys)'.format(type_name(x), length)
Exemplo n.º 4
0
def register_repr(cls):
    """
    Register a repr function for cls. The function must accept two arguments:
    the object to be represented as a string, and an instance of ReprHelper.
    The registered function will be used by cheap_repr when appropriate,
    and can be retrieved by find_repr_function(cls).
    """

    assert inspect.isclass(cls), 'register_repr must be called with a class. ' \
                                 'The type of %s is %s' % (cheap_repr(cls), type_name(cls))

    def decorator(func):
        repr_registry[cls] = func
        func.__dict__.setdefault('maxparts', 6)
        return func

    return decorator
Exemplo n.º 5
0
def basic_repr(x, *_):
    return '<%s instance at %#x>' % (type_name(x), id(x))
Exemplo n.º 6
0
def repr_Printer(x, _helper):
    contents = repr(x)
    return '{0}({1})'.format(type_name(x), cheap_repr(contents))
Exemplo n.º 7
0
def repr_defaultdict(x, helper):
    return '{0}({1}, {2})'.format(type_name(x), x.default_factory,
                                  repr_dict(x, helper))
Exemplo n.º 8
0
def repr_Mapping(x, helper):
    if not x:
        return type_name(x) + '()'
    return '{0}({1})'.format(type_name(x), repr_dict(x, helper))
Exemplo n.º 9
0
def repr_ChainMap(x, helper):
    return helper.repr_iterable(x.maps, type_name(x) + '(', ')')
Exemplo n.º 10
0
def repr_QuerySet(x, _):
    try:
        model_name = x.model._meta.object_name
    except AttributeError:
        model_name = type_name(x.model)
    return '<%s instance of %s at %#x>' % (type_name(x), model_name, id(x))
Exemplo n.º 11
0
def repr_Set(x, helper):
    if not x:
        return '%s()' % type_name(x)
    else:
        return helper.repr_iterable(x, '%s({' % type_name(x), '})')
Exemplo n.º 12
0
def repr_bound_method(meth, _helper):
    obj = meth.__self__
    return '<bound method %s.%s of %s>' % (type_name(obj), meth.__name__,
                                           cheap_repr(obj))