Example #1
0
 def hide(name):
     value = fi.assignments[name]
     if callable(value):
         qualified_name, path, *_ = inspect_callable(value)
         is_builtin = value.__class__.__name__ == 'builtin_function_or_method'
         is_boring = is_builtin or (qualified_name == name)
         is_suppressed = match(path, suppressed_paths)
         return is_boring or is_suppressed
     return False
def format_value(value, indent=0, truncation=None, wrap=60,
                 max_depth=2, depth=0):
    """
    Stringify some object

    Params
    ---
    value: object to be formatted

    indent: int
        insert extra spaces on each line

    truncation: int
        cut after this nr of characters

    wrap: int
        insert linebreaks after this nr of characters, use 0 to never add a linebreak

    max_depth: int
        max repeated calls to this function when formatting container types

    depth: int
        current nesting level

    Returns
    ---
    string
    """

    if depth > max_depth:
        return '...'

    if isinstance(value, UnresolvedAttribute):
        reason = "# %s" % (value.exc_type)
        val_tpl = reason + "\n%s = %s"
        lastval_str = format_value(value.last_resolvable_value,
                                   truncation=truncation, indent=3, depth=depth+1)
        val_str = val_tpl % (value.last_resolvable_name, lastval_str)
        indent = 10

    elif isinstance(value, (list, tuple, set)):
        val_str = format_iterable(value, truncation, max_depth, depth)

    elif isinstance(value, dict):
        val_str = format_dict(value, truncation, max_depth, depth)

    elif np and isinstance(value, np.ndarray):
        val_str = format_array(value, minimize=depth > 0)

    elif callable(value):
        name, filepath, method_owner, ln = inspect_callable(value)
        filename = os.path.basename(filepath) if filepath is not None else None
        if filename is None:
            val_str = safe_repr(value)
        elif method_owner is None:
            name_s = safe_str(name)
            filename_s = safe_str(filename)
            ln_s = safe_str(ln)
            val_str = "<function '%s' %s:%s>" % (name_s, filename_s, ln_s)
        else:
            name_s = safe_str(name)
            filename_s = safe_str(filename)
            method_owner_s = safe_str(method_owner)
            ln_s = safe_str(ln)
            val_str = "<method '%s' of %s %s:%s>" % (name_s, method_owner_s,
                                                     filename_s, ln_s)
    else:
        val_str= safe_repr_or_str(value)

    val_str = truncate(val_str, truncation)

    if depth == 0:
        val_str = wrap_lines(val_str, wrap)

    if indent > 0:
        nl_indented = '\n' + (' ' * indent)
        val_str = val_str.replace('\n', nl_indented)

    return val_str