Best Python code snippet using uiautomator
diamonds_20200104_1110.py
Source:diamonds_20200104_1110.py
1F1 = {'Name': 'O', 'Number': 1, 'Color': 'Rose',2 'Height': 1, 'Width': 5, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1, 1, 1]],3 'Options': [[0, False], [90, False]]}4F2 = {'Name': 'Q', 'Number': 2, 'Color': 'Orange',5 'Height': 2, 'Width': 4, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1, 1], [1, 0, 0, 0]],6 'Options': [[0, False], [90, False], [180, False], [270, False], [0, True], [90, True], [180, True], [270, True]]}7F3 = {'Name': 'Y', 'Number': 3, 'Color': 'Brown',8 'Height': 2, 'Width': 4, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1, 1], [0, 1, 0, 0]],9 'Options': [[0, False], [90, False], [180, False], [270, False], [0, True], [90, True], [180, True], [270, True]]}10F4 = {'Name': 'S', 'Number': 4, 'Color': 'Black',11 'Height': 2, 'Width': 4, 'Angle': 0, 'Mirror': False, 'Shape': [[0, 1, 1, 1], [1, 1, 0, 0]],12 'Options': [[0, False], [90, False], [180, False], [270, False], [0, True], [90, True], [180, True], [270, True]]}13F5 = {'Name': 'V', 'Number': 5, 'Color': 'Dark Blue',14 'Height': 3, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1], [1, 0, 0], [1, 0, 0]],15 'Options': [[0, False], [90, False], [180, False], [270, False]]}16F6 = {'Name': 'P', 'Number': 6, 'Color': 'Light Blue',17 'Height': 2, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1], [1, 1, 0]],18 'Options': [[0, False], [90, False], [180, False], [270, False], [0, True], [90, True], [180, True], [270, True]]}19F7 = {'Name': 'U', 'Number': 7, 'Color': 'Yellow',20 'Height': 2, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1], [1, 0, 1]],21 'Options': [[0, False], [90, False], [180, False], [270, False]]}22F8 = {'Name': 'Z', 'Number': 8, 'Color': 'Grey',23 'Height': 3, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 0, 0], [1, 1, 1], [0, 0, 1]],24 'Options': [[0, False], [90, False], [0, True], [90, True]]}25F9 = {'Name': 'R', 'Number': 9, 'Color': 'Light Green',26 'Height': 3, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 0, 0], [1, 1, 1], [0, 1, 0]],27 'Options': [[0, False], [90, False], [180, False], [270, False], [0, True], [90, True], [180, True], [270, True]]}28FA = {'Name': 'T', 'Number': 10, 'Color': 'Dark Green',29 'Height': 3, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 1], [0, 1, 0], [0, 1, 0]],30 'Options': [[0, False], [90, False], [180, False], [270, False]]}31FB = {'Name': 'W', 'Number': 11, 'Color': 'Cherry',32 'Height': 3, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[1, 1, 0], [0, 1, 1], [0, 0, 1]],33 'Options': [[0, False], [90, False], [180, False], [270, False]]}34FC = {'Name': 'X', 'Number': 12, 'Color': 'Red',35 'Height': 3, 'Width': 3, 'Angle': 0, 'Mirror': False, 'Shape': [[0, 1, 0], [1, 1, 1], [0, 1, 0]],36 'Options': [[0, False]]}37ARR = [F1, F2, F3, F4, F5, F6, F7, F8, F9, FA, FB, FC]38def rotate(source: dict, angle: int) -> dict:39 result = {'Name': source['Name'], 'Number': source['Number'], 'Color': source['Color'],40 'Height': source['Height'], 'Width': source['Width'], 'Angle': angle,41 'Mirror': source['Mirror'], 'Shape': source['Shape']}42 if angle == 90 or angle == -270:43 result['Height'] = source['Width']44 result['Width'] = source['Height']45 result['Shape'] = [[0 for i in range(result['Width'])] for j in range(result['Height'])]46 for j in range(source['Height']):47 for i in range(source['Width']):48 result['Shape'][i][result['Width'] - 1 - j] = source['Shape'][j][i]49 elif angle == 180 or angle == -180:50 result['Shape'] = [[0 for i in range(result['Width'])] for j in range(result['Height'])]51 for j in range(source['Height']):52 for i in range(source['Width']):53 result['Shape'][result['Height'] - 1 - j][result['Width'] - 1 - i] = source['Shape'][j][i]54 elif angle == 270 or angle == -90:55 result['Height'] = source['Width']56 result['Width'] = source['Height']57 result['Shape'] = [[0 for i in range(result['Width'])] for j in range(result['Height'])]58 for j in range(source['Height']):59 for i in range(source['Width']):60 result['Shape'][result['Height'] - 1 - i][j] = source['Shape'][j][i]61 return result62def mirror(source: dict) -> dict:63 result = {'Name': source['Name'], 'Number': source['Number'], 'Color': source['Color'],64 'Height': source['Height'], 'Width': source['Width'], 'Angle': source['Angle'],65 'Mirror': not(source['Mirror']), 'Shape': source['Shape']}66 result['Shape'] = [[0 for i in range(result['Width'])] for j in range(result['Height'])]67 for j in range(source['Height']):68 for i in range(source['Width']):69 result['Shape'][j][result['Width'] - 1 - i] = source['Shape'][j][i]70 return result71def multiprint(source: dict):72 caption = '#' + str(source['Number']) + ' - ' + source['Name'] + ' - ' + str(source['Angle']) + ' - ' + str(source['Mirror'])73 print(caption)74 print(''.join(['-' for i in range(len(caption))]))75 for j in range(source['Height']):76 print(' '.join(map(str,source['Shape'][j])))77 print(''.join(['-' for i in range(len(caption))]))78def print_shape(shape: list):79 print(''.join(['-' for i in range(2* len(shape[0]) - 1)]))80 for j in range(len(shape)):81 print(' '.join(map(str,shape[j])))82 #print(''.join(['-' for i in range(2 * len(shape[0]) - 1)]))83def orientate(task: dict) -> dict:84 l = []85 c = 086 f = 087 for figure in task['Figures']:88 l.append([])89 for option in figure['Options']:90 ready_figure = rotate(mirror(figure) if option[1] else figure, option[0])91 if task['Width'] < ready_figure['Width'] or task['Height'] < ready_figure['Height']:92 continue93 #multiprint(ready_figure)94 for j in range(task['Height'] - ready_figure['Height'] + 1):95 for i in range(task['Width'] - ready_figure['Width'] + 1):96 field = [[0 for a in range(task['Width'])] for b in range(task['Height'])]97 for n in range(ready_figure['Height']):98 for m in range(ready_figure['Width']):99 field[j+n][i+m] = ready_figure['Shape'][n][m]100 #print_shape(field)101 checker = True102 for n in range(len(field)):103 for m in range(len(field[n])):104 if field[n][m] == 0:105 checker = trace(field, m, n, m, n, 0) == 4106 if not checker:107 break108 if not checker:109 break110 if checker:111 l[f].append(field)112 c = c + 1113 f = f + 1114 return {'Height': task['Height'], 'Width': task['Width'], 'Figures': task['Figures'], 'Counter': c, 'List': l}115def merge(field_a: list, field_b: list) -> (list, bool):116 if field_a is None:117 return field_b[:], True118 elif field_b is None:119 return field_a[:], True120 else:121 result = [[0 for a in range(len(field_a[0]))] for b in range(len(field_a))]122 checker = True123 for j in range(len(field_a)):124 for i in range(len(field_a[j])):125 tmp = field_a[j][i] + field_b[j][i]126 result[j][i] = tmp127 if tmp > 1:128 checker = False129 if checker:130 for j in range(len(result)):131 for i in range(len(result[j])):132 if result[j][i] == 0:133 checker = trace(result, i, j, i, j, 0) == 4134 if not checker:135 break136 if not checker:137 break138 #test_left = result[j][i - 1] > 0 if i > 0 else True139 #test_right = result[j][i + 1] > 0 if i < len(result[j]) - 1 else True140 #test_top = result[j - 1][i] > 0 if j > 0 else True141 #test_bottom = result[j + 1][i] > 0 if j < len(result) - 1 else True142 #if test_left and test_right and test_top and test_bottom:143 # checker = False144 #if (not test_left) and test_right and test_top and test_bottom:145 # checker = trace(result, i-1, j, i, j, 1)146 return result, checker147def trace(field: list, i: int, j: int, pi: int, pj: int, r: int) -> int:148 test_left = True if i > pi else (field[j][i - 1] > 0 if i > 0 else True)149 test_right = True if i < pi else (field[j][i + 1] > 0 if i < (len(field[j]) - 1) else True)150 test_top = True if j > pj else (field[j - 1][i] > 0 if j > 0 else True)151 test_bottom = True if j < pj else (field[j + 1][i] > 0 if j < (len(field) - 1) else True)152 if r > 0:153 if test_left and test_right and test_top and test_bottom:154 return r155 elif (not test_left) and test_right and test_top and test_bottom:156 return trace(field, i - 1, j, i, j, r + 1) if r < 3 else 4157 elif test_left and (not test_right) and test_top and test_bottom:158 return trace(field, i + 1, j, i, j, r + 1) if r < 3 else 4159 elif test_left and test_right and (not test_top) and test_bottom:160 return trace(field, i, j - 1, i, j, r + 1) if r < 3 else 4161 elif test_left and test_right and test_top and (not test_bottom):162 return trace(field, i, j + 1, i, j, r + 1) if r < 3 else 4163 elif (not test_left) and (not test_right) and test_top and test_bottom:164 return max(trace(field, i - 1, j, i, j, r + 2), trace(field, i + 1, j, i, j, r + 2)) if r < 2 else 4165 elif test_left and test_right and (not test_top) and (not test_bottom):166 return max(trace(field, i, j - 1, i, j, r + 2), trace(field, i, j + 1, i, j, r + 2)) if r < 2 else 4167 elif (not test_left) and test_right and (not test_top) and test_bottom:168 return max(trace(field, i - 1, j, i, j, r + 2), trace(field, i, j - 1, i, j, r + 2)) if r < 2 else 4169 elif (not test_left) and test_right and test_top and (not test_bottom):170 return max(trace(field, i - 1, j, i, j, r + 2), trace(field, i, j + 1, i, j, r + 2)) if r < 2 else 4171 elif test_left and (not test_right) and (not test_top) and test_bottom:172 return max(trace(field, i + 1, j, i, j, r + 2), trace(field, i, j - 1, i, j, r + 2)) if r < 2 else 4173 elif test_left and (not test_right) and test_top and (not test_bottom):174 return max(trace(field, i + 1, j, i, j, r + 2), trace(field, i, j + 1, i, j, r + 2)) if r < 2 else 4175 elif (not test_left) and (not test_right) and (not test_top) and test_bottom:176 return max(trace(field, i - 1, j, i, j, r + 3), trace(field, i + 1, j, i, j, r + 3),177 trace(field, i, j - 1, i, j, r + 3)) if r < 1 else 4178 elif (not test_left) and (not test_right) and test_top and (not test_bottom):179 return max(trace(field, i - 1, j, i, j, r + 3), trace(field, i + 1, j, i, j, r + 3),180 trace(field, i, j + 1, i, j, r + 3)) if r < 1 else 4181 elif (not test_left) and test_right and (not test_top) and (not test_bottom):182 return max(trace(field, i - 1, j, i, j, r + 3), trace(field, i, j - 1, i, j, r + 3),183 trace(field, i, j + 1, i, j, r + 3)) if r < 1 else 4184 elif test_left and (not test_right) and (not test_top) and (not test_bottom):185 return max(trace(field, i + 1, j, i, j, r + 3), trace(field, i, j - 1, i, j, r + 3),186 trace(field, i, j + 1, i, j, r + 3)) if r < 1 else 4187 elif (not test_left) and (not test_right) and (not test_top) and (not test_bottom):188 return 4189 else:190 if test_left and test_right and test_top and test_bottom:191 return r192 elif (not test_left) and test_right and test_top and test_bottom:193 return trace(field, i - 1, j, i, j, r + 1) if r < 3 else 4194 elif test_left and (not test_right) and test_top and test_bottom:195 return trace(field, i + 1, j, i, j, r + 1) if r < 3 else 4196 elif test_left and test_right and (not test_top) and test_bottom:197 return trace(field, i, j - 1, i, j, r + 1) if r < 3 else 4198 elif test_left and test_right and test_top and (not test_bottom):199 return trace(field, i, j + 1, i, j, r + 1) if r < 3 else 4200 else:201 return 4202def check(field: list) -> bool:203 s = 0204 z = 0205 for j in range(len(field)):206 for i in range(len(field[j])):207 s = s + field[j][i]208 if field[j][i] == 0: z = z + 1209 return s == len(field) * len(field[0]) and z == 0210def solve(task: dict, shift: int = 0, field: list = None) -> (dict, bool):211 l = task['List']212 for c in range(len(l[shift])):213 merged, checker = merge(field, l[shift][c])214 if checker:215 if len(l) > shift + 1:216 fields, checker = solve(task, shift + 1, merged)217 if checker:218 fields[shift] = l[shift][c]219 return fields, checker220 else:221 checker = check(merged)222 if checker:223 return {shift: l[shift][c]}, checker224 return {}, False225def print_result(fields: dict):226 separator = ''.join(['-' for i in range((2 * len(fields[0][0]) - 1 + 3) * len(fields) - 3)])227 print(separator)228 for j in range(len(fields[0])):229 content = ''230 for f in range(len(fields)):231 content = content + ' '.join(map(str, fields[f][j])) + ' '232 print(content)233 print(separator)234T3A = {'Height': 5, 'Width': 3, 'Figures': [F2, F3, FA]}235T3B = {'Height': 5, 'Width': 3, 'Figures': [F4, F6, F7]}236T4A = {'Height': 5, 'Width': 4, 'Figures': [F2, F3, F6, FA]}237T5A = {'Height': 5, 'Width': 5, 'Figures': [F2, F3, F6, FA, FB]}238T6A = {'Height': 5, 'Width': 6, 'Figures': [F2, F3, F6, F8, FA, FB]}239T7A = {'Height': 5, 'Width': 7, 'Figures': [F2, F3, F5, F6, F8, FA, FB]}240T8A = {'Height': 5, 'Width': 8, 'Figures': [F2, F3, F4, F5, F6, F8, FA, FB]}241T12 = {'Height': 5, 'Width': 12, 'Figures': ARR}242T20 = {'Height': 3, 'Width': 20, 'Figures': ARR}243fields, rc = solve(orientate(T6A))244print_result(fields)245#print(F2)246#print(rotate(mirror(F2), 0))...
supervised.py
Source:supervised.py
1from sklearn.metrics import mean_absolute_error2from sklearn.metrics import mean_squared_error3from sklearn.metrics import mean_squared_log_error4from sklearn.metrics import median_absolute_error5import feature_transformation as ft6import math7import pandas as pd8# A customer rolling k-fold implementation, which is capable of cross-validating9# time series data while avoiding look-ahead bias. How to interpret parameters:10# partitions=5, window = 3. ? means training set, ! means testing set.11# These are the operations that will run.12# [?|?|!| | ]13# [?|?|?|!| ]14# [?|?|?|?|!]15# rolling_kfold will average the results of these runs and return them to you.16def rolling_kfold(data, learner, config, partitions=5, window=3):17 partition_size = data.shape[0]/partitions18 19 window_start = 020 count = 021 nbtr_final = pd.DataFrame()22 nbte_final = pd.DataFrame()23 tr_final = pd.DataFrame()24 te_final = pd.DataFrame()25 for iteration in range(0, partitions - window + 1):26 print("\nNEW ITERATION")27 left = 028 middle = window_start + partition_size*(window-1)29 right = window_start + partition_size*(window)30 training_set = data.iloc[left:middle]31 testing_set = data.iloc[(middle + 1):right]32 model, nbtr_err, nbte_err, tr_err, te_err = learn(training_set, testing_set, learner, config["kbest"], config["do_pca"], config["pca_only"])33 window_start += partition_size34 nbtr_final = nbtr_final.append(pd.DataFrame(nbtr_err, index=[0]))35 nbte_final = nbte_final.append(pd.DataFrame(nbte_err, index=[0]))36 tr_final = tr_final.append(pd.DataFrame(tr_err, index=[0]))37 te_final = te_final.append(pd.DataFrame(te_err, index=[0]))38 print("\n")39 40 print("*"*20)41 print("NEXTBUS TRAIN FINAL")42 print(nbtr_final.mean())43 print("TRAIN FINAL")44 print(tr_final.mean())45 print("NEXTBUS TEST FINAL")46 print(nbte_final.mean())47 print("TEST FINAL")48 print(te_final.mean())49 print("*"*20)50 print((nbte_final.mean() - te_final.mean())/nbte_final.mean())51def train_test_split(data, learner, config, percent_train=.80):52 top = 053 middle = int(data.shape[0]*percent_train)54 bottom = data.shape[0]55 training_set = data.iloc[top:middle,:]56 testing_set = data.iloc[middle + 1:bottom,:]57 model, nbtr_err, nbte_err, tr_err, te_err = learn(training_set, testing_set, learner, config["kbest"], config["do_pca"], config["pca_only"])58 print("*"*20)59 print("NEXTBUS TRAIN FINAL")60 print(nbtr_err)61 print("TRAIN FINAL")62 print(tr_err)63 print("NEXTBUS TEST FINAL")64 print(nbte_err)65 print("TEST FINAL")66 print(te_err)67 print("*"*20)68 return model69def learn(train, test, learner, kbest, do_pca, pca_only):70 train = train.reset_index(drop=True)71 test = test.reset_index(drop=True)72 train_left = train.iloc[:,:-1]73 train_right = train.iloc[:,-1].reshape((-1,1))74 test_left = test.iloc[:,:-1]75 test_right = test.iloc[:,-1].reshape((-1,1))76 print("Training set size: " + str(train_left.shape[0]))77 print("Test set size: " + str(test_left.shape[0]))78 selected = ft.kbest(train_left, train_right, k=kbest)79 train_left = train_left[selected]80 test_left = test_left[selected]81 #print(selected)82 train_sta = train_left["secondsToArrival"]83 test_sta = test_left["secondsToArrival"]84 train_left, train_max, train_min = normalize(train_left)85 print("Max values from normalization:")86 print(list(train_max))87 print("Min values from normalization:")88 print(list(train_min))89 test_left, train_max, train_min = normalize(test_left, max=train_max, min=train_min)90 if do_pca and pca_only:91 train_left = ft.pca(train_left)92 test_left = ft.pca(test_left)93 elif do_pca:94 train_left = train_left.join(ft.pca(train_left))95 test_left = test_left.join(ft.pca(test_left))96 nbtr_err = error_report("NEXTBUS TRAIN ERROR", pd.DataFrame(train_right), train_sta)97 learner = learner.fit(train_left, train_right)98 guess = learner.predict(train_left)99 tr_err = error_report("TRAIN SET", train_right, guess)100 nbte_err = error_report("NEXTBUS TEST ERROR", pd.DataFrame(test_right), test_sta)101 guess = learner.predict(test_left)102 te_err = error_report("TEST SET", test_right, guess)103 return learner, nbtr_err, nbte_err, tr_err, te_err104def error_report(title, actual, predicted):105 err = {}106 print("**" + title + "**")107 err["mae"] = mean_absolute_error(actual, predicted)108 err["rmse"] = math.sqrt(mean_squared_error(actual, predicted))109 err["medae"] = median_absolute_error(actual, predicted)110 print("MAE: " + str(err["mae"]))111 print("RMSE: " + str(err["rmse"]))112 print("MEDAE: " + str(err["medae"]))113 return err114def normalize(df, min=None, max=None):115 if min is None:116 min = df.min(axis=0)117 if max is None:118 max = df.max(axis=0)119 temp = max - min120 temp[temp == 0] = 1121 df -= min122 df /= temp...
conftest.py
Source:conftest.py
1#!/usr/bin/env python32import pytest3@pytest.fixture4def rcssserver_output():5 return '''rcssserver-15.2.26Copyright (C) 1995, 1996, 1997, 1998, 1999 Electrotechnical Laboratory.72000 - RoboCup Soccer Simulator Maintenance Group.8Simulator Random Seed: 14693359419CSVSaver: Ready10STDOutSaver: Ready11Using simulator's random seed as Hetero Player Seed: 146933594112Hit CTRL-C to exit13Starting "/bin/sh -c left_team.sh"14Waiting for players to connect15A new (v14) player (test_left 1) connected.16Starting "/bin/sh -c right_team.sh"17A new (v14) player (test_right 1) connected.18A new (v14) player (test_left 2) connected.19A new (v14) player (test_left 3) connected.20A new (v14) player (test_left 4) connected.21A new (v14) player (test_left 5) connected.22A new (v14) player (test_left 6) connected.23A new (v14) player (test_left 7) connected.24A new (v14) player (test_left 8) connected.25A new (v14) player (test_left 9) connected.26A new (v14) player (test_right 2) connected.27A new (v14) player (test_left 10) connected.28A new (v14) online coach (test_left) connected.29A new (v14) player (test_left 11) connected.30A new (v14) player (test_right 3) connected.31A new (v14) player (test_right 4) connected.32A new (v14) player (test_right 5) connected.33A new (v14) player (test_right 6) connected.34A new (v14) player (test_right 7) connected.35A new (v14) player (test_right 8) connected.36A new (v14) player (test_right 9) connected.37A new (v14) player (test_right 10) connected.38A new (v14) online coach (test_right) connected.39Waiting to kick off40Kick_off_left41Waiting after end of match42A player disconnected : (test_right 4)43A player disconnected : (test_right 2)44A player disconnected : (test_right 3)45A player disconnected : (test_left 5)46A player disconnected : (test_left 7)47An online coach disconnected : (test_left)48An online coach disconnected : (test)49A player disconnected : (test_left 9)50A player disconnected : (test_right 5)51A player disconnected : (test_right 10)52A player disconnected : (test_left 11)53A player disconnected : (test_right 7)54A player disconnected : (test_left 4)55A player disconnected : (test_right 8)56A player disconnected : (test_left 8)57A player disconnected : (test_left 10)58A player disconnected : (test_right 9)59A player disconnected : (test_left 6)60A player disconnected : (test_left 2)61A player disconnected : (test_left 1)62A player disconnected : (test_right 1)63A player disconnected : (test_right 11)64A player disconnected : (test_left 3)65A player disconnected : (test_right 6)66Killing 1953967Killing 1954668Game Over. Exiting...69Saving Results:70\tCSVSaver: saving...71\tCSVSaver: ...saved72\tSTDOutSaver: saving...73Game Results:74\t2016-07-24 01:52:2175\t'test_left' vs 'test_right'76\tScore: 0 - 277\tSTDOutSaver: ...saved78Saving Results Complete...
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!!