<commit_before>from abc import ABCMeta
from abc import abstractmethod

from pyrecord._generic_utilities import get_duplicated_iterable_items
from pyrecord._generic_utilities import is_valid_python_identifier


__all__ = [
    "Record",
    "RecordInitializationError",
    "RecordException",
    "RecordTypeError",
    ]


class Record(object):
    
    __metaclass__ = ABCMeta
    
    field_names = ()
    
    _default_values_by_field_name = {}
    
    @abstractmethod
    def __init__(self):
        super(Record, self).__init__()
    
    @classmethod
    def create_type(cls, type_name, *field_names, **default_values_by_field_name):
        _enforce_type_name_validity(type_name)
        
        all_field_names = cls.field_names + \
            field_names + \
            tuple(default_values_by_field_name.keys())
        _enforce_field_name_uniqueness(all_field_names)
        _enforce_field_name_validity(all_field_names)
        
        record_type = type(type_name, (cls, ), {})
        record_type.field_names = all_field_names
        record_type._default_values_by_field_name = dict(
             cls._default_values_by_field_name,
             **default_values_by_field_name
             )
        return record_type


#{ Exceptions


class RecordException(Exception):
    pass


class RecordTypeError(RecordException):
    pass


class RecordInitializationError(RecordException):
    pass


#{ Validators


def _enforce_type_name_validity(type_name):
    if not is_valid_python_identifier(type_name):
        raise RecordTypeError(
            "{} is not a valid identifier for a record type".format(<commit_msg>Restricted validation of field names to the ones defined in subtypes only<commit_after>from abc import ABCMeta
from abc import abstractmethod

from pyrecord._generic_utilities import get_duplicated_iterable_items
from pyrecord._generic_utilities import is_valid_python_identifier


__all__ = [
    "Record",
    "RecordInitializationError",
    "RecordException",
    "RecordTypeError",
    ]


class Record(object):
    
    __metaclass__ = ABCMeta
    
    field_names = ()
    
    _default_values_by_field_name = {}
    
    @abstractmethod
    def __init__(self):
        super(Record, self).__init__()
    
    @classmethod
    def create_type(cls, type_name, *field_names, **default_values_by_field_name):
        _enforce_type_name_validity(type_name)
        
        # Validate field names
        new_field_names = field_names + \
            tuple(default_values_by_field_name.keys())
        all_field_names = cls.field_names + new_field_names
        _enforce_field_name_uniqueness(all_field_names)
        _enforce_field_name_validity(new_field_names)
        
        # Create the record type
        record_type = type(type_name, (cls, ), {})
        record_type.field_names = all_field_names
        record_type._default_values_by_field_name = dict(
             cls._default_values_by_field_name,
             **default_values_by_field_name
             )
        return record_type


#{ Exceptions


class RecordException(Exception):
    pass


class RecordTypeError(RecordException):
    pass


class RecordInitializationError(RecordException):
    pass


#{ Validators


def _enforce_type_name_validity(type_name):
    if not is_valid_python_identifier(type_name):
        raise RecordTypeError(
            "{} is not a valid identifier for a record type".format(
{'fraction_non_alphanumeric': 0.06666666666666667, 'fraction_numerical': 0.0, 'mean_word_length': 3.161800486618005, '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': '1030206', 'n_tokens_mistral': 1028, 'n_tokens_neox': 1021, 'n_words': 185}