<commit_before>import abc


class FieldFactory(object):

    def __new__(cls, field, *args, **kwargs):
        field_type = type(field)
        if field_type is dict:
            kwargs.update(alias=field.keys()[0])
            field = field.values()[0]
            field_type = type(field)

        if field_type is str:
            return SimpleField(field, **kwargs)
        elif isinstance(field, Field):
            for key, value in kwargs.items():
                setattr(field, key, value)
            return field
        return None


class Field(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, field=None, table=None, alias=None):
        self.field = field
        self.name = None
        self.table = table
        self.alias = alias
        self.auto_alias = None
        self.ignore = False
        self.auto = False

    def get_sql(self):
        """
        Gets the FROM sql part for a field
        Ex: field_name AS alias
        :return: :rtype: str
        """
        alias = self.get_alias()
        if alias:
            return '{0} AS {1}'.format(self.get_select_sql(), alias)

        return self.get_identifier()

    def get_name(self):
        alias = self.get_alias()
        if alias:
            return alias
        return self.name

    def get_alias(self):
        alias = None
        if self.alias:
            alias = self.alias
        elif self.auto_alias:
            alias = self.auto_alias

        if self.table and self.table.prefix_fields:
            field_prefix = self.table.get_field_prefix()
            if alias:
                alias = '{0}__{1}'.format(field_prefix, alias)
            else:
                alias = '{0}__{1}'.format(field_prefix, self.name)

        return alias

    def get_identifier(self):
        """
        Gets the name for the field of how it should
        be referenced within a query. It will be
        prefixed with the table name or table alias
        :return: :rtype: str
        """
        alias = self.get_alias()
        if alias:
            return alias
        return self.get_select_sql()

    def get_select_sql(self):
        if self.table:
            return '{0}.{1}'.format(self.table.get_identifier(), self.name)
        return '{0}'.format(self.name)

    def before_add(self):
        pass

    def set_table(self, table):
        self.table = table


class SimpleField(Field):
    def __init__(self, field, table=None, alias=None):
        super(SimpleField, self).__init__(field, table, alias)
        self.name = field


class AggregateField(Field):
    function_name = None

    def __init__(self, field=None, table=None, alias=None, over=None):
        super(AggregateField, self).__init__(field, table, alias)
        self.field = FieldFactory(field)

        self.name = self.function_name
        self.over = over

        field_name = None
        if self.field and type(self.field.field) is str:
            field_name = self.field.field
            if field_name == '*':
                field_name = 'all'

        if field_name:
            self.auto_alias = '{0}_{1}'.format(field_name, self.name.lower())
        else:
            self.auto_alias = self.name.lower()

    def get_select_sql(self):
        return '{0}({1}){2}'.format(
            self.name.upper(),
            self.get_field_identifier(),
            self.get_over(),
        )

    def get_field_identifier(self):
        return self.field.get_identifier()

    def get_over(self):
        if self.over:
            return ' {0}'.format(self.over.get_sql())
        return ''

    def set_table(self, table):
        super(AggregateField, self).set_table(table)
        if self.field and self.field.table is None:
            self.field.table = self.table


class CountField(AggregateField):
    function_name = 'Count'


class AvgField(AggregateField):
    function_name = 'Avg'


class MaxField(AggregateField):
    function_name = 'Max'


class MinField(AggregateField):
    function_name = 'Min'


class StdDevField(AggregateField):
    function_name = 'StdDev'


class NumStdDevField(AggregateField):
    function_name = 'num_stddev'

    def get_select_sql(self):
        return '(({0} - (AVG({0}){1})) / (STDDEV({0}){1}))'.format(<commit_msg>* Cast the count as a float so that it is treated the same way in calculations<commit_after>import abc


class FieldFactory(object):

    def __new__(cls, field, *args, **kwargs):
        field_type = type(field)
        if field_type is dict:
            kwargs.update(alias=field.keys()[0])
            field = field.values()[0]
            field_type = type(field)

        if field_type is str:
            return SimpleField(field, **kwargs)
        elif isinstance(field, Field):
            for key, value in kwargs.items():
                setattr(field, key, value)
            return field
        return None


class Field(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self, field=None, table=None, alias=None):
        self.field = field
        self.name = None
        self.table = table
        self.alias = alias
        self.auto_alias = None
        self.ignore = False
        self.auto = False

    def get_sql(self):
        """
        Gets the FROM sql part for a field
        Ex: field_name AS alias
        :return: :rtype: str
        """
        alias = self.get_alias()
        if alias:
            return '{0} AS {1}'.format(self.get_select_sql(), alias)

        return self.get_identifier()

    def get_name(self):
        alias = self.get_alias()
        if alias:
            return alias
        return self.name

    def get_alias(self):
        alias = None
        if self.alias:
            alias = self.alias
        elif self.auto_alias:
            alias = self.auto_alias

        if self.table and self.table.prefix_fields:
            field_prefix = self.table.get_field_prefix()
            if alias:
                alias = '{0}__{1}'.format(field_prefix, alias)
            else:
                alias = '{0}__{1}'.format(field_prefix, self.name)

        return alias

    def get_identifier(self):
        """
        Gets the name for the field of how it should
        be referenced within a query. It will be
        prefixed with the table name or table alias
        :return: :rtype: str
        """
        alias = self.get_alias()
        if alias:
            return alias
        return self.get_select_sql()

    def get_select_sql(self):
        if self.table:
            return '{0}.{1}'.format(self.table.get_identifier(), self.name)
        return '{0}'.format(self.name)

    def before_add(self):
        pass

    def set_table(self, table):
        self.table = table


class SimpleField(Field):
    def __init__(self, field, table=None, alias=None):
        super(SimpleField, self).__init__(field, table, alias)
        self.name = field


class AggregateField(Field):
    function_name = None

    def __init__(self, field=None, table=None, alias=None, over=None):
        super(AggregateField, self).__init__(field, table, alias)
        self.field = FieldFactory(field)

        self.name = self.function_name
        self.over = over

        field_name = None
        if self.field and type(self.field.field) is str:
            field_name = self.field.field
            if field_name == '*':
                field_name = 'all'

        if field_name:
            self.auto_alias = '{0}_{1}'.format(field_name, self.name.lower())
        else:
            self.auto_alias = self.name.lower()

    def get_select_sql(self):
        return '{0}({1}){2}'.format(
            self.name.upper(),
            self.get_field_identifier(),
            self.get_over(),
        )

    def get_field_identifier(self):
        return self.field.get_identifier()

    def get_over(self):
        if self.over:
            return ' {0}'.format(self.over.get_sql())
        return ''

    def set_table(self, table):
        super(AggregateField, self).set_table(table)
        if self.field and self.field.table is None:
            self.field.table = self.table


class CountField(AggregateField):
    function_name = 'Count'

    def get_select_sql(self):
        sql = super(CountField, self).get_select_sql()
        return 'CAST({0} AS FLOAT)'.format(sql)

class AvgField(AggregateField):
    function_name = 'Avg'


class MaxField(AggregateField):
    function_name = 'Max'


class MinField(AggregateField):
    function_name = 'Min'


class StdDevField(AggregateField):
    function_name = 'StdDev'


class NumStdDevField(AggregateField):
    function_name = 'num_stddev'

    def get_select_sql(self):
        return '(({0} - (AVG({0}){1})) / (STDDEV({0}){1}))'.format(
{'fraction_non_alphanumeric': 0.10996326905417815, 'fraction_numerical': 0.005165289256198347, 'mean_word_length': 2.066877859908483, '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': '6575513', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2575, 'n_words': 632}