How to use get_path_ method in autotest

Best Python code snippet using autotest_python

urlmatcher.py

Source:urlmatcher.py Github

copy

Full Screen

...69 for _, u in enumerate(matching_urls):70 parsed_u = urlparse(u)71 if parsed_u.scheme != parsed_url_to_match.scheme:72 continue73 url_path = get_path_(parsed_url_to_match)74 u_path = get_path_(parsed_u)75 if not should_use_matched_(url, u):76 continue77 score = compute_levenshtein_(url_path, u_path)78 # Levenshtein edit distance is the number of edits between the two79 # string. Thus, the smaller the better.80 if score < best_match_score:81 best_match = u82 best_match_score = score83 # Do a final sanity check that the match make sense.84 if best_match == NO_MATCH:85 return best_match, best_match_score86 return best_match, best_match_score87 def get_all_sift4_scores_(self, url, matching_urls):88 '''Get all matching scores for the given URL.'''89 parsed_url_to_match = urlparse(url)90 result = {}91 for _, u in enumerate(matching_urls):92 parsed_u = urlparse(u)93 if parsed_u.scheme != parsed_url_to_match.scheme:94 result[u] = DEFAULT_EDIT_DISTANCE95 continue96 if not should_use_matched_(url, u):97 result[u] = DEFAULT_EDIT_DISTANCE98 continue99 url_path = get_path_(parsed_url_to_match)100 u_path = get_path_(parsed_u)101 # Search for 500 tokens with a maximum of 200 distance.102 score = compute_sift4_(url_path, u_path, 5)103 result[u] = score104 return result105 def sift4_(self, url, matching_urls):106 '''This is based on Sift4 matching algorithm.'''107 best_match = NO_MATCH108 best_match_score = DEFAULT_EDIT_DISTANCE109 all_sift4_scores = self.get_all_sift4_scores_(url, matching_urls)110 for matching_url, score in all_sift4_scores.items():111 # Levenshtein edit distance is the number of edits between the two112 # string. Thus, the smaller the better.113 if not should_use_matched_(url, matching_url):114 continue115 parsed_matching_url = urlparse(matching_url)116 matching_url_path = get_path_(parsed_matching_url)117 if score < best_match_score:118 best_match = matching_url119 best_match_score = score120 return best_match, best_match_score121 def mahimahi_(self, url, matching_urls):122 '''This is based on Mahimahi's matching function.'''123 parsed_url_to_match = urlparse(url)124 url_to_match_without_query = '{scheme}://{netloc}{path}'.format(125 scheme=parsed_url_to_match.scheme,126 netloc=parsed_url_to_match.netloc,127 path=parsed_url_to_match.path)128 best_match = NO_MATCH129 best_match_score = -1130 for _, u in enumerate(matching_urls):131 parsed_u = urlparse(u)132 if parsed_u.scheme != parsed_url_to_match.scheme:133 continue134 if remove_query_(parsed_url_to_match) != remove_query_(parsed_u):135 continue136 score = min(len(url), len(u))137 for i in range(score):138 if url[i] != u[i]:139 if i > best_match_score:140 best_match = u141 best_match_score = i142 break143 if score > best_match_score:144 best_match = u145 best_match_score = score146 return best_match, best_match_score147def compute_levenshtein_(seq1, seq2):148 size_x = len(seq1) + 1149 size_y = len(seq2) + 1150 matrix = np.zeros ((size_x, size_y))151 for x in range(size_x):152 matrix [x, 0] = x153 for y in range(size_y):154 matrix [0, y] = y155 for x in range(1, size_x):156 for y in range(1, size_y):157 if seq1[x-1] == seq2[y-1]:158 matrix [x,y] = min(159 matrix[x-1, y] + 1,160 matrix[x-1, y-1],161 matrix[x, y-1] + 1162 )163 else:164 matrix [x,y] = min(165 matrix[x-1,y] + 1,166 matrix[x-1,y-1] + 1,167 matrix[x,y-1] + 1168 )169 return (matrix[size_x - 1, size_y - 1])170def compute_sift4_(seq1, seq2, max_offset, max_distance=0):171 '''172 Computes the distance between seq1 and seq2.173 Params:174 seq1: first string to compare175 seq2: second string to compare176 max_offset: number of positions of search for matching tokens177 max_distance: maximum distance between the two strings before giving up178 '''179 # Handle cases where seq1 or seq2 are invalid (None or empty)180 if seq1 is None or len(seq1) == 0:181 if seq2 is None:182 return 0183 return len(seq2)184 if seq2 is None or len(seq2) == 0:185 return len(seq1)186 # General case: both strings are valid and comparable.187 # var l1=s1.length;188 # var l2=s2.length;189 len_seq1 = len(seq1)190 len_seq2 = len(seq2)191 c_1 = 0192 c_2 = 0193 lcss = 0 # largest common subsequence194 local_cs = 0 # local common substring195 trans = 0 # number of transpositions ('ab' vs 'ba')196 offset_arr = [] # offset object: { "c1": c1, "c2": c2, "trans": trans }197 while (c_1 < len_seq1) and (c_2 < len_seq2):198 if seq1[c_1] == seq2[c_2]:199 local_cs += 1200 is_trans = False201 i = 0202 while i < len(offset_arr): # see if current match is a transposition203 offset = offset_arr[i]204 if c_1 <= offset['c1'] or c_2 <= offset['c2']:205 # when two matches cross, the one considered a transposition is the one with the largest difference in offsets206 is_trans = abs(c_2 - c_1) >= abs(offset['c2'] - offset['c1'])207 if is_trans:208 trans += 1209 else:210 if not offset['trans']:211 offset['trans'] = True212 trans += 1;213 break214 else:215 if c_1 > offset['c2'] and c_2 > offset['c1']:216 offset_arr.pop(0)217 else:218 i += 1219 offset_arr.append({220 'c1': c_1,221 'c2': c_2,222 'trans': is_trans,223 })224 else:225 lcss += local_cs226 local_cs = 0227 if c_1 != c_2:228 c_1 = min(c_1,c_2)229 c_2 = min(c_1,c_2) # using min allows the computation of transposition230 # if matching characters are found, remove 1 from both cursors (they get incremented at the end of the loop)231 # so that we can have only one code block handling matches232 while i < max_offset and (c_1 + i < len_seq1 or c_2 + i < len_seq2):233 if (c_1 + i < len_seq1) and (seq1[c_1 + i] == seq2[c_2]):234 c_1 += i - 1235 c_2 -= 1236 break237 if (c_2 + i < len_seq2) and (seq1[c_1] == seq2[c_2 + i]):238 c_1 -= 1239 c_2 += i - 1240 break241 i += 1242 c_1 += 1243 c_2 += 1244 if max_distance > 0:245 temporary_dist = max(c_1,c_2) - (lcss - trans)246 if temporary_dist >= max_distance:247 return round(temporary_dist)248 # this covers the case where the last match is on the last token in list, so that it can compute transpositions correctly249 if (c_1 >= len_seq1) or (c_2 >= len_seq2):250 lcss += local_cs251 local_cs = 0252 c_1 = min(c_1, c_2)253 c_2 = min(c_1,c_2)254 lcss += local_cs255 return round(max(len_seq1, len_seq2) - (lcss - trans)) # remove half the number of transpositions from the lcss256def should_use_matched_(url1, url2):257 '''Checks if the given URLs have the same netloc.'''258 parsed_url1 = urlparse(url1)259 parsed_url2 = urlparse(url2)260 url1_path = parsed_url1.path261 url1_tokens = url1_path.split('/')262 num_url1_tokens = len(url1_tokens)263 url2_path = parsed_url2.path264 url2_tokens = url2_path.split('/')265 num_url2_tokens = len(url2_tokens)266 # return parsed_url1.netloc == parsed_url2.netloc and \267 # num_url1_tokens == num_url2_tokens and \268 # abs(len(url1_tokens[-1]) - len(url2_tokens[-1])) <= 2269 return parsed_url1.netloc == parsed_url2.netloc and \270 num_url1_tokens == num_url2_tokens271def remove_query_(parsed_url):272 '''Removes the query from the URL.'''273 retval = '{scheme}://{netloc}{path}'.format(274 scheme=parsed_url.scheme,275 netloc=parsed_url.netloc,276 path=get_path_(parsed_url))277 return retval278def get_path_(parsed_url):279 '''Returns the path and parameter.'''280 retval = parsed_url.path281 if parsed_url.params != '':282 retval += ';' + parsed_url.params283 return retval284def last_path_token_match_(parsed_u, parsed_url_to_match):285 '''Checks whether the last token of the path (ignoring the params) match.'''286 parsed_url_to_match_last_path_token = parsed_url_to_match.path.split('/')[-1]287 parsed_u_last_path_token = parsed_u.path.split('/')[-1]...

