<jupyter_start><jupyter_text>Decorator Application: Single Dispatch<jupyter_code>from html import escape
def html_escape(arg):
    return escape(str(arg))

def html_int(a):
    return '{0}(<i>{1}</i>)'.format(a, str(hex(a)))

def html_real(a):
    return '{0:.2f}'

def html_str(s):
    return html_escape(s).replace('\n', '<br/>\n')

def html_list(l):
'{0}(<1>{1}</i>)'.format(a, str(hex(a)))

def html_dict(d):
    items = ('<li>{0}={1}</li>'.format(k, v)
            for k, v in d.items())
    return '<ul>\n' + '\n'.join(items) + '\n</ul>'


print(html_str("""this is
a multi line string
with special charactors: 10 < 100"""))
print(html_int(255))
print(html_escape(3+10j))
from decimal import Decimal
htmlize(100)
htmlize("""Python
rocks!
""")
print(htmlize([1, 2, 3]))
print(htmlize(["""Python
rocks!
""", (10, 20, 30), 100]))
def func1():
    func2()
def func2():
    print('func2')
func1()
from decimal import Decimal
from html import escape
def htmlize(arg):
    if isinstance(arg, int):
        return html_int(arg)
    elif isinstance(arg, float) or isinstance(arg, Decimal):
        return html_real(arg)
    elif isinstance(arg, str):
        return html_real(arg)
    elif isinstance(arg, list) or isinstanceI(arg, tuple):
        return html_list(arg)
    elif isinstance(arg, dict):
        return html_dict(arg)
    elif isinstance(arg. set):
        return html_set()
    else:
        return html_escape(arg)
    
def html_escape(arg):
    return escape(str(arg))

def html_int(a):
    return '{0}(<i>{1}</i>)'.format(a, str(hex(a)))

def html_real(a):
    return '{0:.2f}'

def html_str(s):
    return html_escape(s).replace('\n', '<br/>\n')

def html_list(l):
    items = ('<li>{0}</li>'.format(html_escape(item))
            for item in l
            )
    return '<ul>\n' + '\n'.join(items) + '\n</ul>'

def html_dict(d):
    items = ('<li>{0}={1}</li>'.format(html_escape(k), htmlize(v))
            for k, v in d.items())
    return '<ul>\n' + '\n'.join(items) + '\n</ul>'

def html_set(arg):
    return html_list(arg)
htmlize(100)
print(htmlize(["""Python
rocks!
""", (10, 20, 30), 100]))
htmlize([1, 2, 3])
def htmlize(arg):
    registry = {
        object: html_escape,
        int: html_int,
        float: html_real,
        Decimal: html_int,
        str: html_str,
        list: html_list,
        tuple: html_list,
        set: html_list,
        dict: html_dict
    }
    
    fn = registry.get(type(arg), registry[object])
    
    return fn(arg)
def singledispatch(fn):
    registry = {}
    
    registry[object] = fn
    
    def inner(arg):
        return registry[object](arg)
    
        
    return inner
@singledispatch
def htmlize(a):
    return escape(str(a))
htmlize('1 < 100')
def singledispatch(fn):
    registry = {}
    
    registry[object] = fn
    registry[int] = lambda a: '{0}(<1>{1}</i>)'.format(a, str(hex(a)))
    registry[str] = lambda s: escape(s).replace('\n', '<br/>\n')
    
    def inner(arg):
        return registry.get(type(arg), registry[object])
        
    return inner
@singledispatch
def htmlize(a):
    return escape(str(a))
htmlize('1 < 100')
htmlize(100)
def singledispatch(fn):
    registry = {}
    
    registry[object] = fn
    
    def decorated(arg):
        return registry.get(type(arg), registry[object])
    
    def register(type_):
        def inner(fn):
            registry[type_] = fn
            return fn
        return inner
        
    decorated.register = register
#     decorated.registry = registry
    return decorated
@singledispatch
def htmlize(a):
    return escape(str(a))
htmlize
htmlize('1 < 100')
htmlize.register
@htmlize.register(int)
def html_int(a):
    return '{0}(<1>{1}</i>)'.format(a, str(hex(a)))

html_int
htmlize(199)
@htmlize.register(tuple)
@htmlize.register(list)
def html_list(l):
    items = ('<li>{0}</i>)'.format.format(htmlize(item))
            for item in l
            )
    return '<ul>\n' + '\n'.join(items) + '\n</ul>'
htmlize([1, 2, 3])
htmlize((1, 2, 3))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17865140582234387, 'fraction_numerical': 0.023140084598158746, 'mean_word_length': 3.0524193548387095, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '593696', 'license': 'Unlicense', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1450, 'n_words': 323, 'path': 'my_classes/ScopesClosuresAndDecorators/.ipynb_checkpoints/decorator_application_single_dispatch-checkpoint.ipynb', 'repo_name': 'minefarmer/deep-Dive-1', 'size': '4019'}