How to use match_request method in localstack

Best Python code snippet using localstack_python

prospective_search_stub.py

Source:prospective_search_stub.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# Copyright 2007 Google Inc.4#5# Licensed under the Apache License, Version 2.0 (the "License");6# you may not use this file except in compliance with the License.7# You may obtain a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17"""In-memory persistent prospective_search API stub for dev_appserver."""18import base6419import bisect20import cPickle as pickle21import itertools22import logging23import os24import re25import time26import urllib27from google.appengine.api import apiproxy_stub28from google.appengine.api.prospective_search import error_pb29from google.appengine.api.prospective_search import prospective_search_pb30from google.appengine.api.search import query_parser31from google.appengine.api.search import QueryParser32from google.appengine.api.taskqueue import taskqueue_service_pb33from google.appengine.runtime import apiproxy_errors34def ValidateSubscriptionId(sub_id):35 if not sub_id:36 RaiseBadRequest('Invalid subscription id.')37def ValidateTopic(topic):38 if not topic:39 RaiseBadRequest('Invalid topic.')40def ValidateQuery(query):41 parser_return = query_parser.Parse(unicode(query, 'utf-8'))42 if parser_return.tree and parser_return.tree.getType() is QueryParser.EMPTY:43 raise query_parser.QueryException('Empty query.')44def RaiseBadRequest(message):45 raise apiproxy_errors.ApplicationError(error_pb.Error.BAD_REQUEST, message)46class ProspectiveSearchStub(apiproxy_stub.APIProxyStub):47 """Python only Prospective Search service stub."""48 def __init__(self, prospective_search_path, taskqueue_stub,49 service_name='matcher', openfile=open):50 """Initializer.51 Args:52 prospective_search_path: path for file that persists subscriptions.53 taskqueue_stub: taskqueue service stub for returning results.54 service_name: Service name expected for all calls.55 openfile: function to open the pickled subscription state.56 """57 super(ProspectiveSearchStub, self).__init__(service_name)58 self.prospective_search_path = prospective_search_path59 self.taskqueue_stub = taskqueue_stub60 self.topics = {}61 self.topics_schema = {}62 if os.path.isfile(self.prospective_search_path):63 stream = openfile(self.prospective_search_path, 'rb')64 stream.seek(0, os.SEEK_END)65 if stream.tell() != 0:66 stream.seek(0)67 self.topics, self.topics_schema = pickle.load(stream)68 def _Write(self, openfile=open):69 """Persist subscriptions."""70 persisted = openfile(self.prospective_search_path, 'wb')71 pickle.dump((self.topics, self.topics_schema), persisted)72 persisted.close()73 def _Get_Schema(self, schema_entries):74 """Converts a schema list to a schema dictionary.75 Args:76 schema_entries: list of SchemaEntry entries.77 Returns:78 Dictionary mapping field names to SchemaEntry types.79 """80 schema = {}81 for entry in schema_entries:82 schema[entry.name()] = entry.type()83 return schema84 def _Dynamic_Subscribe(self, request, response):85 """Subscribe a query.86 Args:87 request: SubscribeRequest88 response: SubscribeResponse (not used)89 """90 ValidateSubscriptionId(request.sub_id())91 ValidateTopic(request.topic())92 ValidateQuery(request.vanilla_query())93 schema = self._Get_Schema(request.schema_entry_list())94 self.topics_schema[request.topic()] = schema95 if request.lease_duration_sec() == 0:96 expires = time.time() + 0xffffffff97 else:98 expires = time.time() + request.lease_duration_sec()99 topic_subs = self.topics.setdefault(request.topic(), {})100 topic_subs[request.sub_id()] = (request.vanilla_query(), expires)101 self._Write()102 def _Dynamic_Unsubscribe(self, request, response):103 """Unsubscribe a query.104 Args:105 request: UnsubscribeRequest106 response: UnsubscribeResponse (not used)107 """108 ValidateSubscriptionId(request.sub_id())109 ValidateTopic(request.topic())110 try:111 del self.topics[request.topic()][request.sub_id()]112 except KeyError:113 pass114 self._Write()115 def _ExpireSubscriptions(self):116 """Remove expired subscriptions."""117 now = time.time()118 empty_topics = []119 for topic, topic_subs in self.topics.iteritems():120 expired_sub_ids = []121 for sub_id, entry in topic_subs.iteritems():122 _, expires = entry123 if expires < now:124 expired_sub_ids.append(sub_id)125 for sub_id in expired_sub_ids:126 del topic_subs[sub_id]127 if len(topic_subs) == 0:128 empty_topics.append(topic)129 for topic in empty_topics:130 del self.topics[topic]131 def _Dynamic_ListSubscriptions(self, request, response):132 """List subscriptions.133 Args:134 request: ListSubscriptionsRequest135 response: ListSubscriptionsResponse136 """137 ValidateTopic(request.topic())138 self._ExpireSubscriptions()139 topic_subs = self.topics.get(request.topic(), {})140 sub_ids = topic_subs.keys()141 sub_ids.sort()142 start = bisect.bisect_left(sub_ids, request.subscription_id_start())143 sub_ids = sub_ids[start:start + request.max_results()]144 for sub_id in sub_ids:145 vanilla_query, expires = topic_subs[sub_id]146 if request.has_expires_before() and expires > request.expires_before():147 continue148 record = response.add_subscription()149 record.set_id(sub_id)150 record.set_vanilla_query(vanilla_query)151 record.set_expiration_time_sec(expires)152 record.set_state(prospective_search_pb.SubscriptionRecord.OK)153 def _Dynamic_ListTopics(self, request, response):154 """List topics.155 Args:156 request: ListTopicsRequest157 response: ListTopicsResponse158 """159 topics = self.topics.keys()160 topics.sort()161 if request.has_topic_start():162 start = bisect.bisect_left(topics, request.topic_start())163 else:164 start = 0165 iter_topics = topics[start:start + request.max_results()]166 for topic in iter_topics:167 response.topic_list().append(topic)168 def _DeliverMatches(self, subscriptions, match_request):169 """Deliver list of subscriptions as batches using taskqueue.170 Args:171 subscriptions: list of subscription ids172 match_request: MatchRequest173 """174 parameters = {'topic': match_request.topic()}175 if match_request.has_result_python_document_class():176 python_document_class = match_request.result_python_document_class()177 parameters['python_document_class'] = python_document_class178 parameters['document'] = base64.urlsafe_b64encode(179 match_request.document().Encode())180 if match_request.has_result_key():181 parameters['key'] = match_request.result_key()182 taskqueue_request = taskqueue_service_pb.TaskQueueBulkAddRequest()183 batch_size = match_request.result_batch_size()184 for i in xrange(0, len(subscriptions), batch_size):185 add_request = taskqueue_request.add_add_request()186 add_request.set_queue_name(match_request.result_task_queue())187 add_request.set_task_name('')188 add_request.set_eta_usec(0)189 add_request.set_url(match_request.result_relative_url())190 add_request.set_description('prospective_search::matches')191 header = add_request.add_header()192 header.set_key('content-type')193 header.set_value('application/x-www-form-urlencoded; charset=utf-8')194 parameters['results_count'] = len(subscriptions)195 parameters['results_offset'] = i196 parameters['id'] = subscriptions[i:i+batch_size]197 add_request.set_body(urllib.urlencode(parameters, doseq=True))198 taskqueue_response = taskqueue_service_pb.TaskQueueBulkAddResponse()199 self.taskqueue_stub._Dynamic_BulkAdd(taskqueue_request, taskqueue_response)200 def _Dynamic_Match(self, request, response):201 """Match a document.202 Args:203 request: MatchRequest204 response: MatchResponse (not used)205 """206 self._ExpireSubscriptions()207 doc = {}208 properties = itertools.chain(request.document().property_list(),209 request.document().raw_property_list())210 for prop in properties:211 prop_name = unicode(prop.name(), 'utf-8')212 doc.setdefault(prop_name, [])213 if prop.value().has_int64value():214 value = prop.value().int64value()215 if (value < 2**32) and (value >= -2**32):216 doc[prop_name].append(prop.value().int64value())217 elif prop.value().has_stringvalue():218 unicode_value = unicode(prop.value().stringvalue(), 'utf-8')219 doc[prop_name].append(unicode_value)220 elif prop.value().has_doublevalue():221 doc[prop_name].append(prop.value().doublevalue())222 elif prop.value().has_booleanvalue():223 doc[prop_name].append(prop.value().booleanvalue())224 matches = []225 topic_subs = self.topics.get(request.topic(), {})226 sub_ids = topic_subs.keys()227 for sub_id in sub_ids:228 vanilla_query, _ = topic_subs[sub_id]229 if self._FindMatches(vanilla_query, doc):230 matches.append(sub_id)231 if matches:232 self._DeliverMatches(matches, request)233 def _FindMatches(self, query, doc):234 """Entry point for matching document against a query."""235 self._Debug('_FindMatches: query: %r, doc: %s' % (query, doc), 0)236 query_tree = self._Simplify(query_parser.Parse(unicode(query, 'utf-8')))237 match = self._WalkQueryTree(query_tree, doc)238 self._Debug('_FindMatches: result: %s' % match, 0)239 return match240 def ExtractGlobalEq(self, node):241 if node.getType() == QueryParser.EQ and len(node.children) >= 2:242 if node.children[0].getType() == QueryParser.GLOBAL:243 return node.children[1]244 return node245 def _WalkQueryTree(self, query_node, doc, query_field=None, level=0):246 """Recursive match of doc from query tree at the given node."""247 query_type = query_node.getType()248 query_text = query_node.getText()249 self._Debug('_WalkQueryTree: query type: %r, field: %r, text: %r'250 % (query_type, query_field, query_text), level=level)251 if query_type is QueryParser.CONJUNCTION:252 for child in query_node.children:253 if not self._WalkQueryTree(254 self.ExtractGlobalEq(child), doc, query_field, level=level + 1):255 return False256 return True257 elif query_type is QueryParser.DISJUNCTION:258 for child in query_node.children:259 if self._WalkQueryTree(260 self.ExtractGlobalEq(child), doc, query_field, level=level + 1):261 return True262 if query_type is QueryParser.NEGATION:263 self._Debug(('No such field so no match: field: %r, children: %r'264 % (query_type, query_node.children[0])),265 level)266 child = query_node.children[0]267 return not self._WalkQueryTree(268 self.ExtractGlobalEq(child), doc, query_field, level=level + 1)269 elif query_type is QueryParser.HAS:270 if query_node.children[0].getType() is not QueryParser.GLOBAL:271 query_field = query_node.children[0].getText()272 if query_field not in doc:273 self._Debug(('No such field so no match: field: %r' % query_field),274 level)275 return False276 return self._WalkQueryTree(query_node.children[1], doc, query_field,277 level=level + 1)278 elif query_type is QueryParser.VALUE or query_type is QueryParser.TEXT:279 if query_parser.IsPhrase(query_node):280 query_text = query_parser.GetQueryNodeTextUnicode(query_node)281 if query_field is not None:282 return self._MatchField(doc, query_field, query_text, level=level)283 for field_name in doc:284 if self._MatchField(doc, field_name, query_text, level=level):285 return True286 elif query_type in query_parser.COMPARISON_TYPES:287 query_field = query_node.children[0].getText()288 query_text = query_node.children[1].getText()289 if query_field is not None:290 if query_field not in doc:291 self._Debug(('No such field so no match: field: %r' % query_field),292 level)293 return False294 return self._MatchField(doc, query_field, query_text, query_type,295 level=level)296 for field_name in doc:297 if self._MatchField(doc, field_name, query_text, query_type,298 level=level):299 return True300 self._Debug('Fallthrough at %s returning false, query_node.children: %s'301 % (query_text, [n.getText() for n in query_node.children]),302 level)303 return False304 def _MatchField(self, doc, field_name, query_val, op=QueryParser.HAS,305 level=0):306 """Returns true iff 'doc[field_name] op query_val' evaluates to true."""307 field_vals = doc[field_name]308 if type(field_vals) is not list:309 field_vals = list(field_vals)310 self._Debug('_MatchField: doc[%s]: %r %s %r'311 % (field_name, field_vals, op, query_val), level)312 if (op is QueryParser.HAS313 or (op is QueryParser.EQ314 and type(field_vals[0]) is unicode)):315 if query_val.startswith('"') and query_val.endswith('"'):316 query_val = query_val[1:-1]317 query_val = re.sub(r'\s+', r'\s+', query_val)318 re_query = re.compile(u'(^\\s*|\\s+)%s(\\s+|\\s*$)'319 % query_val, re.IGNORECASE)320 for val in field_vals:321 value_text = ('%s' % val)322 if re_query.search(value_text):323 return True324 elif op is QueryParser.EQ:325 for val in field_vals:326 if (type(val) is int) or (type(val) is float):327 if val == float(query_val):328 return True329 elif type(val) is bool:330 query_bool = re.match('(?i)true', query_val) is not None331 if val == bool(query_bool):332 return True333 else:334 query_num = float(query_val)335 if op is QueryParser.GT:336 for val in field_vals:337 if val > query_num: return True338 elif op is QueryParser.GE:339 for val in field_vals:340 if val >= query_num: return True341 elif op is QueryParser.LESSTHAN:342 for val in field_vals:343 if val < query_num: return True344 elif op is QueryParser.LE:345 for val in field_vals:346 if val <= query_num: return True347 return False348 def _Simplify(self, parser_return):349 """Simplifies the output of the parser."""350 if parser_return.tree:351 return self._SimplifyNode(query_parser.SimplifyNode(parser_return.tree))352 return parser_return353 def _SimplifyNode(self, node):354 """Simplifies the node removing singleton conjunctions and others."""355 if not node.getType():356 return self._SimplifyNode(node.children[0])357 elif (node.getType() in query_parser.COMPARISON_TYPES358 and node.getChildCount() is 2359 and node.children[0].getType() is QueryParser.GLOBAL):360 return self._SimplifyNode(node.children[1])361 elif node.getType() is QueryParser.SEQUENCE:362 return self._SimplifyNode(query_parser.SequenceToConjunction(node))363 elif (node.getType() is QueryParser.VALUE364 and node.getChildCount() is 2 and365 (node.children[0].getType() is QueryParser.TEXT or366 node.children[0].getType() is QueryParser.STRING or367 node.children[0].getType() is QueryParser.NUMBER)):368 return self._SimplifyNode(node.children[1])369 elif (node.getType() is QueryParser.EQ and370 (node.children[0].getType() is QueryParser.CONJUNCTION371 or node.children[0].getType() is QueryParser.DISJUNCTION)):372 return self._SimplifyNode(node.children[0])373 for i, child in enumerate(node.children):374 node.setChild(i, self._SimplifyNode(child))375 return node376 def _Debug(self, msg, level):377 """Helper method to print out indented messages."""...

