Best Python code snippet using pyshould_python
entity_wrapper.py
Source:entity_wrapper.py
1import aiohttp2import dill3import logging4from pathlib import Path5class EntityWrapperException(Exception):6 pass7class EntityWrapper:8 def __init__(self, service_url, client_session: aiohttp.ClientSession):9 self.service_url = service_url10 self.logger = logging.getLogger('entity_matcher')11 self.client_session = client_session12 self.train_labels = None13 self.train_entities_q = None14 self.train_entities_a = None15 async def get_from_er_server(self, relative_url, params=None):16 try:17 async with self.client_session.get(18 self.service_url + "/" + relative_url,19 params=params) as resp:20 status = resp.status21 if status != 200:22 raise EntityWrapperException(23 "ER call to {} failed with status {}".format(24 relative_url, status))25 response = await resp.json()26 if response is None:27 raise EntityWrapperException("Response was none")28 return response29 except (aiohttp.client_exceptions.ClientConnectorError,30 aiohttp.client_exceptions.ContentTypeError) as exc:31 raise EntityWrapperException("aiohttp error", exc)32 async def extract_entities(self, sample):33 entities = await self.get_from_er_server("ner", {'q': sample})34 if not isinstance(entities, list):35 raise EntityWrapperException(36 "Unexpected ER response - should be a list")37 for e in entities:38 if "'s" in e['value']:39 e['value'] = e['value'].replace("'s", "")40 e['value'] = e['value'].lower()41 if e['value'].startswith('the'):42 e['value'] = e['value'].replace('the', '')43 self.logger.debug("entities for '{}': {}".format(sample, entities))44 return entities45 async def tokenize(self, sample, filter_ents='True', sw_size='small'):46 tokens = await self.get_from_er_server("tokenize", {47 'q': sample,48 'filter_ents': filter_ents,49 'sw_size': sw_size50 })51 if not isinstance(tokens, list):52 raise EntityWrapperException(53 "Unexpected ER response - should be a list")54 return [token for token in tokens]55 def __prepro_question(self, test_q):56 test_match = test_q.lower()57 test_match = test_match.replace('"', '')58 test_match = test_match.replace("'s", "")59 test_match = test_match.replace("", "")60 d = test_match.maketrans('', '', '!"\'(),./:;<=>?[\\]`{|}')61 test_match = test_match.translate(d)62 test_match = test_match.split()63 return test_match64 def match_entities(self, test_q, ents_msg, subset_idxs=None):65 test_match = self.__prepro_question(test_q)66 self.logger.info("test_match: {}".format(test_match))67 # subset if already pre-selected using different algo68 sub_idxs = subset_idxs if subset_idxs is not None else list(69 range(len(self.train_entities_a)))70 ents_q_a = [(i, self.train_entities_q[i], self.train_entities_a[i])71 for i in sub_idxs]72 # search for interrogative words matching entitites73 interrog_matches = [(i, ents_q,74 self.interrogative_match(75 test_match, (ents_msg, ents_q, ents_a)))76 for i, ents_q, ents_a in ents_q_a]77 _, _, cnt = zip(*interrog_matches)78 self.logger.debug("interrog count: {}".format(cnt))79 max_cnt = max(cnt)80 interrog_matches = [(m[0], m[1]) for m in interrog_matches81 if m[2] == max_cnt and max_cnt > 0]82 self.logger.info("interrog matches: {}".format(interrog_matches))83 if len(interrog_matches) > 0:84 train_ents = interrog_matches85 else:86 train_ents = [(i, ents_q) for i, ents_q, ents_a in ents_q_a]87 # search for entity matches between train and test q's88 matched_labels = self.find_matches(train_ents, test_match)89 if len(matched_labels) > 0:90 self.logger.info("entity matches: {} ({} max matches)".format(91 matched_labels, max_cnt))92 return matched_labels93 elif len(interrog_matches) > 0:94 self.logger.info("interrog matches: {}".format(95 [(i, self.train_labels[i]) for i, _ in interrog_matches]))96 return [(i, self.train_labels[i]) for i, _ in interrog_matches]97 else:98 self.logger.info("no entity matches")99 return []100 def find_matches(self, train_ents, test_match):101 max_matches = 0102 matched_labels = []103 for i, tr_ents in train_ents:104 num_matches = 0105 self.logger.debug("train sample ents: {}".format(tr_ents))106 num_matches += sum(1 if e not in ['the'] and e in test_match else 0107 for ent in tr_ents for e in self.split_entities(ent))108 if num_matches > max_matches:109 max_matches = num_matches110 matched_labels = [(i, self.train_labels[i])]111 elif num_matches == max_matches and max_matches > 0:112 matched_labels.append((i, self.train_labels[i]))113 return matched_labels114 def split_entities(self, ent):115 if ent['category'] in [116 'sys.person', 'sys.group', 'sys.organization'117 ]:118 tmp_ent = ent['value'].split()119 else:120 tmp_ent = [ent['value']]121 return tmp_ent122 def interrogative_match(self, test_match, ents):123 match = 0124 match += int(self.check_who_questions(test_match, ents))125 match += int(self.check_for_person(test_match, ents))126 match += int(self.check_who_questions_inv(test_match, ents))127 # match += int(self.check_what_questions(test_match, ents))128 return match129 def check_who_questions(self, test_match, ents):130 ents_msg, ents_q, ents_a = ents131 match = False132 if 'who' in test_match and not any(133 [e['category'] == 'sys.person' for e in ents_msg]):134 match = any(135 [ent_a['category'] == 'sys.person' for ent_a in ents_a])136 return match137 def check_who_questions_inv(self, test_match, ents):138 ents_msg, ents_q, ents_a = ents139 match = False140 if 'who' in test_match:141 match = any([142 e in test_match for ent in ents_q143 for e in ent['value'].split()144 if ent['category'] == 'sys.person'145 ])146 return match147 def check_what_questions(self, test_match, ents):148 ents_msg, ents_q, ents_a = ents149 match = False150 orgs = [151 e['value'].lower() for e in ents_msg152 if e['category'] == ['sys.group', 'sys.organization']153 ]154 if 'what' in test_match and len(orgs) > 0:155 match = any([ent_q['value'] in orgs for ent_q in ents_q])156 return match157 def check_for_person(self, test_match, ents):158 ents_msg, ents_q, ents_a = ents159 person = [160 e['value'].lower().split() for e in ents_msg161 if e['category'] == 'sys.person'162 ]163 person = [e for p in person for e in p]164 if len(person) > 0:165 return any(166 [p in e['value'].lower() for e in ents_q for p in person])167 else:168 return False169 def save_data(self, file_path: Path, q_ents, a_ents, train_labels):170 if not isinstance(q_ents, list) or not isinstance(a_ents, list):171 self.logger.error('data to save must be list')172 raise EntityWrapperException('data to save must be list')173 with file_path.open('wb') as f:174 dill.dump([q_ents, a_ents, train_labels], f)175 def load_data(self, file_path: Path):176 with file_path.open('rb') as f:177 d = dill.load(f)178 self.train_entities_q = d[0]179 self.train_entities_a = d[1]...
views.py
Source:views.py
...25 crumbs = [26 ("Homepage", reverse("teacher_index")),27 ("Coursework", reverse(redir_path, args=[test_match.coursework.id]))28 ]29 group = fh.feedback_group_for_test_match(test_match)30 comment_list = [(c.submit_date, c.comment,31 fh.nick_for_comment(c.user, group, request.user))32 for c in cm.Comment.objects.filter(object_pk=test_match_id)]33 names = fh.detail_test_match_anon_names(request.user, test_match, group)34 details = {35 "test_match": test_match,36 "solution_name": names[1],37 "test_name": names[2],38 "can_submit": perm == p.TestMatchMode.WRITE,39 "test_files": test_match.test.get_files(test_match.test_version),40 "result_files": test_match.result.get_files(),41 "solution_files": test_match.solution.get_files(test_match.solution_version),42 "user_owns_test": test_match.test.creator == request.user,43 "user_owns_sol": test_match.solution.creator == request.user,...
centrists.py
Source:centrists.py
1def central(l, names):2 can_be_center = ["YES"] * 33 first = names[0]4 second = names[1]5 third = names[2]6 match = ""7 odd = ""8 odd_index = -19 for letter in range(l):10 if odd:11 # print("odd loop")12 test_match = [""] * 313 compare_indexes = []14 for i in range(3):15 if i == odd_index:16 continue17 else:18 compare_indexes.append(i)19 # print("index {}".format(i))20 if names[i][letter] == odd:21 test_match[i] = "ODD"22 # print("at letter {}, {} matches odd {}".format(letter, i, odd))23 elif names[i][letter] == match:24 test_match[i] = "MATCH"25 # print("at letter {}, {} matches match".format(letter, i, match))26 else:27 test_match[i] = names[i][letter]28 if len(compare_indexes) == 2 and test_match[compare_indexes[0]] == test_match[compare_indexes[1]]:29 continue30 elif test_match.count("ODD") == 1 and test_match.count("MATCH") == 1:31 extreme_index = test_match.index("MATCH")32 can_be_center[extreme_index] = "NO"33 # print("extreme no")34 break35 else:36 break37 if first[letter] == second[letter] and first[letter] != third[letter]:38 can_be_center[2] = "NO"39 match = first[letter]40 odd = third[letter]41 # print("odd: {}".format(odd))42 odd_index = 243 elif third[letter] == second[letter] and third[letter] != first[letter]:44 can_be_center[0] = "NO"45 match = third[letter]46 odd = first[letter]47 # print("odd: {}".format(odd))48 odd_index = 049 return can_be_center50def driver():51 t = int(raw_input())52 for i in range(1, t + 1):53 L = int(raw_input())54 names = [str(s) for s in raw_input().split(" ")]55 result = central(L, names)56 print("Case #{}: {} {} {}".format(i, result[0], result[1], result[2]))...
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!!