<jupyter_start><jupyter_text><jupyter_code>from functools import singledispatch

singledispatch?
"""Reimplement hyperextend using singledispatch"""

@singledispatch
def hyperextend(item, l):
    """Append by default"""
    l.append(item)
    return l

@hyperextend.register(Iterable)
def _(item, l):
    """Extend for Iterable items"""
    l.extend(item)
    return l
    
letters = ['a', 'b', 'c']
hyperextend(['easy as', 1, 2], letters)
hyperextend(3, letters)<jupyter_output><empty_output><jupyter_text><jupyter_code>"""raze: flatten anything

For example:
>>> raze([
    1, 2, {
        '3': [4, {
            ('5', 6): (7, 8)
        }]
    }
])
# [1, 2, 3, 4, 5, 6, 7, 8]
"""
from functools import singledispatch

@singledispatch
def raze(item):
    """Raze returns the item unchanged by default"""
    return item

@raze.register(Iterable)  # noqa
def _(l):
    items = []
    for item in l:
        result = raze(item)
        hyperextend(result, items)
    return items

@raze.register(str)
def _(s):
    return s

@raze.register(dict)
def _(d):
    """Flatten dictionaries by treating it as a list of key, value tuples"""
    items = []
    for key, value in d.items():
        result = raze((key, value))
        hyperextend(result, items)
    return items

nested_message = {'S': {'to': {('o', 'p'): [' ', ['ki', ['ds ', {'af': 'raid'}, ' ', ['t', 'o ', 'le'], {'ave': [' ', 'hi', 's', ' ', 'sto', 'op!']}]]]}}}

message = raze(nested_message)
''.join(message)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1877496671105193, 'fraction_numerical': 0.012649800266311585, 'mean_word_length': 3.5545454545454547, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '115151', 'license': 'MIT', 'n_tokens_mistral': 581, 'n_tokens_neox': 564, 'n_words': 146, 'path': 'singledispatch.ipynb', 'repo_name': 'mshenfield/single-dispatch-lightning-talk', 'size': '1502'}