Exemplo n.º 1
0
def type_stringifier(value):
    if HAVE_NUMPY and isinstance(value, numpy.ndarray):
        return text_type("ndarray %s %s") % (value.dtype, value.shape)

    elif isinstance(value, STR_SAFE_TYPES):
        try:
            return text_type(value)
        except Exception:
            pass

    elif hasattr(type(value), "safely_stringify_for_pudb"):
        try:
            # (E.g.) Mock objects will pretend to have this
            # and return nonsense.
            result = value.safely_stringify_for_pudb()
        except Exception:
            pass
        else:
            if isinstance(result, string_types):
                return text_type(result)

    elif type(value) in [set, frozenset, list, tuple, dict]:
        return text_type("%s (%s)") % (type(value).__name__, len(value))

    return text_type(type(value).__name__)
Exemplo n.º 2
0
def type_stringifier(value):
    if HAVE_NUMPY and isinstance(value, numpy.ndarray):
        return text_type("ndarray %s %s") % (value.dtype, value.shape)

    elif isinstance(value, STR_SAFE_TYPES):
        try:
            return text_type(value)
        except Exception:
            pass

    elif hasattr(type(value), "safely_stringify_for_pudb"):
        try:
            # (E.g.) Mock objects will pretend to have this
            # and return nonsense.
            result = value.safely_stringify_for_pudb()
        except Exception:
            pass
        else:
            if isinstance(result, string_types):
                return text_type(result)

    elif type(value) in [set, frozenset, list, tuple, dict]:
        return text_type("%s (%s)") % (type(value).__name__, len(value))

    return text_type(type(value).__name__)
Exemplo n.º 3
0
def get_stringifier(iinfo):
    """Return a function that turns an object into a Unicode text object."""

    if iinfo.display_type == "type":
        return type_stringifier
    elif iinfo.display_type == "repr":
        return repr
    elif iinfo.display_type == "str":
        return str
    elif iinfo.display_type == "id":
        return id_stringifier
    else:
        try:
            if not custom_stringifier_dict:  # Only execfile once
                from os.path import expanduser
                execfile(expanduser(iinfo.display_type),
                         custom_stringifier_dict)
        except Exception:
            ui_log.exception("Error when importing custom stringifier")
            return error_stringifier
        else:
            if "pudb_stringifier" not in custom_stringifier_dict:
                print(
                    "%s does not contain a function named pudb_stringifier at "
                    "the module level." % iinfo.display_type)
                raw_input("Hit enter:")
                return lambda value: text_type(
                    "ERROR: Invalid custom stringifier file: "
                    "pudb_stringifer not defined.")
            else:
                return (lambda value: text_type(custom_stringifier_dict[
                    "pudb_stringifier"](value)))
Exemplo n.º 4
0
def get_stringifier(iinfo):
    """Return a function that turns an object into a Unicode text object."""

    if iinfo.display_type == "type":
        return type_stringifier
    elif iinfo.display_type == "repr":
        if PY3:
            return repr
        else:
            return lambda value: repr(value).decode("utf-8")
    elif iinfo.display_type == "str":
        if PY3:
            return str
        else:
            return lambda value: (value.decode("utf-8") if isinstance(
                value, bytes) else text_type(value))
    else:
        try:
            if not custom_stringifier_dict:  # Only execfile once
                from os.path import expanduser
                execfile(expanduser(iinfo.display_type),
                         custom_stringifier_dict)
        except Exception:
            print("Error when importing custom stringifier:")
            from traceback import print_exc
            print_exc()
            raw_input("Hit enter:")
            return lambda value: text_type(
                "ERROR: Invalid custom stringifier file.")
        else:
            if "pudb_stringifier" not in custom_stringifier_dict:
                print(
                    "%s does not contain a function named pudb_stringifier at "
                    "the module level." % iinfo.display_type)
                raw_input("Hit enter:")
                return lambda value: text_type(
                    "ERROR: Invalid custom stringifier file: "
                    "pudb_stringifer not defined.")
            else:
                return (lambda value: text_type(custom_stringifier_dict[
                    "pudb_stringifier"](value)))
