File: util.py

package info (click to toggle)
python-fudge 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 716 kB
  • sloc: javascript: 3,300; python: 2,537; makefile: 84; sh: 5
file content (36 lines) | stat: -rw-r--r-- 1,076 bytes parent folder | download
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

try:
    from functools import wraps
except ImportError:
    # for python < 2.5... this is a limited set of what we need to do
    # with a wrapped function :
    def wraps(f):
        def wrap_with_f(new_f):
            new_f.__name__ = f.__name__
            if hasattr(f, '__module__'):
                new_f.__module__ = f.__module__
            return new_f
        return wrap_with_f

def fmt_val(val, shorten=True):
    """Format a value for inclusion in an 
    informative text string.
    """
    val = repr(val)
    max = 50
    if shorten:
        if len(val) > max:
            close = val[-1]
            val = val[0:max-4] + "..."
            if close in (">", "'", '"', ']', '}', ')'):
                val = val + close
    return val

def fmt_dict_vals(dict_vals, shorten=True):
    """Returns list of key=val pairs formatted
    for inclusion in an informative text string.
    """
    items = list(dict_vals.items())
    if not items:
        return [fmt_val(None, shorten=shorten)]
    return ["%s=%s" % (k, fmt_val(v, shorten=shorten)) for k,v in items]