"""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