Full Screen

Full Screen

uber.py

Source:uber.py Github

copy

Full Screen

...239 continue240 d.status = Driver.Status.MATCHING241 d.match_request = match_request242 p.status = Passenger.Status.MATCHING243 self._log_match_request(match_request)244 def _generate_match_requests(self) -> List[MatchRequest]:245 return self.matcher.match(self.drivers, self.passengers, self.map)246 def _check_graph(self, graph):247 # only allow nx.DiGraph248 if not type(graph) is nx.DiGraph:249 raise TypeError("graph should be of type nx.DiGraph")250 # only allow strongly connected graphs251 if not nx.is_strongly_connected(graph):252 raise ValueError("graph is not strongly connected")253 254 # only allow constant weights255 weight = None256 for u,v,d in graph.edges(data=True):257 try:258 edge_weight = d["weight"]259 except:260 raise ValueError("edges of the graphs should have the weight attribute")261 if weight is None:262 weight = edge_weight263 elif edge_weight != weight:264 raise ValueError("edges of the graph should have constant weight")265 return weight266 def _log(self):267 if not self.is_logging:268 return269 270 for message in self.messages:271 logging.info(message)272 273 self.messages = []274 def _log_match(self, driver: int, response: int):275 if not(response == 0 or response == 1):276 return277 if response == 0:278 message = f"Driver {driver} rejected match request."279 elif response == 1:280 message = f"Driver {driver} accepted match request."281 self.messages.append(message)282 def _log_move(self, driver: int, prev_position: int, new_position: int, arrival: bool = False, pickup: bool = False, passenger: int = None):283 284 self.messages.append(f"Driver {driver} moved from {prev_position} to {new_position}.")285 if arrival:286 self.messages.append(f"Driver {driver} arrived at passenger {passenger}'s destination.")287 288 if pickup:289 self.messages.append(f"Driver {driver} picked up the passenger {passenger}.")290 def _log_match_request(self, match_request: MatchRequest):291 self.messages.append(f"Match Request for driver {match_request.driver}, passenger {match_request.passenger} with price {match_request.price}.")292 def _log_passenger_generation(self, passenger: int, position: int, destination: int):...

