@@ -45,15 +45,13 @@ class Query(object):
4545 :param dataset: The namespace to which to restrict results.
4646 """
4747
48- # NOTE: Order is very important here since operators that end with
49- # '<=' and '>=' also end with '='.
50- OPERATORS = (
51- ('<=' , datastore_pb .PropertyFilter .LESS_THAN_OR_EQUAL ),
52- ('>=' , datastore_pb .PropertyFilter .GREATER_THAN_OR_EQUAL ),
53- ('<' , datastore_pb .PropertyFilter .LESS_THAN ),
54- ('>' , datastore_pb .PropertyFilter .GREATER_THAN ),
55- ('=' , datastore_pb .PropertyFilter .EQUAL ),
56- )
48+ OPERATORS = {
49+ '<=' : datastore_pb .PropertyFilter .LESS_THAN_OR_EQUAL ,
50+ '>=' : datastore_pb .PropertyFilter .GREATER_THAN_OR_EQUAL ,
51+ '<' : datastore_pb .PropertyFilter .LESS_THAN ,
52+ '>' : datastore_pb .PropertyFilter .GREATER_THAN ,
53+ '=' : datastore_pb .PropertyFilter .EQUAL ,
54+ }
5755 """Mapping of operator strings and their protobuf equivalents."""
5856
5957 def __init__ (self , kind = None , dataset = None , namespace = None ):
@@ -134,14 +132,16 @@ def filter(self, expression, value):
134132 property_name , operator = None , None
135133 expression = expression .strip ()
136134
137- for operator_string , pb_op_enum in self .OPERATORS :
138- if expression .endswith (operator_string ):
139- operator = pb_op_enum
140- property_name = expression [0 :- len (operator_string )].strip ()
141- # After one match, we move on since >= and <= conflict with =.
142- break
135+ # Use None to split on *any* whitespace.
136+ expr_pieces = expression .rsplit (None , 1 )
137+ if len (expr_pieces ) == 2 :
138+ property_name , operator = expr_pieces
139+ property_name = property_name .strip ()
143140
144- if not operator or not property_name :
141+ # If no whitespace in `expression`, `operator` will be `None` and
142+ # self.OPERATORS[None] will be `None` as well.
143+ pb_op_enum = self .OPERATORS .get (operator )
144+ if pb_op_enum is None :
145145 raise ValueError ('Invalid expression: "%s"' % expression )
146146
147147 # Build a composite filter AND'd together.
@@ -151,7 +151,7 @@ def filter(self, expression, value):
151151 # Add the specific filter
152152 property_filter = composite_filter .filter .add ().property_filter
153153 property_filter .property .name = property_name
154- property_filter .operator = operator
154+ property_filter .operator = pb_op_enum
155155
156156 # Set the value to filter on based on the type.
157157 helpers ._set_protobuf_value (property_filter .value , value )
0 commit comments