<commit_before>from django.core.urlresolvers import NoReverseMatch
from rest_framework.fields import ReadOnlyField, SkipField
from rest_framework.reverse import reverse


class CollapsedIdentityField(ReadOnlyField):
    """
    Returns the instance ID without making a database query.
    """

    def get_attribute(self, instance):
        if self.field_name not in self.parent.fields:
            raise SkipField()

        source = self.parent.source + '_id'

        if hasattr(instance, source):
            return getattr(instance, source)
        else:
            raise SkipField()


class CollapsedHyperlinkField(CollapsedIdentityField):
    """
    Returns the instance URL without making a database query.
    """

    def get_attribute(self, instance):
        identity = super(CollapsedHyperlinkField, self).get_attribute(instance)

        try:
            viewname = self.parent.fields[self.field_name].view_name
            args = (identity,)
            request = self.context['request']

            return reverse(viewname, args, request=request)

        except (AttributeError, KeyError, NoReverseMatch):
            raise SkipField()<commit_msg>Fixed collapsed fields not handling root source.<commit_after>from django.core.urlresolvers import NoReverseMatch
from rest_framework.fields import ReadOnlyField, SkipField
from rest_framework.reverse import reverse


class CollapsedIdentityField(ReadOnlyField):
    """
    Returns the instance ID without making a database query.
    """

    def get_attribute(self, instance):
        if self.field_name not in self.parent.fields:
            raise SkipField()

        source = self.parent.source

        if source == '*':
            source = 'pk'
        else:
            source += '_id'

        if hasattr(instance, source):
            return getattr(instance, source)
        else:
            raise SkipField()


class CollapsedHyperlinkField(CollapsedIdentityField):
    """
    Returns the instance URL without making a database query.
    """

    def get_attribute(self, instance):
        identity = super(CollapsedHyperlinkField, self).get_attribute(instance)

        try:
            viewname = self.parent.fields[self.field_name].view_name
            args = (identity,)
            request = self.context['request']

            return reverse(viewname, args, request=request)

        except (AttributeError, KeyError, NoReverseMatch):
            raise SkipField()
{'fraction_non_alphanumeric': 0.08224755700325732, 'fraction_numerical': 0.0, 'mean_word_length': 2.7227272727272727, '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': '7568471', 'n_tokens_mistral': 636, 'n_tokens_neox': 607, 'n_words': 171}