<commit_before>'''
Created on Sep 9, 2013

@author: <NAME>
'''

from sqlalchemy.orm import class_mapper, joinedload
from sqlalchemy import desc, asc

def filter_from_url_arg(model_cls, query, arg):
    '''
    Parse filter URL argument ``arg`` and apply to ``query``

    Example: 'column1<=value,column2==value' -> query.filter(Model.column1 <= value, Model.column2 == value)
    '''

    fields = arg.split(',')
    mapper = class_mapper(model_cls)

    exprs = []
    joins = []
    for expr in fields:
        e_mapper = mapper
        e_model_cls = model_cls

        operator = None
        method = None
        for op, m in operator_to_method.items():
            if op in expr:
                operator = op
                method = m

        if operator is None:
            raise Exception('No operator in expression {0}.'.format(expr))

        (column_names, value) = expr.split(operator)

        column_names = column_names.split('__')
        value = value.strip()

        for column_name in column_names:
            if column_name in e_mapper.relationships:
                joins.append(column_name)
                e_model_cls = e_mapper.attrs[column_name].mapper.class_
                e_mapper = class_mapper(e_model_cls)

        if hasattr(e_model_cls, column_name):
            column = getattr(e_model_cls, column_name)
            exprs.append(getattr(column, method)(value))
        else:
            raise Exception('Invalid property {0} in class {1}.'.format(column_name, e_model_cls))


    return query.join(*joins).filter(*exprs)

operator_to_method = {<commit_msg>Operator fix.<commit_after>'''
Created on Sep 9, 2013

@author: <NAME>
'''

from sqlalchemy.orm import class_mapper, joinedload
from sqlalchemy import desc, asc

def filter_from_url_arg(model_cls, query, arg):
    '''
    Parse filter URL argument ``arg`` and apply to ``query``

    Example: 'column1<=value,column2==value' -> query.filter(Model.column1 <= value, Model.column2 == value)
    '''

    fields = arg.split(',')
    mapper = class_mapper(model_cls)

    exprs = []
    joins = []
    for expr in fields:
        if expr == "":
            continue

        e_mapper = mapper
        e_model_cls = model_cls

        operator = None
        method = None
        for op, m in operator_to_method.items():
            if op in expr:
                operator = op
                method = m

        if operator is None:
            raise Exception('No operator in expression "{0}".'.format(expr))

        (column_names, value) = expr.split(operator)

        column_names = column_names.split('__')
        value = value.strip()

        for column_name in column_names:
            if column_name in e_mapper.relationships:
                joins.append(column_name)
                e_model_cls = e_mapper.attrs[column_name].mapper.class_
                e_mapper = class_mapper(e_model_cls)

        if hasattr(e_model_cls, column_name):
            column = getattr(e_model_cls, column_name)
            exprs.append(getattr(column, method)(value))
        else:
            raise Exception('Invalid property {0} in class {1}.'.format(column_name, e_model_cls))


    return query.join(*joins).filter(*exprs)

operator_to_method = {
{'fraction_non_alphanumeric': 0.10600924499229585, 'fraction_numerical': 0.007395993836671803, 'mean_word_length': 2.3637305699481863, 'pattern_counts': {'":': 1, '<': 9, '<?xml version=': 0, '>': 7, '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': '4530118', 'n_tokens_mistral': 1017, 'n_tokens_neox': 967, 'n_words': 240}