Best Python code snippet using localstack_python
generic_proxy.py
Source:generic_proxy.py
...267 return source268 elif not isinstance(source, str):269 # Ignore any other types270 return source271 return self.arn_regex.sub(lambda m: self._adjust_match(m, static_partition), source)272 def _adjust_match(self, match: Match, static_partition: str = None):273 region = match.group("Region")274 partition = self._partition_lookup(region) if static_partition is None else static_partition275 service = match.group("Service")276 account_id = match.group("AccountID")277 resource_path = match.group("ResourcePath")278 return f"arn:{partition}:{service}:{region}:{account_id}:{resource_path}"279 def _partition_lookup(self, region: str):280 try:281 partition = self._get_partition_for_region(region)282 except ArnPartitionRewriteListener.InvalidRegionException:283 try:284 # If the region is not properly set (f.e. because it is set to a wildcard),285 # the partition is determined based on the default region.286 partition = self._get_partition_for_region(config.DEFAULT_REGION)...
TMMatching.py
Source:TMMatching.py
...67 else: break68 return l_best_segments69 # Estimate TER between sources segmets.70 def ter_score(self, src_x, src_y): return pyter.ter(src_x.split(), src_y.split())71 def _adjust_match(self, segment, filter, ter):72 if ter > 1:73 ter = 174 ter = (100 - (ter * 100))75 if filter:76 # Penalize match if segment doesn't match filter77 for fname, fvalues in filter.items():78 if fvalues and not set(fvalues) & set(segment[fname]):79 ter -= 580 return ter81 #Check 100% match82 def _cen_p_match(self, l_best_segments):83 #print(l_best_segments)84 segment = l_best_segments[0][0]85 match = l_best_segments[0][1]86 ter = self.ter_score(self.src_input, segment['source_text'])87 logging.info("initial ter: {}".format(ter))88 if match == 100 and 'Q' not in segment.keys() and ter == 0: # 100% match --> Return match considering domain89 # logging.info("source: {}, tm_source: {}, tm_target: {}, match: {}".format(src_input, segment['source_text'], segment['target_text'], str(ter)))90 self.dic_match = self._create_dic_match(segment, ter)91 return self.dic_match92 def _create_dic_match(self, segment, ter):93 filter = {'domain': ['Automotive']}94 print(filter)95 self.dic_match['tm_src'] = segment['source_text']96 print(self.dic_match['tm_src'])97 self.dic_match['tm_tgt'] = segment['target_text']98 print(self.dic_match['tm_tgt'])99 self.dic_match['match'] = self._adjust_match(segment, filter, ter)100 print(self.dic_match)101 return self.dic_match102 def fuzzy_match(self, l_best_segments):103 logging.info("Improve Fuzzy match")104 fuzzy_alg = TMFuzzyMatch(self.src_input, self.src_lang, self.tgt_lang, self.dic_match, self.threshold, l_best_segments) #Class to improve fuzzy match105 self.dic_match = fuzzy_alg.process()106 return self.dic_match107 def fuzzy_match_posTagger(self, l_best_segments):108 logging.info("Improve Fuzzy match pos tagger")109 fuzzy_alg = TMFuzzyMatch_PosTagger(self.src_input, self.src_lang, self.tgt_lang, self.dic_match, self.threshold,110 l_best_segments) # Class to improve fuzzy match111 self.dic_match = fuzzy_alg.process()112 return self.dic_match113 # ************It is necessary check the domain***************114 # if (improve_ter <= ter) and (115 # improve_ter < 0.25): # Fuzzy Match and was possible improve ter value --> Return match considering domain116 # print("source: " + src_input + ' tm_target: ' + process_match['reg_exp']['tm_tgt_re'] + ' Match: ' + str(117 # self._adjust_match(segment, filter, improve_ter)))118 # break119 # if (120 # count == 10 or improve_ter > ter) and ter < 0.25: # Fuzzy Match, but wasn't improved ter value --> Return elasticsearch match121 # print("source: " + src_input + ' tm_target: ' + segment['target_text'] + ' Match: ' + str(122 # self._adjust_match(segment, filter, ter)))123 # todo_ok = False124 # break125 #126 # else:127 # todo_ok = False128# ************************************************************************************129class TMFuzzyMatch(TMMatching):130 def __init__(self, src_input, src_lang, tgt_lang, dic_match, threshold, l_best_segments):131 super().__init__(src_input, src_lang, tgt_lang, dic_match, threshold)132 self.l_best_segments = l_best_segments133 #self.pipe = ['tokenizer', 'reg_exp', 'pos_tagger', 'sent_split']134 #self.processor = {'reg_exp': TMRegExpPreprocessor(),135 # 'sent_split': 'split'} # TMSplitPreprocessor()136 def pre_process(self, text, lang, preprocess):...
TMDbApi.py
Source:TMDbApi.py
...378 break379 # Adjust match % according to filters380 if ter >= qparams.min_match:381 self.timer.start("adjust_match")382 ter = self._adjust_match(segment, qparams.domains, ter)383 self.timer.stop("adjust_match")384 new_segments.append((segment, ter))385 match = True386 elif qparams.aut_trans and len(segments) == 1: # Mark segment as one needed to machine-translate387 match = False388 new_segments.append((segment, ter))389 new_segments.sort(key = lambda x: (x[1], x[0].tm_change_date) if (x[0].tm_change_date is not None) else (x[1], str(datetime.datetime(1970, 1, 1))), reverse = True)390 logging.info("New match segments: {}".format(new_segments))391 tm_match.timer.print()392 return new_segments, match393 def _add_segments(self, segments):394 batch_status = []395 self.timer.start("add_segments:source")396 batch_status.append(self.ml_index.add_segments(segments, 'source'))397 self.timer.stop("add_segments:source")398 self.timer.start("add_segments:target")399 batch_status.append(self.ml_index.add_segments(segments, 'target'))400 self.timer.stop("add_segments:target")401 self.timer.start("add_segments:map")402 batch_status.append(self.seg_map.add_segments(segments))403 self.timer.stop("add_segments:map")404 logging.info('Added {} segments, status: {}'.format(len(segments), batch_status))405 return batch_status406 def _find_pivot_lang(self, langs):407 langs = [l.lower() for l in langs]408 lang_graph = self.seg_map.get_lang_graph()409 path_len = shortest_path_length(lang_graph, langs[0], langs[1])410 if path_len != 2:411 return None412 # Find shortest path413 path = shortest_path(lang_graph, langs[0], langs[1])414 assert (len(path) == 3)415 # Get a pivot language and scan all pivot segments416 return path[1]417 def _src_id2tgt_id(self, src_id, source_lang, target_lang):418 # Query mapping segment419 target_id,map_doc = self.seg_map.get(uuid.UUID(src_id), source_lang, target_lang)420 if not target_id:421 logging.warning("Can't find matching segment for {}".format(src_id))422 return None,None423 else:424 assert isinstance(target_id, uuid.UUID)425 return target_id,map_doc426 def _msrc_id2tgt_id(self, src_hits, source_lang, target_lang, return_multiple=False):427 margs = [(uuid.UUID(src_hit.meta.id), source_lang, target_lang) for src_hit in src_hits]428 return self.seg_map.mget(margs, return_multiple=return_multiple)429 def _delete(self, langs, docs, filter, force_delete):430 source_lang, target_lang = langs431 # Delete map doc, returns tuple of 2 lists: deleted source and target ids432 deleted_ids = self.seg_map.delete(langs, docs, filter, force_delete)433 logging.info("After deleting from map: {} source and {} target potential orphan segments".format(len(deleted_ids[0]), len(deleted_ids[1])))434 # Check and delete only orphans (for source and target)435 for lang,ids in zip(langs,deleted_ids):436 ids_exist = self.seg_map.mexist(source_lang, ids)437 ids_to_delete = [id for id,exists in zip(ids, ids_exist) if not exists]438 logging.info("Lang: {}, actual orphans to delete: {}".format(lang.upper(), len(ids_to_delete)))439 self.ml_index.delete(lang, ids_to_delete)440 def _doc2segment(self, md, sd=None, td=None):441 doc = md442 if sd: doc['source_pos'] = sd.get('pos')443 if td: doc['target_pos'] = td.get('pos')444 segment = TMTranslationUnit(doc)445 return segment446 def _adjust_match(self, segment, domains, match):447 if domains:448 if not set(domains) & set(getattr(segment, 'domain')):449 match -= self.QUERY_PENALIZE[0]450 if self._is_dirty(segment):451 match -= self.QUERY_PENALIZE[1]452 return match453 def _is_dirty(self, segment):454 if segment.dirty_score and segment.dirty_score >= self.DIRTY_THRESHOLD:455 return True456 return False457if __name__ == "__main__":458 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)459 tmdb = TMDbApi()460 #tmdb.init_db()...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!