<commit_before>import builtins

BUILTIN_EXCEPTIONS = [
    "ArithmeticError",
    "AssertionError",
    "AttributeError",
    "BaseException",
    "BufferError",
    "BytesWarning",
    "DeprecationWarning",
    "EOFError",
    "EnvironmentError",
    "Exception",
    "FloatingPointError",
    "FutureWarning",
    "GeneratorExit",
    "IOError",
    "ImportError",
    "ImportWarning",
    "IndentationError",
    "IndexError",
    "KeyError",
    "KeyboardInterrupt",
    "LookupError",
    "MemoryError",
    "NameError",
    "NotImplemented",
    "NotImplementedError",
    "OSError",
    "OverflowError",
    "PendingDeprecationWarning",
    "ReferenceError",
    "RuntimeError",
    "RuntimeWarning",
    "StandardError",
    "StopIteration",
    "SyntaxError",
    "SyntaxWarning",
    "SystemError",
    "SystemExit",
    "TabError",
    "TypeError",
    "UnboundLocalError",
    "UnicodeDecodeError",
    "UnicodeEncodeError",
    "UnicodeError",
    "UnicodeTranslateError",
    "UnicodeWarning",
    "UserWarning",
    "ValueError",
    "Warning",
    "ZeroDivisionError",
]


class ExceptionFactory(object):
    '''
    Class for building and storing Exception types.
    Built-in exception names are reserved.


    '''
    def __init__(self):
        self._exceptions = {}

    def _build_exception(self, name):
        ex_cls = self._exceptions[name] = type(name, (Exception,), {})
        return ex_cls

    def exception(self, name, *args):
        return self.exception_cls(name)(*args)

    def exception_cls(self, name):
        if name not in BUILTIN_EXCEPTIONS:
            ex_cls = self._exceptions.get(name, None)
            if not ex_cls:
                ex_cls = self._build_exception(name)
        else:
            ex_cls = getattr(builtins, name, None)
            # maybe the exception class was deleted? Who knows
            if ex_cls is None:
                raise NameError("global name '{}' is not defined".format(name))
        return ex_cls


class ExceptionContainer(object):
    '''
    Usage:
        exceptions = ExceptionContainer()
        assert KeyError is exceptions.KeyError

        try:
            ...
        except exceptions.KeyError as e:
            print e.args
        except exceptions.SomeException as e:
            print e.args
    '''
    def __init__(self):
        self._factory = ExceptionFactory()

    def __getattr__(self, name):
        return self._factory.exception_cls(name)
<commit_msg>Minor exception_factory refactor<commit_after>import builtins

BUILTIN_EXCEPTIONS = [
    "ArithmeticError",
    "AssertionError",
    "AttributeError",
    "BaseException",
    "BufferError",
    "BytesWarning",
    "DeprecationWarning",
    "EOFError",
    "EnvironmentError",
    "Exception",
    "FloatingPointError",
    "FutureWarning",
    "GeneratorExit",
    "IOError",
    "ImportError",
    "ImportWarning",
    "IndentationError",
    "IndexError",
    "KeyError",
    "KeyboardInterrupt",
    "LookupError",
    "MemoryError",
    "NameError",
    "NotImplemented",
    "NotImplementedError",
    "OSError",
    "OverflowError",
    "PendingDeprecationWarning",
    "ReferenceError",
    "RuntimeError",
    "RuntimeWarning",
    "StandardError",
    "StopIteration",
    "SyntaxError",
    "SyntaxWarning",
    "SystemError",
    "SystemExit",
    "TabError",
    "TypeError",
    "UnboundLocalError",
    "UnicodeDecodeError",
    "UnicodeEncodeError",
    "UnicodeError",
    "UnicodeTranslateError",
    "UnicodeWarning",
    "UserWarning",
    "ValueError",
    "Warning",
    "ZeroDivisionError",
]


class ExceptionFactory(object):
    '''
    Class for building and storing Exception types.
    Built-in exception names are reserved.
    '''
    def __init__(self):
        self._exceptions = {}

    def _build_exception(self, name):
        self._exceptions[name] = type(name, (Exception,), {})
        return self._exceptions[name]

    def exception_cls(self, name):
        if name not in BUILTIN_EXCEPTIONS:
            cls = self._exceptions.get(name, None)
            if not cls:
                cls = self._build_exception(name)
        else:
            cls = getattr(builtins, name, None)
            # maybe the exception class was deleted? Who knows
            if cls is None:
                raise NameError("global name '{}' is not defined".format(name))
        return cls

    def exception(self, name, *args):
        return self.exception_cls(name)(*args)


class ExceptionContainer(object):
    '''
    Usage:
        exceptions = ExceptionContainer()
        assert KeyError is exceptions.KeyError

        try:
            ...
        except exceptions.KeyError as e:
            print e.args
        except exceptions.SomeException as e:
            print e.args
    '''
    def __init__(self):
        self._factory = ExceptionFactory()

    def __getattr__(self, name):
        return self._factory.exception_cls(name)

{'fraction_non_alphanumeric': 0.11282467532467533, 'fraction_numerical': 0.0, 'mean_word_length': 2.5358680057388807, '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': '7533121', 'n_tokens_mistral': 1441, 'n_tokens_neox': 1423, 'n_words': 313}