def generate_filter_expression(filter_key: str, filter_cond: str, filter_value: str) -> dict: filter_attr = Attr(filter_key) if filter_cond == "eq": return {"FilterExpression": filter_attr.eq(filter_value)} elif filter_cond == "ne": return {"FilterExpression": filter_attr.eq(filter_value)} elif filter_cond == "gt": return {"FilterExpression": filter_attr.gt(filter_value)} elif filter_cond == "ge": return {"FilterExpression": filter_attr.gte(filter_value)} elif filter_cond == "lt": return {"FilterExpression": filter_attr.lt(filter_value)} elif filter_cond == "le": return {"FilterExpression": filter_attr.lte(filter_value)} elif filter_cond == "begins_with": return {"FilterExpression": filter_attr.begins_with(filter_value)} elif filter_cond == "between": return {"FilterExpression": filter_attr.between(*[filter_value])} elif filter_cond == "contains": return {"FilterExpression": filter_attr.contains(filter_value)} else: raise AttributeError("filter condition missing")
def test_build_expression_gte(self): a = Attr('myattr') self.assert_condition_expression_build(a.gte('foo'), '#n0 >= :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_build_expression_gte(self): a = Attr('myattr') self.assert_condition_expression_build( a.gte('foo'), '#n0 >= :v0', {'#n0': 'myattr'}, {':v0': 'foo'})
def test_build_expression_gte(self): a = Attr("myattr") self.assert_condition_expression_build(a.gte("foo"), "#n0 >= :v0", {"#n0": "myattr"}, {":v0": "foo"})
def doquery(self, queryspec): db = boto3.resource('dynamodb') table = db.Table(queryspec.DataType) data = None if len(queryspec.Filters) == 0: response = table.scan() data = response['Items'] while 'LastEvaluatedKey' in response: response = table.scan( ExclusiveStartKey=response['LastEvaluatedKey']) data.extend(response['Items']) else: for prop in queryspec.Filters: if data is None and queryspec.Filters[prop][2]: # queryable key = Key(prop) if queryspec.Filters[prop][0] == '=': expr = key.eq(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<': expr = key.lt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>': expr = key.gt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<=': expr = key.lte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>=': expr = key.gte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == 'btw': expr = key.between(queryspec.Filters[prop][1][0], queryspec.Filters[prop][1][0]) response = table.query(KeyConditionExpression=expr) data = response['Items'] else: # queryable = False if data is None: attr = Attr(prop) if queryspec.Filters[prop][0] == '=': expr = attr.eq(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<': expr = attr.lt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>': expr = attr.gt(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '<=': expr = attr.lte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == '>=': expr = attr.gte(queryspec.Filters[prop][1]) elif queryspec.Filters[prop][0] == 'btw': expr = attr.between(queryspec.Filters[prop][1][0], queryspec.Filters[prop][1][0]) response = table.scan(FilterExpression=expr) data = response['Items'] while 'LastEvaluatedKey' in response: response = table.scan( ExclusiveStartKey=response['LastEvaluatedKey']) data.extend(response['Items']) else: pass # TODO: Secondary value if data is None or len(data) == 0 or len(queryspec.Sorts) == 0: return data sortproperty = next(iter(queryspec.Sorts)) sorteddata = sorted(data, key=lambda item: item[sortproperty]) return sorteddata