def apply_query(query, entities, _key=None):
 if (not isinstance(query, Query)):
  raise datastore_errors.BadArgumentError(('query argument must be a datastore_query.Query (%r)' % (query,)))
 if (not isinstance(entities, list)):
  raise datastore_errors.BadArgumentError(('entities argument must be a list (%r)' % (entities,)))
 key = (_key or (lambda x: x))
 filtered_results = filter((lambda r: query._key_filter(key(r))), entities)
 if (not query._order):
  if query._filter_predicate:
   return filter((lambda r: query._filter_predicate(key(r))), filtered_results)
  return filtered_results
 names = query._order._get_prop_names()
 if query._filter_predicate:
  names |= query._filter_predicate._get_prop_names()
 exists_filter = _PropertyExistsFilter(names)
 value_maps = []
 for result in filtered_results:
  value_map = _make_key_value_map(key(result), names)
  if (exists_filter._apply(value_map) and ((not query._filter_predicate) or query._filter_predicate._prune(value_map))):
   value_map['__result__'] = result
   value_maps.append(value_map)
 value_maps.sort(query._order._cmp)
 return [value_map['__result__'] for value_map in value_maps]

def apply_query ( query , entities , _key = None ) : if ( not isinstance ( query , Query ) ) : raise datastore_errors . BadArgumentError ( ( 'query argument must be a datastore_query.Query (%r)' % ( query , ) ) ) if ( not isinstance ( entities , list ) ) : raise datastore_errors . BadArgumentError ( ( 'entities argument must be a list (%r)' % ( entities , ) ) ) key = ( _key or ( lambda x : x ) ) filtered_results = filter ( ( lambda r : query . _key_filter ( key ( r ) ) ) , entities ) if ( not query . _order ) : if query . _filter_predicate : return filter ( ( lambda r : query . _filter_predicate ( key ( r ) ) ) , filtered_results ) return filtered_results names = query . _order . _get_prop_names ( ) if query . _filter_predicate : names |= query . _filter_predicate . _get_prop_names ( ) exists_filter = _PropertyExistsFilter ( names ) value_maps = [ ] for result in filtered_results : value_map = _make_key_value_map ( key ( result ) , names ) if ( exists_filter . _apply ( value_map ) and ( ( not query . _filter_predicate ) or query . _filter_predicate . _prune ( value_map ) ) ) : value_map [ '__result__' ] = result value_maps . append ( value_map ) value_maps . sort ( query . _order . _cmp ) return [ value_map [ '__result__' ] for value_map in value_maps ]
performs the given query on a set of in-memory entities .