<commit_before>import os
import contextlib


@contextlib.contextmanager
def cd(path):
    '''Creates the path if it doesn't exist'''
    old_dir = os.getcwd()
    try:
        os.makedirs(path)
    except OSError:
        pass
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(old_dir)


class SetDefault:
    '''Context manager like getattr, but yields a default value,
    and sets on the instance on exit:

    with SetDefault(obj, attrname, []) as attr:
        attr.append('something')
    print obj.something
    '''
    def __init__(self, obj, attr, default_val):
        self.obj = obj
        self.attr = attr
        self.default_val = default_val

    def __enter__(self):
        val = getattr(self.obj, self.attr, self.default_val)
        self.val = val
        return val

    def __exit__(self, exc_type, exc_value, traceback):
        setattr(self.obj, self.attr, self.val)


def set_default(obj, attr, default_val):
    with SetDefault(obj, attr, default_val) as result:
        return result


@contextlib.contextmanager
def index_error_stopiter():
    try:
        yield
    except IndexError:
        raise StopIteration()<commit_msg>Can pass callable to set_default<commit_after>import os
import contextlib


@contextlib.contextmanager
def cd(path):
    '''Creates the path if it doesn't exist'''
    old_dir = os.getcwd()
    try:
        os.makedirs(path)
    except OSError:
        pass
    os.chdir(path)
    try:
        yield
    finally:
        os.chdir(old_dir)


class SetDefault:
    '''Context manager like getattr, but yields a default value,
    and sets on the instance on exit:

    with SetDefault(obj, attrname, []) as attr:
        attr.append('something')
    print obj.something
    '''
    def __init__(self, obj, attr, default_val):
        self.obj = obj
        self.attr = attr
        self.default_val = default_val

    def __enter__(self):
        if not hasattr(self.obj, self.attr):
            if callable(self.default_val):
                val = self.default_val()
            else:
                val = self.default_val
        self.val = val
        return val

    def __exit__(self, exc_type, exc_value, traceback):
        setattr(self.obj, self.attr, self.val)


def set_default(obj, attr, default_val):
    with SetDefault(obj, attr, default_val) as result:
        return result


@contextlib.contextmanager
def index_error_stopiter():
    try:
        yield
    except IndexError:
        raise StopIteration()
{'fraction_non_alphanumeric': 0.09327461969575661, 'fraction_numerical': 0.0, 'mean_word_length': 2.4186046511627906, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '4355874', 'n_tokens_mistral': 781, 'n_tokens_neox': 762, 'n_words': 214}