Exemple #1
0
def filter_pandas(df, filters):
    r"""
    Filter a GeoPandas DataFrame, return a new filtered DataFrame.
    Currently all filters are joined by 'AND'
    TODO: Support for 'OR', parentheses?

    :param df: The DataFrame to filter
    :param filters: An array of (attribute, operator, value) arrays\
    for example [('city', 'in', ['Boston', 'New York']), ('id', '>', 10)]
    :return: A filtered DataFrame
    """
    for filter in filters:
        attribute = filter[0]
        operator = filter[1]
        values = filter[2]
        if operator.lower() == "in":
            df = df[df[attribute].isin(values)]
        elif operator.lower() == "not in":
            df = df[~df[attribute].isin(values)]
        elif operator.lower() in ["contains", "startswith", "endswith"]:
            str_func = getattr(df[attribute].str, operator.lower())
            df = df[str_func(r'{}'.format(values))]
        elif operator in ops.keys():
            df = df[ops[operator](df[attribute], values)]
    return df
Exemple #2
0
    def eval_binary_expr(self,op1, operator, op2):
        op1,op2 = op1, op2
        if(operator == 'Contains') :
           return op1.like('%'+op2+'%')
        if operator.lower() == 'in':
            l = [s for s in re.split("[,|;\W]+", op2)]
            return op1.in_(l)
        if operator.lower() == 'checked':
            operator = '='
            if '-1' in op2:
                return None
            return op1.in_(op2)

        return self.get_operator_fn(operator)(op1, op2)
Exemple #3
0
    def _process_domain(self, result, domain):
        if domain and len(domain) == 3:
            field, operator, value = domain

            compute_operator = {
                'ilike': lambda field, value: str(value).lower() in str(field).lower(),
                'like': lambda field, value: str(value) in str(field),
                'not ilike': lambda field, value: str(value).lower() not in str(field).lower(),
                'not like': lambda field, value: str(value) not in str(field),
                '=': lambda field, value: value == field,
                '!=': lambda field, value: value != field,
                '>': lambda field, value: value < field,
                '<': lambda field, value: value > field,
                '>=': lambda field, value: value <= field,
                '<=': lambda field, value: value >= field,
            }

            operator = operator.lower()
            if field in result:
                if operator in compute_operator.iterkeys():
                    return compute_operator.get(operator)(result.get(field), value)

                raise osv.except_osv(u'错误', u'未添加的domain条件%s' % domain)

        raise osv.except_osv(u'错误', u'不可识别的domain条件,请检查domain"%s"是否正确' % domain)
Exemple #4
0
 def eval_binary_expr(self,op1, operator, op2):
     op = operator.lower()
     if op in ['is', 'is not'] and isinstance(op2,str) and op2.lower() == 'null':
         op2 = None
     try : 
         return self.get_operator_fn(op)(op1, op2)
     except (NameError,KeyError): 
         return not_(self.get_operator_fn(op.replace('not','').replace(' ',''))(op1, op2))
Exemple #5
0
 def eval_binary_expr(self,op1, operator, op2):
     op = operator.lower()
     if op in ['is null', 'is not null']:
         op = op.replace('null','').replace(' ','')
         op2 = None
     try : 
         return self.get_operator_fn(op)(op1, op2)
     except (NameError,KeyError): 
         return not_(self.get_operator_fn(op.replace('not','').replace(' ',''))(op1, op2))
Exemple #6
0
 def eval_binary_expr(self,op1, operator, op2):
     op = operator.lower()
     if op in ['is null', 'is not null']:
         op = op.replace('null','').replace(' ','')
         op2 = None
     try : 
         return self.get_operator_fn(op)(op1, op2)
     except (NameError,KeyError): 
         return not_(self.get_operator_fn(op.replace('not','').replace(' ',''))(op1, op2))