Full Screen

Full Screen

file_manager.py

Source:file_manager.py Github

copy

Full Screen

...27 28 29 def delete(self):30 for file_name in self.get_delete_candidates_():31 file_path = self.get_path_(file_name, self.dst_dir, self.dst_extension)32 try:33 file_path.unlink()34 self.dst_file_names.remove(file_name)35 except FileNotFoundError as error:36 print(f'Error: file "{file_path}" does not found')37 finally:38 continue39 40 def get_path_(self, file_name, root_dir, extension):41 return root_dir / (file_name + '.' + extension)42 43 # def get_delete_candidates_(self):44 # '''45 # Doesnot work when src and dst is the same.46 # '''47 # for file_name in self.dst_file_names:48 # if file_name not in self.src_file_names:49 # yield file_name50 51 def get_delete_candidates_(self):52 candidates = []53 for file_name in self.dst_file_names:54 if file_name not in self.src_file_names:55 candidates.append(file_name)56 return candidates57 58 59 def random_copy(self, annot_dir, image_dir, ratio=0.2):60 file_names = list(self.src_file_names)61 62 count = math.ceil(ratio * len(file_names))63 indexes = np.random.permutation(len(file_names))64 indexes = indexes[:count]65 for i in indexes:66 annot_file_path = self.get_path_(file_names[i], self.src_dir, self.src_extension)67 img_file_path = self.get_path_(file_names[i], self.dst_dir, self.dst_extension)68 69 copyfile(annot_file_path, annot_dir / annot_file_path.name)...

Full Screen

Full Screen

test_com.py

Source:test_com.py Github

copy

Full Screen

...3 file_name = 'test.csv'4 sub_dir = 'test_data'5 path = os.path.join(pathlib.Path(__file__).parent,sub_dir,file_name)6 return path7def get_path_(base_path:str,file_name:str='',sub_dir:str=''):8 import pathlib,os9 file_name = 'test.csv'10 sub_dir = 'test_data'11 if sub_dir != '':12 path = os.path.join(pathlib.Path(base_path).parent,sub_dir,file_name)13 else:14 path = os.path.join(pathlib.Path(base_path).parent,file_name)...

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 autotest 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