Full Screen

Full Screen

serializers.py

Source:serializers.py Github

copy

Full Screen

1from rest_framework.serializers import ModelSerializer, SerializerMethodField, CurrentUserDefault, CharField, IntegerField2from rest_framework.validators import UniqueTogetherValidator3from django.db.models import Q, Count4from .models import *5import time 6from datetime import datetime7class MeetingTypeSerializer(ModelSerializer):8 class Meta:9 model = MeetingType10 fields = ("id", "name", "created_at")11class PlaceTypeSerializer(ModelSerializer):12 class Meta:13 model = PlaceType14 fields = ("id", "name", "created_at")15class PlaceSerializer(ModelSerializer):16 class Meta:17 model = Place18 fields = ("id", "name", "place_type", "is_premium", "description", "link", "created_at")19class RatingSerializer(ModelSerializer):20 class Meta:21 model = Rating22 fields = ("id", "description", "created_at")23class MatchedMeetingSerializer(ModelSerializer):24 openby_nickname = CharField(source='openby.nickname', read_only=True)25 openby_uid = CharField(source='openby.uid', read_only=True)26 openby_age = CharField(source='openby.age', read_only=True)27 openby_belong = CharField(source='openby.belong', read_only=True)28 openby_department = CharField(source='openby.department', read_only=True)29 class Meta:30 model = Meeting31 fields= ("id", "openby_uid", "openby_nickname", "openby_age", "openby_belong", "openby_department", "rating")32class MeetingSerializer(ModelSerializer):33 place_type_name = CharField(source='place_type.name', read_only=True)34 openby_nickname = CharField(source='openby.nickname', read_only=True)35 openby_age = CharField(source='openby.age', read_only=True)36 openby_belong = CharField(source='openby.belong', read_only=True)37 openby_department = CharField(source='openby.department', read_only=True)38 openby_profile = CharField(source='openby.image', read_only=True)39 received_request = SerializerMethodField('get_received_request_prefetch_related')40 sent_request = SerializerMethodField('get_sent_request_prefetch_related')41 matched_meeting = MatchedMeetingSerializer()42 def get_received_request_prefetch_related(self, meeting):43 match_request_queryset = meeting.match_receiver.all().order_by('created_at').filter(Q(is_declined=False))44 45 data = [{'id': match_request.id,'is_selected': match_request.is_selected, 'sender': match_request.sender.id, 'receiver': match_request.receiver.id, 'manager_uid': match_request.manager.uid, 'manager_name': match_request.manager.nickname, 'manager_profile': match_request.manager.image, 'accepted_at': int(((match_request.accepted_at - datetime(1970, 1, 1)).total_seconds() - 3600 * 9)* 1000) if match_request.accepted_at != None else None46} for match_request in match_request_queryset]47 return_data = { 'count': match_request_queryset.count(), 'data': data}48 return return_data49 def get_sent_request_prefetch_related(self, meeting):50 match_request_queryset = meeting.match_sender.all().order_by('created_at').filter(Q(is_declined=False))51 data = [{'id': match_request.id,'is_selected': match_request.is_selected, 'sender': match_request.sender.id, 'receiver': match_request.receiver.id, 'manager_uid': match_request.manager.uid, 'manager_name': match_request.manager.nickname, 'manager_profile': match_request.manager.image, 'accepted_at': int(((match_request.accepted_at - datetime(1970, 1, 1)).total_seconds() - 3600 * 9)* 1000) if match_request.accepted_at != None else None} for match_request in match_request_queryset]52 return_data = { 'count': match_request_queryset.count(), 'data': data}53 return return_data 54 class Meta:55 model = Meeting56 fields = ("id", "meeting_type", "openby", "openby_nickname", "openby_age", "openby_belong", "openby_department", "openby_profile", "date", "place_type", "place_type_name", "place", "appeal", "rating", "is_matched", "created_at", "received_request", "sent_request", "matched_meeting")5758class MeetingCreateSerializer(ModelSerializer):59 class Meta:60 model = Meeting61 fields = ("id", "meeting_type", "openby", "date", "place_type", "appeal", "rating", "is_matched", "created_at")62 validators = [63 UniqueTogetherValidator(64 queryset=Meeting.objects.all(), 65 fields=['openby', 'date']66 )67 ]68class MatchRequestSerializer(ModelSerializer):69 manager_uid = IntegerField(source='manager.uid', read_only=True)70 class Meta:71 model = MatchRequest72 fields = ("id", "sender", "receiver", "manager", "manager_uid", "accepted_at", "created_at")73class MatchRequestSenderSerializer(ModelSerializer):74 sender = MeetingSerializer()75 #receiver = MeetingSerializer()76 class Meta:77 model = MatchRequest78 fields = ("id", "sender", "created_at")7980class MatchRequestReceiverSerializer(ModelSerializer):81 #sender = MeetingSerializer()82 receiver = MeetingSerializer()83 class Meta:84 model = MatchRequest85 fields = ("id", "receiver", "created_at")8687class ChattingRoomListSerializer(ModelSerializer):88 manager_info = SerializerMethodField('get_manager_info')89 meeting_info = SerializerMethodField('get_meeting_info')90 receiver_info = SerializerMethodField('get_receiver_info')91 sender_info = SerializerMethodField('get_sender_info')92 def get_manager_info(self, match):93 return {'id': match.manager.id, 'uid': match.manager.uid, 'nickname': match.manager.nickname}94 def get_meeting_info(self, match):95 return {'type': match.sender.meeting_type.name, 'date': match.sender.date, 'accepted_at': match.accepted_at}96 def get_receiver_info(self, match):97 return {'meeting_id': match.receiver.id, 'id': match.receiver.openby.id, 'uid': match.receiver.openby.uid, 'nickname': match.receiver.openby.nickname, 'age': match.receiver.openby.age, 'gender': '남' if match.receiver.openby.gender == 1 else '여', 'belong': match.receiver.openby.belong, 'department': match.receiver.openby.department }98 def get_sender_info(self, match):99 return {'meeting_id': match.sender.id, 'id': match.sender.openby.id, 'uid': match.sender.openby.uid, 'nickname': match.sender.openby.nickname, 'age': match.sender.openby.age, 'gender': '남' if match.sender.openby.gender == 1 else '여', 'belong': match.sender.openby.belong, 'department': match.sender.openby.department}100 class Meta:101 model = MatchRequest ...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful