<commit_before>from sqlalchemy import Table, MetaData, Column, ForeignKey, ForeignKeyConstraint
from sqlalchemy import String, Unicode, Integer, Float, Boolean, Date, Interval, PickleType
import datetime
import pytz
import re

def int_hex(x):
    return int(x, 16)
    
def date_yyyymmdd(x):
    return datetime.datetime.strptime(x, '%Y%m%d').date()

def timedelta_hms(x):
    (hours, minutes, seconds) = map(int, re.split(':', x))
    return datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)

class Field(object):
    """Represents an attribute to store in the database. An Entity object is
    associated with an array of Fields in Entity.fields. Like SQLAlchemy's 
    Column() but with some extra fields: 
    
    - name: column name
    - column_type: SQLAlchemy column type, such as String, Integer, etc.
    - foreign_key: name of foreign key (the name of a field in the table, like
      tablename.field, not a call to ForeignKey() itself) (default None)
    - primary_key: whether this field is a primary key or not (default False)
    - cast: a function to call on the field when instantiating the Entity, to
      convert its type to the appropriate value (default None, no cast)
    - mandatory: is this field's presence mandatory in GTFS? (default False)
    - default: if this field is omitted or a blank string, what is the default 
      value? (default None)
    """
    def __init__(self, name, column_type, foreign_key=None, primary_key=False,
                 cast=None, mandatory=False, default=None):
        self.name = name
        self.column_type = column_type
        self.foreign_key = foreign_key
        self.primary_key = primary_key
        self.cast = cast
        self.mandatory = mandatory
        self.default = default

class EntityMissingFieldError(Exception):
    """Raised when a subclass of Entity is instantiated without mandatory fields."""
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class EntityBadFieldError(Exception):
    """Raised when a subclass of Entity is instantiated with improper values."""
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class Entity(object):
    """Represents the base class of objects to be stored by SQLAlchemy. 
    An Entity is associated with an array of Field objects. Entities are 
    unmapped, mapping is done separately via the "classical" method 
    (declarative being in my opinion a bigger pain to implement here).
    """
    
    metadata = MetaData()
    
    table_name = None
    gtfs_required = None
    fields = []
    
    def __init__(self, **kwargs):
        for field in self.fields:
            if field.name in kwargs:
                field_value = kwargs[field.name]
                if field_value == '':
                    setattr(self, field.name, field.default)
                elif field_value is not None and field.cast is not None:
                    setattr(self, field.name, field.cast(field_value))
                else:
                    setattr(self, field.name, field_value)
            else:
                setattr(self, field.name, field.default)
            
        self.check_mandatory_fields()
    
    def check_mandatory_fields(self):
        for field in self.fields:
            if field.mandatory and getattr(self, field.name, None) is None:
                raise EntityMissingFieldError('Missing field %s in %s' % \
                                              (field.name, self.__class__.__name__))

# Subclasses of Entity are below; these represent the data in GTFS. 
# The attributes of each class are mapped in by create_and_map tables in
# map_entities.py:
# 
#  1. each Field in fields is mapped
#  2. cross-references are mapped in with the properties parameter of mapper()

class Agency(Entity):
    """A transit agency."""
    
    table_name = 'agency'
    gtfs_required = True
    
    # Note that agency_id is not mandatory according to GTFS. We assume here 
    # that agency_id and agency_name together are enough for a primary key - 
    # hope people aren't both omitting agency_ids and duping agency_names. 
    
    fields = [
              Field('feed_name', String, cast=str, primary_key=True),
              Field('agency_id', String, cast=str, primary_key=True),
              Field('agency_name', Unicode, cast=unicode, mandatory=True),
              Field('agency_url', String, cast=str, mandatory=True),
              Field('agency_timezone', PickleType, cast=pytz.timezone, mandatory=True),
              Field('agency_lang', String, cast=str),
              Field('agency_phone', String, cast=str),
              Field('agency_fare_url', String, cast=str),
             ]
    <commit_msg>Set a default for agency_id in the agency table<commit_after>from sqlalchemy import Table, MetaData, Column, ForeignKey, ForeignKeyConstraint
from sqlalchemy import String, Unicode, Integer, Float, Boolean, Date, Interval, PickleType
import datetime
import pytz
import re

def int_hex(x):
    return int(x, 16)
    
def date_yyyymmdd(x):
    return datetime.datetime.strptime(x, '%Y%m%d').date()

def timedelta_hms(x):
    (hours, minutes, seconds) = map(int, re.split(':', x))
    return datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)

class Field(object):
    """Represents an attribute to store in the database. An Entity object is
    associated with an array of Fields in Entity.fields. Like SQLAlchemy's 
    Column() but with some extra fields: 
    
    - name: column name
    - column_type: SQLAlchemy column type, such as String, Integer, etc.
    - foreign_key: name of foreign key (the name of a field in the table, like
      tablename.field, not a call to ForeignKey() itself) (default None)
    - primary_key: whether this field is a primary key or not (default False)
    - cast: a function to call on the field when instantiating the Entity, to
      convert its type to the appropriate value (default None, no cast)
    - mandatory: is this field's presence mandatory in GTFS? (default False)
    - default: if this field is omitted or a blank string, what is the default 
      value? (default None)
    """
    def __init__(self, name, column_type, foreign_key=None, primary_key=False,
                 cast=None, mandatory=False, default=None):
        self.name = name
        self.column_type = column_type
        self.foreign_key = foreign_key
        self.primary_key = primary_key
        self.cast = cast
        self.mandatory = mandatory
        self.default = default

class EntityMissingFieldError(Exception):
    """Raised when a subclass of Entity is instantiated without mandatory fields."""
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class EntityBadFieldError(Exception):
    """Raised when a subclass of Entity is instantiated with improper values."""
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class Entity(object):
    """Represents the base class of objects to be stored by SQLAlchemy. 
    An Entity is associated with an array of Field objects. Entities are 
    unmapped, mapping is done separately via the "classical" method 
    (declarative being in my opinion a bigger pain to implement here).
    """
    
    metadata = MetaData()
    
    table_name = None
    gtfs_required = None
    fields = []
    
    def __init__(self, **kwargs):
        for field in self.fields:
            if field.name in kwargs:
                field_value = kwargs[field.name]
                if field_value == '':
                    setattr(self, field.name, field.default)
                elif field_value is not None and field.cast is not None:
                    setattr(self, field.name, field.cast(field_value))
                else:
                    setattr(self, field.name, field_value)
            else:
                setattr(self, field.name, field.default)
            
        self.check_mandatory_fields()
    
    def check_mandatory_fields(self):
        for field in self.fields:
            if field.mandatory and getattr(self, field.name, None) is None:
                raise EntityMissingFieldError('Missing field %s in %s' % \
                                              (field.name, self.__class__.__name__))

# Subclasses of Entity are below; these represent the data in GTFS. 
# The attributes of each class are mapped in by create_and_map tables in
# map_entities.py:
# 
#  1. each Field in fields is mapped
#  2. cross-references are mapped in with the properties parameter of mapper()

class Agency(Entity):
    """A transit agency."""
    
    table_name = 'agency'
    gtfs_required = True
    
    # Note that agency_id is not mandatory according to GTFS. We assume here 
    # that agency_id and agency_name together are enough for a primary key - 
    # hope people aren't both omitting agency_ids and duping agency_names. 
    
    fields = [
              Field('feed_name', String, cast=str, primary_key=True),
              Field('agency_id', String, cast=str, primary_key=True, default="None"),
              Field('agency_name', Unicode, cast=unicode, mandatory=True),
              Field('agency_url', String, cast=str, mandatory=True),
              Field('agency_timezone', PickleType, cast=pytz.timezone, mandatory=True),
              Field('agency_lang', String, cast=str),
              Field('agency_phone', String, cast=str),
              Field('agency_fare_url', String, cast=str),
             ]
    
{'fraction_non_alphanumeric': 0.08495648570244509, 'fraction_numerical': 0.0008288437629506838, 'mean_word_length': 2.6207801950487624, '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': '3288441', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2602, 'n_words': 947}