Exemplo n.º 5
0
def type_stringifier(value):
    if HAVE_NUMPY and isinstance(value, numpy.ndarray):
        return text_type("%s(%s) %s") % (type(value).__name__, value.dtype,
                                         value.shape)

    elif HAVE_NUMPY and isinstance(value, numpy.number):
        return text_type("%s (%s)" % (value, value.dtype))

    elif isinstance(value, STR_SAFE_TYPES):
        try:
            return text_type(value)
        except Exception:
            message = "string safe type stringifier failed"
            ui_log.exception(message)
            return "!! %s !!" % message

    elif hasattr(type(value), "safely_stringify_for_pudb"):
        try:
            # (E.g.) Mock objects will pretend to have this
            # and return nonsense.
            result = value.safely_stringify_for_pudb()
        except Exception:
            message = "safely_stringify_for_pudb call failed"
            ui_log.exception(message)
            result = "!! %s !!" % message

        if isinstance(result, string_types):
            return text_type(result)

    elif type(value) in [set, frozenset, list, tuple, dict]:
        return text_type("%s (%s)") % (type(value).__name__, len(value))

    return text_type(type(value).__name__)
Exemplo n.º 6
0
def get_stringifier(iinfo):
    """Return a function that turns an object into a Unicode text object."""

    if iinfo.display_type == "type":
        return type_stringifier
    elif iinfo.display_type == "repr":
        if PY3:
            return repr
        else:
            return lambda value: repr(value).decode("utf-8")
    elif iinfo.display_type == "str":
        if PY3:
            return str
        else:
            return lambda value: (
                    value.decode("utf-8") if isinstance(value, bytes)
                    else text_type(value))
    else:
        try:
            if not custom_stringifier_dict:  # Only execfile once
                from os.path import expanduser
                execfile(expanduser(iinfo.display_type), custom_stringifier_dict)
        except Exception:
            print("Error when importing custom stringifier:")
            from traceback import print_exc
            print_exc()
            raw_input("Hit enter:")
            return lambda value: text_type("ERROR: Invalid custom stringifier file.")
        else:
            if "pudb_stringifier" not in custom_stringifier_dict:
                print("%s does not contain a function named pudb_stringifier at "
                      "the module level." % iinfo.display_type)
                raw_input("Hit enter:")
                return lambda value: text_type(
                        "ERROR: Invalid custom stringifier file: "
                        "pudb_stringifer not defined.")
            else:
                return (lambda value:
                    text_type(custom_stringifier_dict["pudb_stringifier"](value)))
Exemplo n.º 7
0
def pudb_stringifier(value):
    if HAVE_NUMPY and isinstance(value, numpy.ndarray):
        return text_type("ndarray %s %s") % (value.dtype, value.shape)
    elif HAVE_NUMPY and numpy.isscalar(value):
        return text_type("%s") % value
    elif HAVE_PYTORCH and isinstance(value, torch.Tensor):
        dtype = str(value.dtype)[6:]
        shape = tuple(value.size())
        if shape:
            return text_type("tTensor %s %s") % (dtype, shape)
        else:
            return text_type("tTensor %s %s %s") % (dtype, shape, value.item())
    elif isinstance(value, Path):
        return text_type("Path %s") % value

    elif isinstance(value, STR_SAFE_TYPES):
        try:
            return text_type(value)
        except Exception:
            pass

    elif hasattr(type(value), "safely_stringify_for_pudb"):
        try:
            # (E.g.) Mock objects will pretend to have this
            # and return nonsense.
            result = value.safely_stringify_for_pudb()
        except Exception:
            pass
        else:
            if isinstance(result, string_types):
                return text_type(result)

    elif type(value) in [set, frozenset, list, tuple, dict]:
        return text_type("%s (%s)") % (type(value).__name__, len(value))

    return text_type(type(value).__name__)