Exemple #7
0
 def eval_binary_expr(self, op1, operator, op2):
     op = operator.lower()
     if op in ['is', 'is not'] and isinstance(
             op2, str) and op2.lower() == 'null':
         op2 = None
     try:
         return self.get_operator_fn(op)(op1, op2)
     except (NameError, KeyError):
         return not_(
             self.get_operator_fn(op.replace('not', '').replace(' ',
                                                                ''))(op1,
                                                                     op2))
Exemple #8
0
def filter_pandas(df, filters):
    r"""
    Filter a GeoPandas DataFrame, return a new filtered DataFrame.
    Currently all filters are joined by 'AND'
    TODO: Support for 'OR', parentheses?
    :param df: The DataFrame to filter
    :param filters: An array of (attribute, operator, value) arrays\
    for example [('city', 'in', ['Boston', 'New York']), ('id', '>', 10)]
    :return: A filtered DataFrame
    """
    for filter in filters:
        attribute = filter[0]
        operator = filter[1]
        values = filter[2]
        if operator.lower() == "in":
            df = df[df[attribute].isin(values)]
        elif operator.lower() == "not in":
            df = df[~df[attribute].isin(values)]
        elif operator.lower() == "contains":
            df = df[df[attribute].str.contains(r'{}'.format(values))]
        elif operator in ops.keys():
            df = df[ops[operator](df[attribute], values)]
    return df
Exemple #9
0
def main():
    fs = cgi.FieldStorage()
    aggr=fs.getvalue("aggr")
    operator=fs.getvalue("operator")
    dates=fs.getvalue("dates")
    if ((not aggr) or (not operator) or (not dates)):
        error.errhttp("406")
        return
    urlaggr=getaggrurl(aggr)
    if(urlaggr=="404"):
        error.errhttp("404")
        return
    aggr=aggr.lower()
    operator=operator.lower()
    dates=dates.lower()
    req=urllib2.Request(url=urlaggr)
    req.add_header('Accept', 'application/xml, text/turtle, text/csv, application/json')
    response = urllib2.urlopen(req)
    restype= response.info().gettype()
    resource=response.read()
    response.close()
    if(restype=="application/xml"):
        meta=trasforma.locationfromxml(resource,loclist)
    elif(restype=="text/turtle"):
        meta=trasforma.locationfromturtle(resource,loclist)
    elif(restype=="text/csv"):
        meta=trasforma.locationfromcsv(resource,loclist)
    elif(restype=="application/json"):
        meta=trasforma.locationfromjson(resource,loclist)
    else:
        error.errhttp("406")
        return
    finallist=getopened(dates, operator,loclist)
    if(isinstance(finallist, ( int, long ))):
        error.errcode(str(finallist))
        return
    trasforma.formatresult(os.environ["HTTP_ACCEPT"], finallist, meta)
Exemple #10
0
def oper(op):
    operator, value = op.split(" ")
    if operator.lower() == "inc":
        return int(value)
    else:
        return int(value) * -1
        operation_sequence = str(
            input("Enter the operation sequence separated by comma"))
        operators = operation_sequence.split(",")
        output = None
        comparisons_sum = 0
        skip = False
        # if len(list(set(operators)))==1:
        #     query_terms.sort(key=lambda x: len(inverted_index.get(x)[1]))
        #     print("Sorted",query_terms)

        # for each operator in the given list we iterate through them to perform the opertions and obtain final result
        for index, operator in enumerate(operators):
            pos_lists = []
            NOT = False

            if "and" in operator.lower():
                if index == 0:
                    pos_list_1 = inverted_index.get(query_terms[index])
                    if not pos_list_1:
                        print(
                            "Term not found: {}\nHence, operation has failed".
                            format(query_terms[index]))
                        skip = True
                        break
                    else:
                        pos_list_1 = pos_list_1[1]
                else:
                    # to use the output from applying previous operation
                    pos_list_1 = output
                pos_list_2 = inverted_index.get(query_terms[index + 1])
                # covers AND NOT