<commit_before>import re
from django.conf.urls import patterns, url
from rest_framework.decorators import api_view
from rest_framework.response import Response
from . import views


urlre_p1 = re.compile(r'\(\?P<\w+>.*\(\?#(:\w+)\)\)')
urlre_p2 = re.compile(r'^\^|\$$')


def rev(viewname):
    for urlp in urlpatterns:
        if urlp.name == viewname:
            return urlre_p2.sub('', urlre_p1.sub(r'\g<1>', urlp._regex))
    raise RuntimeError("No reverse match for %s" % viewname)


def response(path, endpoints):
    return Response({name: path + p for name, p in endpoints.items()})


@api_view(('GET',))
def api_root(request):
    return response(request.path, {
        'currency': rev('api-currency-detail'),
        'book': rev('api-book-detail'),
    })


urlpatterns = patterns('',  # noqa
    url(r'^$',
        api_root,
        name='api-root'),
    url(r'^currency/$',
        views.CurrencyList.as_view(),
        name='api-currency-list'),
    url(r'^currency/(?P<pk>[A-Z]+(?#:cur_code))/$',
        views.CurrencyDetail.as_view(),
        name='api-currency-detail'),
    url(r'^book/$',
        views.BookList.as_view(),
        name='api-book-list'),
    url(r'^book/(?P<pk>\d+(?#:book_id))/$',
        views.BookDetail.as_view(),
        name='api-book-detail'),
)
<commit_msg>Use transactions on server side also (probably worth performance penalty)<commit_after>import re
from django.conf.urls import patterns, url
from django.db import transaction
from rest_framework.decorators import api_view
from rest_framework.response import Response
from . import views


urlre_p1 = re.compile(r'\(\?P<\w+>.*\(\?#(:\w+)\)\)')
urlre_p2 = re.compile(r'^\^|\$$')


def rev(viewname):
    for urlp in urlpatterns:
        if urlp.name == viewname:
            return urlre_p2.sub('', urlre_p1.sub(r'\g<1>', urlp._regex))
    raise RuntimeError("No reverse match for %s" % viewname)


def response(path, endpoints):
    return Response({name: path + p for name, p in endpoints.items()})


@api_view(('GET',))
def api_root(request):
    return response(request.path, {
        'currency': rev('api-currency-detail'),
        'book': rev('api-book-detail'),
    })


urlpatterns = patterns('',  # noqa
    url(r'^$',
        api_root,
        name='api-root'),
    url(r'^currency/$',
        transaction.atomic(views.CurrencyList.as_view()),
        name='api-currency-list'),
    url(r'^currency/(?P<pk>[A-Z]+(?#:cur_code))/$',
        transaction.atomic(views.CurrencyDetail.as_view()),
        name='api-currency-detail'),
    url(r'^book/$',
        views.BookList.as_view(),
        name='api-book-list'),
    url(r'^book/(?P<pk>\d+(?#:book_id))/$',
        views.BookDetail.as_view(),
        name='api-book-detail'),
)

{'fraction_non_alphanumeric': 0.19101533966398831, 'fraction_numerical': 0.003652300949598247, 'mean_word_length': 3.259720062208398, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3050921', 'n_tokens_mistral': 962, 'n_tokens_neox': 933, 'n_words': 175}