<jupyter_start><jupyter_text>To be run in **Jupyter** to generate `../exercises/*.ipynb` from `./*.ipynb`<jupyter_code>import json, os, re
from IPython.core.display import display, HTML
def convert(code):
    converted = []
    for line in code:
        assert 'todo' not in line.lower()
        m = re.match('^\s*(?:#|//|<!-- )--snip', line)
        if m:
            break
        m = re.match(r'^(\s*)(.*?)\s+(?:#|//)([^ @].*)$', line)
        converted.append((m.group(1) + m.group(3) + '\n') if m else line)
    return converted
SOURCE = '.'
DESTINATION = '../exercises'
NOTE_FMT = '### NOTE: This file is auto-generated from "%s".\n\n'
LINK_FMT = ('<li><a target="_blank" href="%s">%s</a> (%d bytes) &#8680; '
            '<a target="_blank" href="%s">%s</a> (%d bytes)</li>')
if not os.path.exists(DESTINATION):
    os.mkdir(DESTINATION)
display(HTML('<pre>'))
for name in sorted(os.listdir(SOURCE)):
    if not name.endswith('.ipynb') or name.startswith('_'):
        continue
    path = os.path.join(SOURCE, name)
    nb = json.load(open(path))
    for cell in nb['cells']:
        if cell['cell_type'] == 'code':
            cell['source'] = convert(cell['source'])
        if 'outputs' in cell: cell['outputs'] = []
        if 'execution_count' in cell: cell['execution_count'] = None
#     nb['cells'][0]['source'].insert(0, NOTE_FMT % os.path.abspath(path))
    path2 = os.path.join(DESTINATION, name)
    json.dump(nb, open(path2, 'w'))
    display(HTML(LINK_FMT % (path, path, os.path.getsize(path),
                             path2, path2, os.path.getsize(path2))))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20954742715437075, 'fraction_numerical': 0.008059516429014259, 'mean_word_length': 3.045112781954887, 'pattern_counts': {'":': 0, '<': 13, '<?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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '213687', 'license': 'Apache-2.0', 'n_tokens_mistral': 599, 'n_tokens_neox': 593, 'n_words': 128, 'path': 'extras/amld/notebooks/solutions/_generate_exercises.ipynb', 'repo_name': 'cesar-ilharco/workshops', 'size': '1613'}