<commit_before>from django.core.exceptions import ValidationError
from django.db import models

from cyder.base.eav import validators
from cyder.base.eav.constants import (ATTRIBUTE_TYPES, ATTRIBUTE_INFORMATIONAL,
                                      ATTRIBUTE_OPTION, ATTRIBUTE_STATEMENT)
from cyder.base.eav.fields import AttributeValueTypeField, EAVValueField
from cyder.base.eav.utils import is_hex_byte_sequence
from cyder.base.eav.validators import VALUE_TYPES


class Attribute(models.Model):
    class Meta:
        db_table = 'attribute'


    name = models.CharField(max_length=255)
    attribute_type = models.CharField(max_length=1, choices=ATTRIBUTE_TYPES)
    value_type = AttributeValueTypeField(max_length=20, choices=VALUE_TYPES,
                                  attribute_type_field='attribute_type')


    def __unicode__(self):
        return self.name


class EAVBase(models.Model):
    """The entity-attribute-value base model
    When you inherit from this model, you must define the following fields::
        entity = ForeignKey(ENTITY)
        attribute = ForeignKey(Attribute)
    where ENTITY is the entity model for the EAV model you're defining.

    The child class is required to define the attribute field because it allows
    you to filter the attribute choices by adding
    :code:`limit_choices_to={'attribute_type': x}` or
    :code:`limit_choices_to={'attribute_type__in': x}` to the argument list.
    """

    class Meta:
        abstract = True
        unique_together = ('entity', 'attribute')


    value = EAVValueField(max_length=255, attribute_field='attribute')


    def __unicode__(self):
        kv_formats = {
            ATTRIBUTE_INFORMATIONAL: u'{0} = {1}',
            ATTRIBUTE_OPTION: u'option {0} {1}',
            ATTRIBUTE_STATEMENT: u'{0} {1}',
        }

        if self.attribute.value_type == 'string':
            add_quotes = not is_hex_byte_sequence(self.value)
        elif self.attribute.value_type == 'text':
            add_quotes = True
        else:
            add_quotes = False

        value = (u'"{0}"' if add_quotes else u'{0}').format(self.value)

        return (kv_formats[self.attribute.attribute_type]
                .format(self.attribute.name, value))
<commit_msg>A bit of integration<commit_after>from django.core.exceptions import ValidationError
from django.db import models

from cyder.base.eav import validators
from cyder.base.eav.constants import (ATTRIBUTE_TYPES, ATTRIBUTE_INFORMATIONAL,
                                      ATTRIBUTE_OPTION, ATTRIBUTE_STATEMENT)
from cyder.base.eav.fields import AttributeValueTypeField, EAVValueField
from cyder.base.eav.utils import is_hex_byte_sequence
from cyder.base.eav.validators import VALUE_TYPES
from cyder.base.mixins import ObjectUrlMixin


class Attribute(models.Model):
    class Meta:
        db_table = 'attribute'


    name = models.CharField(max_length=255)
    attribute_type = models.CharField(max_length=1, choices=ATTRIBUTE_TYPES)
    value_type = AttributeValueTypeField(max_length=20, choices=VALUE_TYPES,
                                  attribute_type_field='attribute_type')


    def __unicode__(self):
        return self.name


class EAVBase(models.Model, ObjectUrlMixin):
    """The entity-attribute-value base model
    When you inherit from this model, you must define the following fields::
        entity = ForeignKey(ENTITY)
        attribute = ForeignKey(Attribute)
    where ENTITY is the entity model for the EAV model you're defining.

    The child class is required to define the attribute field because it allows
    you to filter the attribute choices by adding
    :code:`limit_choices_to={'attribute_type': x}` or
    :code:`limit_choices_to={'attribute_type__in': x}` to the argument list.
    """

    class Meta:
        abstract = True
        unique_together = ('entity', 'attribute')


    value = EAVValueField(max_length=255, attribute_field='attribute')


    def __unicode__(self):
        kv_formats = {
            ATTRIBUTE_INFORMATIONAL: u'{0} = {1}',
            ATTRIBUTE_OPTION: u'option {0} {1}',
            ATTRIBUTE_STATEMENT: u'{0} {1}',
        }

        if self.attribute.value_type == 'string':
            add_quotes = not is_hex_byte_sequence(self.value)
        elif self.attribute.value_type == 'text':
            add_quotes = True
        else:
            add_quotes = False

        value = (u'"{0}"' if add_quotes else u'{0}').format(self.value)

        return (kv_formats[self.attribute.attribute_type]
                .format(self.attribute.name, value))


    def details(self):
        """For tables."""
        data = super(EAVBase, self).details()
        data['data'] = [
            ('Attribute', 'attribute__name', self.attribute),
            ('Value', 'value', self.value),
        ]
        return data

{'fraction_non_alphanumeric': 0.09371121224658668, 'fraction_numerical': 0.00703351261894911, 'mean_word_length': 2.9086499595796282, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84291', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1414, 'n_words': 348}