r""" htmlize(): generic function example # BEGIN HTMLIZE_DEMO >>> htmlize({1, 2, 3}) # <1> '
{1, 2, 3}
' >>> htmlize(abs) '
<built-in function abs>
' >>> htmlize('Heimlich & Co.\n- a game') # <2> '

Heimlich & Co.
\n- a game

' >>> htmlize(42) # <3> '
42 (0x2a)
' >>> print(htmlize(['alpha', 66, {3, 2, 1}])) # <4> # END HTMLIZE_DEMO """ # BEGIN HTMLIZE from functools import singledispatch import html @singledispatch # <1> def htmlize(obj): content = html.escape(repr(obj)) return '
{}
'.format(content) @htmlize.register(str) # <2> def _(text): # <3> content = html.escape(text).replace('\n', '
\n') return '

{0}

'.format(content) @htmlize.register(int) # <4> def _(n): return '
{0} (0x{0:x})
'.format(n) @htmlize.register(list) def _(a_list): inner = '\n
  • '.join(htmlize(item) for item in a_list) return '' # END HTMLIZE