<commit_before>import importlib
from itertools import chain

from rest_framework import fields
from rest_framework.exceptions import ParseError, NotFound
from django.db.models.related import RelatedObject
from django.db.models import ManyToManyField


def field_is_remote(model, field_name):
    """
    Helper function to determine whether model field is remote or not.
    Remote fields are many-to-many or many-to-one.
    """
    if not hasattr(model, '_meta'):
        # ephemeral model with no metaclass
        return False

    meta = model._meta
    try:
        model_field = meta.get_field_by_name(field_name)[0]
        return isinstance(model_field, (ManyToManyField, RelatedObject))
    except:
        related_objects = chain(
            meta.get_all_related_objects(),
            meta.get_all_related_many_to_many_objects()
        )
        related_objects_map = {
            o.get_accessor_name(): o
            for o in related_objects
        }
        if field_name in related_objects_map:
            return True
        else:
            raise AttributeError(
                '%s is not a valid field for %s' % (field_name, model)
            )


class DynamicField(fields.Field):

    """
    Generic field to capture additional custom field attributes
    """
<commit_msg>more efficient map<commit_after>import importlib
from itertools import chain

from rest_framework import fields
from rest_framework.exceptions import ParseError, NotFound
from django.db.models.related import RelatedObject
from django.db.models import ManyToManyField


def field_is_remote(model, field_name):
    """
    Helper function to determine whether model field is remote or not.
    Remote fields are many-to-many or many-to-one.
    """
    if not hasattr(model, '_meta'):
        # ephemeral model with no metaclass
        return False

    meta = model._meta
    try:
        model_field = meta.get_field_by_name(field_name)[0]
        return isinstance(model_field, (ManyToManyField, RelatedObject))
    except:
        related_object_names = {
            o.get_accessor_name()
            for o in chain(
                meta.get_all_related_objects(),
                meta.get_all_related_many_to_many_objects()
            )
        }
        if field_name in related_object_names:
            return True
        else:
            raise AttributeError(
                '%s is not a valid field for %s' % (field_name, model)
            )


class DynamicField(fields.Field):

    """
    Generic field to capture additional custom field attributes
    """

{'fraction_non_alphanumeric': 0.0675990675990676, 'fraction_numerical': 0.000777000777000777, 'mean_word_length': 2.4938941655359566, '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': '475965', 'n_tokens_mistral': 709, 'n_tokens_neox': 694, 'n_words': 207}