<commit_before>import re
from collections import OrderedDict
import traceback


# We need to accomodate this kind of workflow:
# https://ipython.org/ipython-doc/1/interactive/notebook.html#keyboard-shortcuts


def parse_notebook(text):
    parts = re.split('\s*\n#: +(.*)\n\s*', '\n'+text+'\n')
    assert parts[0].strip() == ''
    if len(parts) % 2 == 1:
        parts.append('')
    parts = iter(parts)
    next(parts)
    parts = zip(parts, parts)
    return OrderedDict(parts)


def make_target():
    locals = {}
    def target(arg):
        name, text = arg
        print('>>> running {!r}'.format(name))
        print('\n'.join('... ' + l for l in text.splitlines()))
        try:
            exec(text, {}, locals)
        except Exception:
            traceback.print_exc()
    return target


def make_driver(notebook_path, completer):
    name = old_name = None

    def driver(do):
        nonlocal name
        nonlocal old_name

        with open(notebook_path) as f:
            notebook_text = f.read()
        cells = parse_notebook(notebook_text)

        completer.words = list(cells)

        name = input('>>> at {!r}; run: '.format(old_name)).strip()
        # TODO: handle EOF

        if not name.strip():
            name = old_name
        else:
            old_name = name

        do((name, cells[name]))

        return False

    return driver
<commit_msg>Ask what cell to run before reloading the notebook.<commit_after>import re
from collections import OrderedDict
import traceback


# We need to accomodate this kind of workflow:
# https://ipython.org/ipython-doc/1/interactive/notebook.html#keyboard-shortcuts


def parse_notebook(text):
    parts = re.split('\s*\n#: +(.*)\n\s*', '\n'+text+'\n')
    assert parts[0].strip() == ''
    if len(parts) % 2 == 1:
        parts.append('')
    parts = iter(parts)
    next(parts)
    parts = zip(parts, parts)
    return OrderedDict(parts)


def make_target():
    locals = {}
    def target(arg):
        name, text = arg
        print('>>> running {!r}'.format(name))
        print('\n'.join('... ' + l for l in text.splitlines()))
        try:
            exec(text, {}, locals)
        except Exception:
            traceback.print_exc()
    return target


def make_driver(notebook_path, completer):
    name = old_name = None

    def driver(do):
        nonlocal name
        nonlocal old_name

        name = input('>>> at {!r}; run: '.format(old_name)).strip()
        # TODO: handle EOF

        with open(notebook_path) as f:
            notebook_text = f.read()
        cells = parse_notebook(notebook_text)

        completer.words = list(cells)

        if not name.strip():
            name = old_name
        else:
            old_name = name

        do((name, cells[name]))

        return False

    return driver

{'fraction_non_alphanumeric': 0.13549075391180654, 'fraction_numerical': 0.002844950213371266, 'mean_word_length': 2.2407834101382487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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}}
{'dir': 'git-commits-cleaned', 'id': '5413077', 'n_tokens_mistral': 907, 'n_tokens_neox': 873, 'n_words': 224}