File size: 1,045 Bytes
1600cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
"""Formatting handlers picked by a kind name.

The first dispatcher is a chain of equality tests that each return a call, which is the
shape the registry pattern rewrites. The second one is not: its branches do not take the
same arguments, and its last branch returns instead of raising, so the pattern has to
refuse it.
"""


def format_short(item):
    return str(item)


def format_long(item):
    return "%s (%s)" % (item, type(item).__name__)


def format_json(item):
    return sorted(str(part) for part in item)


def format_labelled(item, label):
    return "%s: %s" % (label, item)


def render(kind, item):
    if kind == "short":
        return format_short(item)
    elif kind == "long":
        return format_long(item)
    elif kind == "json":
        return format_json(item)
    else:
        raise ValueError("unsupported kind")


def render_pair(kind, item, label):
    if kind == "short":
        return format_short(item)
    elif kind == "labelled":
        return format_labelled(item, label)
    else:
        return None