<commit_before>from django.core.urlresolvers import reverse
from avocado.events import usage
from .base import FieldBase


class FieldStats(FieldBase):
    "Field Stats Resource"

    def get(self, request, pk):
        uri = request.build_absolute_uri
        instance = request.instance

        if instance.simple_type == 'number':
            stats = instance.max().min().avg()
        elif (instance.simple_type == 'date' or
              instance.simple_type == 'time' or
              instance.simple_type == 'datetime'):
            stats = instance.max().min()
        else:
            stats = instance.count(distinct=True)

        if stats is None:
            resp = {}
        else:
            resp = next(iter(stats))

        resp['_links'] = {
            'self': {
                'href': uri(
                    reverse('serrano:field-stats', args=[instance.pk])),
            },
            'parent': {
                'href': uri(reverse('serrano:field', args=[instance.pk])),
            },
        }
<commit_msg>Return empty response when StopIteration is raised on exhausted iterator<commit_after>from django.core.urlresolvers import reverse
from avocado.events import usage
from .base import FieldBase


class FieldStats(FieldBase):
    "Field Stats Resource"

    def get(self, request, pk):
        uri = request.build_absolute_uri
        instance = request.instance

        if instance.simple_type == 'number':
            stats = instance.max().min().avg()
        elif (instance.simple_type == 'date' or
              instance.simple_type == 'time' or
              instance.simple_type == 'datetime'):
            stats = instance.max().min()
        else:
            stats = instance.count(distinct=True)

        if stats is None:
            resp = {}
        else:
            try:
                resp = next(iter(stats))
            except StopIteration:
                resp = {}

        resp['_links'] = {
            'self': {
                'href': uri(
                    reverse('serrano:field-stats', args=[instance.pk])),
            },
            'parent': {
                'href': uri(reverse('serrano:field', args=[instance.pk])),
            },
        }

{'fraction_non_alphanumeric': 0.11969286359530262, 'fraction_numerical': 0.0, 'mean_word_length': 1.7413366336633664, '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': '7369457', 'n_tokens_mistral': 596, 'n_tokens_neox': 573, 'n_words': 137}