<commit_before>""" Helpers for guessing our way into queries against the database
"""
import logging
import re

import six
import sqlalchemy as sa
import sqlalchemy_utils as sau

logger = logging.getLogger('tilezilla')

# [OP]
# 1. REGEX
COMPARATORS = ['eq', 'ne', 'le', 'lt', 'ge', 'gt', 'in', 'like']


# [KEY]
# 1. PARSE SYSTEM
def _convert_expression_operator(expr):
    """ Convert expression operators from '=' style to 'eq' style
    Args:
        expr (str): Expression to convert
    Returns:
        str: Converted expression
    """
    TO_CONVERT = {
        ' eq ': ['=', '=='],
        ' ne ': ['!=', '=!'],
        ' lt ': ['<'],
        ' le ': ['<=', '=<'],
        ' gt ': ['>'],
        ' ge ': ['>=', '=>']
    }
    for op, matches in six.iteritems(TO_CONVERT):
        for match in matches:
            if match in expr:
                return expr.replace(match, op)
    return expr


def _parse_expression_filter(expr):
    """ Convert a filter expression into [KEY], [OPERATOR], [VALUE...]

    Args:
        str: A filter expression

    Returns:
        tuple (str, str, str): The key, operator, and value or values

    Raises:
        KeyError: Raise if operator not in available comparators
            (:attr:`COMPARATORS`)
    """
    # Parse operator
    for comp in COMPARATORS:
        split = re.split('\s(?i)%s\s' % comp, expr)
        if len(split) == 2:
            return split[0].strip(), comp, split[1]
    raise KeyError('Could not find a supported comparator in expression')


# [VALUE]
# TYPE CAST SYSTEM
def convert_query_type(column, value):
    """ Return `value` converted to type expected by `table` for column `key`

    Args:
        column (sqlalchemy.orm.attributes.QueryableAttribute): A SQLAlchemy
            ORM attribute
        value (str): A value to convert

    Returns:
        type: `value`, converted to a different type
    """
    return sau.cast_if(value, type(column.type))


# [FUNCTION]
# The thing to write that uses above<commit_msg>Enable filter w/ date/datetime str<commit_after>""" Helpers for guessing our way into queries against the database
"""
import logging
import re

import arrow
import six
import sqlalchemy as sa
import sqlalchemy_utils as sau

logger = logging.getLogger('tilezilla')

# [OP]
# 1. REGEX
COMPARATORS = ['eq', 'ne', 'le', 'lt', 'ge', 'gt', 'in', 'like']


# [KEY]
# 1. PARSE SYSTEM
def _convert_expression_operator(expr):
    """ Convert expression operators from '=' style to 'eq' style
    Args:
        expr (str): Expression to convert
    Returns:
        str: Converted expression
    """
    TO_CONVERT = {
        ' eq ': ['=', '=='],
        ' ne ': ['!=', '=!'],
        ' lt ': ['<'],
        ' le ': ['<=', '=<'],
        ' gt ': ['>'],
        ' ge ': ['>=', '=>']
    }
    for op, matches in six.iteritems(TO_CONVERT):
        for match in matches:
            if match in expr:
                return expr.replace(match, op)
    return expr


def _parse_expression_filter(expr):
    """ Convert a filter expression into [KEY], [OPERATOR], [VALUE...]

    Args:
        str: A filter expression

    Returns:
        tuple (str, str, str): The key, operator, and value or values

    Raises:
        KeyError: Raise if operator not in available comparators
            (:attr:`COMPARATORS`)
    """
    # Parse operator
    for comp in COMPARATORS:
        split = re.split('\s(?i)%s\s' % comp, expr)
        if len(split) == 2:
            return split[0].strip(), comp, split[1]
    raise KeyError('Could not find a supported comparator in expression')


# [VALUE]
# TYPE CAST SYSTEM
def convert_query_type(column, value):
    """ Return `value` converted to type expected by `table` for column `key`

    Args:
        column (sqlalchemy.orm.attributes.QueryableAttribute): A SQLAlchemy
            ORM attribute
        value (str): A value to convert

    Returns:
        type: `value`, converted to a different type
    """
    if isinstance(column.type, (sa.sql.sqltypes.DateTime,
                                sa.sql.sqltypes.Date)):
        return arrow.get(value).datetime
    return sau.cast_if(value, type(column.type))


# [FUNCTION]
# The thing to write that uses above
{'fraction_non_alphanumeric': 0.13752081846300263, 'fraction_numerical': 0.0023792529145848203, 'mean_word_length': 2.5150501672240804, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4117832', 'n_tokens_mistral': 1307, 'n_tokens_neox': 1241, 'n_words': 413}