<commit_before>'''
Django-style URL dispatcher view.

    App(root_url=url_dispatcher(
        (r'^/$', views.index),
        (re.compile(r'^/(?P<foo>\d+)/'), views.detail, {'bar': True}),
    )

The view will be called with the request, and any matched _named_ groups.
Extra kwargs can be passed as a 3rd positional argument.

There is a namedtuple class defined called URL provided.  All patterns will be
assembled as a URL instance, and their regex compiled.  If kwargs are not
specified, they will default to {}.
'''

from collections import namedtuple
from functools import partial
import re

from . import response

class KeepLooking(Exception):
    '''Used to tell a url_dispatcher to skip this pattern and keep looking.'''
    pass

URL = namedtuple('url', ('regex', 'view'))

class url_dispatcher(object):
    __slots__ = ('patterns',)

    def __init__(self, *patterns):
        self.patterns = [self._make_url(pattern) for pattern in patterns]

    def _make_url(self, pattern):
        '''Helper to ensure all patterns are url instances.'''
        return URL(re.compile(pattern[0]), pattern[1])

    def __call__(self, request, *args, **kwargs):
        path = getattr(request, 'remaining_path', request.path)
        for pattern in self.patterns:
            m = pattern.regex.match(path)
            if m:
                request.remaining_path = path[m.end():]<commit_msg>Fix import for file move<commit_after>'''
Django-style URL dispatcher view.

    App(root_url=url_dispatcher(
        (r'^/$', views.index),
        (re.compile(r'^/(?P<foo>\d+)/'), views.detail, {'bar': True}),
    )

The view will be called with the request, and any matched _named_ groups.
Extra kwargs can be passed as a 3rd positional argument.

There is a namedtuple class defined called URL provided.  All patterns will be
assembled as a URL instance, and their regex compiled.  If kwargs are not
specified, they will default to {}.
'''

from collections import namedtuple
from functools import partial
import re

from antfarm import response

class KeepLooking(Exception):
    '''Used to tell a url_dispatcher to skip this pattern and keep looking.'''
    pass

URL = namedtuple('url', ('regex', 'view'))

class url_dispatcher(object):
    __slots__ = ('patterns',)

    def __init__(self, *patterns):
        self.patterns = [self._make_url(pattern) for pattern in patterns]

    def _make_url(self, pattern):
        '''Helper to ensure all patterns are url instances.'''
        return URL(re.compile(pattern[0]), pattern[1])

    def __call__(self, request, *args, **kwargs):
        path = getattr(request, 'remaining_path', request.path)
        for pattern in self.patterns:
            m = pattern.regex.match(path)
            if m:
                request.remaining_path = path[m.end():]
{'fraction_non_alphanumeric': 0.11421410669530971, 'fraction_numerical': 0.0021482277121374865, 'mean_word_length': 3.3385093167701863, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4293382', 'n_tokens_mistral': 822, 'n_tokens_neox': 803, 'n_words': 292}