Best Python code snippet using autotest_python
beacon_scanner.py
Source:beacon_scanner.py
...78 >>> space_object = load_data("test_file2.txt")79 >>> for i in range(len(space_object.scanner_list)):80 ... for j in range(len(space_object.scanner_list)):81 ... if i != j:82 ... found, mapping, orientation, translation = space_object.scanner_list[i].determine_relationship(space_object.scanner_list[j])83 ... if found:84 ... space_object.mapping_dict[(space_object.scanner_list[i].identifier, space_object.scanner_list[j].identifier)] = mapping85 ... space_object.orientation_dict[(space_object.scanner_list[i].identifier, space_object.scanner_list[j].identifier)] = orientation86 ... space_object.translation_dict[(space_object.scanner_list[i].identifier, space_object.scanner_list[j].identifier)] = translation87 >>> space_object.find_orientations_and_translations_to_first_scanner()88 >>> for i in range(1, len(space_object.scanner_list)):89 ... assert (space_object.scanner_list[0].identifier, space_object.scanner_list[i].identifier) in space_object.orientation_dict90 ... assert (space_object.scanner_list[0].identifier, space_object.scanner_list[i].identifier) in space_object.translation_dict91 >>> assert space_object.orientation_dict[(space_object.scanner_list[0].identifier, space_object.scanner_list[2].identifier)] == [-2, -3, 1]92 >>> assert space_object.translation_dict[(space_object.scanner_list[0].identifier, space_object.scanner_list[2].identifier)] == [20, 1133, -1061]93 """94 for i in range(1, len(self.scanner_list)):95 if (self.scanner_list[0].identifier, self.scanner_list[i].identifier) in self.orientation_dict:96 continue97 else:98 orientation_path = self.find_path(self.scanner_list[0].identifier, self.scanner_list[i].identifier,99 self.orientation_dict)[1]100 orientation = self.orientation_dict[(orientation_path[-2], orientation_path[-1])]101 translation = self.translation_dict[(orientation_path[-2], orientation_path[-1])]102 for j in range(len(orientation_path)-2, 0, -1):103 orientation = apply_orientation(orientation, self.orientation_dict[(orientation_path[j-1], orientation_path[j])])104 translation = apply_orientation(translation, self.orientation_dict[(orientation_path[j-1], orientation_path[j])])105 translation_to_apply_to_translation = [-self.translation_dict[(orientation_path[j-1], orientation_path[j])][k] for k in106 range(len(self.translation_dict[(orientation_path[j-1], orientation_path[j])]))]107 translation = apply_translation(translation, translation_to_apply_to_translation)108 self.orientation_dict[(self.scanner_list[0].identifier, self.scanner_list[i].identifier)] = orientation109 self.translation_dict[(self.scanner_list[0].identifier, self.scanner_list[i].identifier)] = translation110 def recalibrate(self):111 """112 Recalibrates positions of all beacons with respect to first scanner113 Example:114 >>> space_object = load_data("test_file2.txt")115 >>> for i in range(len(space_object.scanner_list)):116 ... for j in range(len(space_object.scanner_list)):117 ... if i != j:118 ... found, mapping, orientation, translation = space_object.scanner_list[i].determine_relationship(space_object.scanner_list[j])119 ... if found:120 ... space_object.mapping_dict[(space_object.scanner_list[i].identifier, space_object.scanner_list[j].identifier)] = mapping121 ... space_object.orientation_dict[(space_object.scanner_list[i].identifier, space_object.scanner_list[j].identifier)] = orientation122 ... space_object.translation_dict[(space_object.scanner_list[i].identifier, space_object.scanner_list[j].identifier)] = translation123 >>> space_object.find_orientations_and_translations_to_first_scanner()124 >>> space_object.recalibrate()125 """126 first_scanner = self.scanner_list[0]127 for scanner in self.scanner_list:128 if scanner == first_scanner:129 for beacon in scanner.beacon_list:130 self.beacons_wrt_first_scanner.add((beacon[0], beacon[1], beacon[2]))131 else:132 for beacon in scanner.beacon_list:133 new_beacon = copy.deepcopy(beacon)134 new_beacon = apply_orientation(new_beacon, self.orientation_dict[(first_scanner.identifier, scanner.identifier)])135 new_beacon = apply_translation(new_beacon, self.translation_dict[(first_scanner.identifier, scanner.identifier)])136 self.beacons_wrt_first_scanner.add((new_beacon[0], new_beacon[1], new_beacon[2]))137 return138 def count_beacons(self):139 """140 Counts the total number of beacons scanned by all scanners141 Example:142 """143 # For beacon1 in scanner1144 # For beacon2 in scanner2145 for i in range(len(self.scanner_list)):146 for j in range(len(self.scanner_list)):147 # Ensuring the two scanners are not the same148 if i != j:149 # Get mapping of all nodes with one other node.150 found, mapping, orientation, translation = self.scanner_list[i].determine_relationship(self.scanner_list[j])151 if found:152 # print(mapping)153 self.mapping_dict[(self.scanner_list[i].identifier, self.scanner_list[j].identifier)] = mapping154 self.orientation_dict[(self.scanner_list[i].identifier, self.scanner_list[j].identifier)] = orientation155 self.translation_dict[(self.scanner_list[i].identifier, self.scanner_list[j].identifier)] = translation156 # break157 # Find the rotation required to orient all scanners in the same direction as the first scanner using transitivity158 # Find the translation required to move all scanners to the first scanner using the mapping of scanners and159 # transitivity160 self.find_orientations_and_translations_to_first_scanner()161 # Using the rotation and translation information for all scanners, find the beacon coordinates of all beacons with162 # respect to the first scanner adding these to a set of tuples of coordinates. First orient then translate.163 self.recalibrate()164 # for beacon in self.beacons_wrt_first_scanner:165 # print(str(beacon[0])+','+str(beacon[1])+','+str(beacon[2]))166 return len(self.beacons_wrt_first_scanner)167 def find_greatest_manhatten_distance_between_scanners(self):168 """169 Finds two scanners that are the furthest apart and returns their identifiers170 """171 self.count_beacons()172 scanner_coordinates = {self.scanner_list[0].identifier: [0, 0, 0]}173 for key, val in self.translation_dict.items():174 if key[0] == self.scanner_list[0].identifier:175 scanner_coordinates[key[1]] = val176 max_distance = 0177 first_scanner = 0178 second_scanner = 0179 for i in scanner_coordinates.keys():180 for j in scanner_coordinates.keys():181 if manhattan_distance(scanner_coordinates[i], scanner_coordinates[j])>max_distance:182 max_distance = manhattan_distance(scanner_coordinates[i], scanner_coordinates[j])183 first_scanner = i184 second_scanner = j185 # print(first_scanner)186 # print(second_scanner)187 return max_distance188class Scanner:189 def __init__(self, identifier, beacon_list):190 self.identifier = identifier191 self.beacon_list = copy.deepcopy(beacon_list)192 self.relative_distances_per_beacon_list = []193 for beacon1 in beacon_list:194 self.relative_distances_per_beacon_list.append([])195 for beacon2 in beacon_list:196 self.relative_distances_per_beacon_list[-1].append(manhattan_distance(beacon1, beacon2))197 def __str__(self):198 """199 Prints string form of this class.200 Example:201 >>> print(Scanner(0, [[404, -588, -901], [528, -643, 409], [-838, 591, 734]]))202 Scanner 0:203 0: [ 404,-588,-901] [0: 0, 1: 1489, 2: 4056, ]204 1: [ 528,-643, 409] [0: 1489, 1: 0, 2: 2925, ]205 2: [-838, 591, 734] [0: 4056, 1: 2925, 2: 0, ]206 <BLANKLINE>207 """208 assert len(self.beacon_list) == len(self.relative_distances_per_beacon_list)209 returning_string = "Scanner " + str(self.identifier) + ":\n"210 for i in range(len(self.beacon_list)):211 returning_string += str(i).rjust(2) + ": [" + \212 str(self.beacon_list[i][0]).rjust(4) + "," + \213 str(self.beacon_list[i][1]).rjust(4) + "," + \214 str(self.beacon_list[i][2]).rjust(4) + "]"215 returning_string += " ["216 for j in range(len(self.relative_distances_per_beacon_list[i])):217 returning_string += str(j) + ": " + str(self.relative_distances_per_beacon_list[i][j]).rjust(4) + ", "218 returning_string += "]\n"219 return returning_string220 def determine_relationship(self, scanner_object):221 """222 Determines mapping of beacons, the orientation and translation relation between the two scanners.223 Example:224 >>> space_object = load_data("test_file1.txt")225 >>> space_object.scanner_list[0].determine_relationship(space_object.scanner_list[1])226 (True, {1: 8, 3: 12, 4: 1, 5: 24, 6: 18, 7: 10, 9: 0, 12: 2, 14: 5, 19: 15, 24: 19, 0: 3}, [-1, 2, -3], [-68, 1246, 43])227 """228 assert isinstance(scanner_object, Scanner)229 beacon_mapping = self.find_mapping(scanner_object)230 orientation_relation = []231 translation_relation = []232 if len(beacon_mapping) > 11:233 # Find orientation relation234 orientation_relation = self.find_orientation(scanner_object, beacon_mapping)235 # Find translation relation236 translation_relation = self.find_translation(scanner_object, beacon_mapping, orientation_relation)237 return True, beacon_mapping, orientation_relation, translation_relation238 else:239 return False, beacon_mapping, orientation_relation, translation_relation...
20_part_1.py
Source:20_part_1.py
...79 results.append(clock_wise)80 results.append(counter)81 for i in list(set(results)):82 self.possible_arr.append(TileArrangement(self, i))83 def determine_relationship(self, candidate):84 for i, my in enumerate(self.possible_arr):85 for j, their in enumerate(candidate.possible_arr):86 if '' in my.arr_list or '' in their.arr_list:87 import pdb;pdb.set_trace()88 # Is above us?89 if my.arr_list[0] == their.arr_list[-1]:90 my.neighbours['up'] = their91 their.neighbours['down'] = my92 # Check whether it is on the bottom:93 if my.arr_list[-1] == their.arr_list[0]:94 my.neighbours['down'] = their95 their.neighbours['up'] = my96 # Check whether it is on the left:97 if Tile.get_left_side(my.arr_list) == Tile.get_right_side(their.arr_list):98 my.neighbours['left'] = their99 their.neighbours['right'] = my100 # Check whether it is on the right:101 if Tile.get_right_side(my.arr_list) == Tile.get_left_side(their.arr_list):102 my.neighbours['right'] = their103 their.neighbours['left'] = my104 return None105def print_board(board):106 for line in board:107 cur_row = ""108 for i, tile in enumerate(line):109 cur_row += str(tile.name) + " " * 11110 cur_row += "\n"111 for i in range(10):112 for t in line:113 cur_row += t.final_arr.arr_list[i] + " " * 5114 cur_row += "\n"115 print(cur_row)116tiles = input.split("\n\n")117tile_dict = {}118for t in tiles:119 tile_number, tile_text = t.split('\n', 1)120 tile_obj = Tile(int(tile_number[4:9]), tile_text)121 tile_dict[int(tile_number[4:9])] = tile_obj122 tile_obj.generate_possibilities()123square = []124square_size = int(len(tile_dict) ** 0.5)125for i in range(square_size):126 square.append([None] * square_size)127#### TEST print board - Randomly put everything into a square:128# for i, j in enumerate(tile_dict.values()):129# square[i // 3][i % 3] = j130# j.final_arr = j.possible_arr[0]131#132# print_board(square)133#134for tile_name, tile_obj in tile_dict.items():135 for can_name, can_obj in tile_dict.items():136 if can_obj.was_scanned:137 continue138 if can_obj == tile_obj:139 continue140 relationship = tile_obj.determine_relationship(can_obj)141 tile_obj.was_scanned = True142# ttt = tile_dict[1951]143#144# for i in ttt.possible_arr:145# for k, v in i.neighbours.items():146# if v != None:147# print(k, '->', v.tile.name)148#149# print('-------------')150#151# find top left:152top_left = None153possible_corners = []154for tile_obj in tile_dict.values():...
style_engine.py
Source:style_engine.py
...53 rows.append([e_id, l_id, score, popularity])54 db.create_pairs(rows)55def determine_score(pair):56 wears = (pair[e_wear_index] + pair[l_wear_index]) / 257 likes, dislikes = determine_relationship(pair)58 #print('likes: {0} dislikes: {1}'.format(likes, dislikes))59 likes += pair[e_like_index] + pair[l_like_index]60 dislikes += pair[e_dislike_index] + pair[l_dislike_index]61 #print('likes: {0} dislikes: {1}'.format(likes, dislikes))62 score = (likes - dislikes) / (likes + dislikes + 1)63 pop = score - (0.03 * wears)64 return score, pop65def determine_relationship(pair):66 # Go through each of the Style Rules67 # General rules for matching the eyeshadow and lipstick to each other68 positive = 069 negative = 070 71 # Do not pair a bold lip and bold eye72 if pair[e_bold_index] == 'BOLD' and pair[l_bold_index] == 'BOLD':73 negative = negative + 174 # Pair a bold eye with a subtle lip75 if pair[e_bold_index] == 'BOLD' and pair[l_bold_index] == 'NUDE':76 positive = positive + 177 # Pair a bold lip with a subtle eye78 if pair[e_bold_index] == 'SUBTLE' and pair[l_bold_index] == 'BOLD':79 positive = positive + 1...
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!!