Best Python code snippet using yandex-tank
find_path_turtle.py
Source:find_path_turtle.py
1import random2def grid_make():3 """4 A test function to generate a random grid.5 """6 grid = []7 size_x = 10008 size_y = 10009 gap = 1510 for x in range(size_x//gap):11 row = []12 for y in range(size_y//gap):13 r = random.random()14 if r <= .7:15 row.append(0)16 else:17 row.append(3)18 grid.append(row)19 indexes = [x for x in range(size_x//gap)]20 rs = indexes[random.randrange(0, len(indexes))]21 indexes.remove(rs)22 cs = indexes[random.randrange(0, len(indexes))]23 indexes.remove(cs)24 rt = indexes[random.randrange(0, len(indexes))]25 indexes.remove(rt)26 ct = indexes[random.randrange(0, len(indexes))]27 indexes.remove(ct)28 grid[rs][cs] = 129 grid[rt][ct] = 230 # for x in grid:31 # print(x)32 return grid33def establish_knowns(grid):34 """35 Function to return dictionary that has the walls, start, and stop locations.36 """37 d = {'w':set()}38 for x in range(len(grid)):39 for y in range(len(grid[x])):40 if grid[x][y] == 1:41 d["s"] = (x, y)42 elif grid[x][y] == 2:43 d["t"] = (x, y)44 elif grid[x][y] == 3:45 d["w"].add((x, y))46 return d47# class that contains a variable to help the find_path function know which "node" to prioritize.48class Node:49 def __init__(self, g, loc):50 self.g = g51 self.loc = loc52# returns the euclidean distance between two points53def calc(pA, pB):54 """returns the euclidean distance between two points"""55 return (pA[0]-pB[0])**2+(pA[1]-pB[1])**256# needs optimizations badly. VERY BADLY57def find_path(loc, path, been_to, d, grid, paths):58 """59 Recursively returns the paths to target location borrowing a simple heuristic from popular path finding algorithms which is the euclidean distance. 60 This function isn't the best or efficient but it works and displays a fun gui.61 """62 if len(paths) == 0 and loc == d['t'] and tuple(path) not in paths:63 path.append(loc)64 paths.add(tuple(path))65 return True66 elif len(paths) > 0 and len(min(paths, key=len)) > len(path) and loc == d['t'] and tuple(path) not in paths:67 # print(len(path), len(paths))68 path.append(loc)69 paths.add(tuple(path))70 return True71 elif len(paths) == 0 or len(path) < len(min(paths, key=len)) and tuple(path) not in paths:72 been_to.add(loc)73 path.append(loc)74 options = []75 if loc[0] + 1 < len(grid) and 0 <= loc[1] < len(grid[loc[0]]) and (loc[0]+1, loc[1]) not in been_to and (loc[0]+1, loc[1]) not in d['w']:76 n = Node(calc((loc[0]+1, loc[1]), d['t']), (loc[0]+1, loc[1]))77 options.append(n)78 if loc[0] - 1 >= 0 and 0 <= loc[1] < len(grid[loc[0]]) and (loc[0] - 1, loc[1]) not in been_to and (loc[0]-1, loc[1]) not in d['w']:79 n = Node(calc((loc[0]-1, loc[1]), d['t']), (loc[0]-1, loc[1]))80 options.append(n)81 if loc[1] + 1 < len(grid[0]) and 0 <= loc[0] < len(grid) and (loc[0], loc[1] + 1) not in been_to and (loc[0], loc[1] + 1) not in d['w']:82 n = Node(calc((loc[0], loc[1]+1), d['t']), (loc[0], loc[1]+1))83 options.append(n)84 if loc[1] - 1 >= 0 and 0 <= loc[0] < len(grid) and (loc[0], loc[1] - 1) not in been_to and (loc[0], loc[1] - 1) not in d['w']:85 n = Node(calc((loc[0], loc[1]-1), d['t']), (loc[0], loc[1]-1))86 options.append(n)87 if len(options) == 0 or (len(paths) > 0 and len(path) > len(min(paths, key=len))) or tuple(path) in paths:88 return False89 options.sort(key=lambda x: x.g)90 # for x in options:91 # print(x.g)92 # print('--------------')93 # print(len(options))94 for x in options:95 been_to_copy = been_to.copy()96 find_path(x.loc, path[:], been_to_copy, d, grid, paths)97class vertex:98 def __init__(self, weight, loc):99 self.weight = weight100 self.loc = loc101 self.visited = False102def Dijkstras(grid, info):103 nodes = [ [ vertex(float('inf'), (r, c)) for c in range(len(grid[r])) ] for r in range(len(grid)) ]104 path = {}105 vertexes = []106 for _ in nodes:107 for v in _:108 path[v.loc] = []109 vertexes.append(v)110 vertexes[(info['s'][0])*len(grid)+(info['s'][1])].weight = 0111 # print("start", vertexes[(info['s'][0])*len(grid)+(info['s'][1])].loc)112 # print("length of list", len(vertexes))113 while True:114 # print(list(map(lambda x: x.loc, path)))115 _min = vertex(float('inf'), None)116 i = 0117 for v in range(len(vertexes)):118 if _min.weight > vertexes[v].weight:119 _min = vertexes[v]120 i = v121 122 123 if (_min.loc == info['t']):124 print("DONE")125 break126 if not (_min.loc[0] + 1 >= len(grid)) and not vertexes[(_min.loc[0] + 1)*len(grid) + (_min.loc[1])].visited and vertexes[(_min.loc[0] + 1)*len(grid) + (_min.loc[1])].loc not in info['w']:127 # print(1, vertexes[(_min.loc[0] + 1)*len(grid) + (_min.loc[1])].loc)128 path[vertexes[(_min.loc[0] + 1)*len(grid) + (_min.loc[1])].loc].append(_min.loc)129 vertexes[(_min.loc[0] + 1)*len(grid) + (_min.loc[1])].weight = min(vertexes[(_min.loc[0] + 1)*len(grid) + (_min.loc[1])].weight, _min.weight + calc(info['s'], (_min.loc[0] + 1, _min.loc[1])))130 if not (_min.loc[0] - 1 < 0) and not vertexes[(_min.loc[0] - 1)*len(grid) + (_min.loc[1])].visited and vertexes[(_min.loc[0] - 1)*len(grid) + (_min.loc[1])].loc not in info['w']:131 # print(2, vertexes[(_min.loc[0] - 1)*len(grid) + (_min.loc[1])].loc)132 path[vertexes[(_min.loc[0] - 1)*len(grid) + (_min.loc[1])].loc].append(_min.loc)133 vertexes[(_min.loc[0] - 1)*len(grid) + (_min.loc[1])].weight = min(vertexes[(_min.loc[0] - 1)*len(grid) + (_min.loc[1])].weight, _min.weight + calc(info['s'], (_min.loc[0] - 1, _min.loc[1])))134 if not (_min.loc[1] + 1 >= len(grid[0])) and not vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] + 1)].visited and vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] + 1)].loc not in info['w']:135 # print(3, vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] + 1)].loc)136 path[vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] + 1)].loc].append(_min.loc)137 vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] + 1)].weight = min(vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] + 1)].weight, _min.weight + calc(info['s'], (_min.loc[0], _min.loc[1] + 1)))138 139 if not (_min.loc[1] - 1 < 0) and not vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] - 1)].visited and vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] - 1)].loc not in info['w']:140 # print(4, vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] - 1)].loc)141 path[vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] - 1)].loc].append(_min.loc)142 vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] - 1)].weight = min(vertexes[(_min.loc[0])*len(grid) + (_min.loc[1] - 1)].weight, _min.weight + calc(info['s'], (_min.loc[0], _min.loc[1] - 1)))143 vertexes[i].weight = float('inf')144 vertexes[i].visited = True145 journey = [info['t']]146 curr = path[info['t']][0]147 while True:148 if curr == info['s']:149 journey.append(curr)150 break151 journey.append(curr)152 curr = path[curr][0]153 return journey[::-1]154 155 156 157if __name__ == "__main__":158 grid = grid_make()159 print(len(grid), len(grid[0]))160 for x in grid:161 print(x)162 d = establish_knowns(grid)163 print("start: ", d['s'], "end: ", d['t'])164 # paths = set()165 # been_to = set()166 # find_path(d['s'], [], been_to, d, grid, paths)167 paths = Dijkstras(grid, d)...
minmax_normalization.py
Source:minmax_normalization.py
1"""2 MinMaxNormalization3"""4from __future__ import print_function5import numpy as np6np.random.seed(1337) # for reproducibility789class MinMaxNormalization(object):10 '''MinMax Normalization --> [-1, 1]11 x = (x - min) / (max - min).12 x = x * 2 - 113 '''1415 def __init__(self):16 pass1718 def fit(self, X):19 self._min = X.min()20 self._max = X.max()21 print("min:", self._min, "max:", self._max)2223 def transform(self, X):24 X = 1. * (X - self._min) / (self._max - self._min)25 X = X * 2. - 1.26 return X2728 def fit_transform(self, X):29 self.fit(X)30 return self.transform(X)3132 def inverse_transform(self, X):33 X = (X + 1.) / 2.34 X = 1. * X * (self._max - self._min) + self._min35 return X363738class MinMaxNormalization_01(object):39 '''MinMax Normalization --> [0, 1]40 x = (x - min) / (max - min).41 '''4243 def __init__(self):44 pass4546 def fit(self, X):47 self._min = X.min()48 self._max = X.max()49 print("min:", self._min, "max:", self._max)5051 def transform(self, X):52 X = 1. * (X - self._min) / (self._max - self._min)53 return X5455 def fit_transform(self, X):56 self.fit(X)57 return self.transform(X)5859 def inverse_transform(self, X):60 X = 1. * X * (self._max - self._min) + self._min
...
preprocessing.py
Source:preprocessing.py
1import numpy as np2class MinMaxNormalization01(object):3 def __init__(self, ):4 pass5 def fit(self, data):6 self._min = np.amin(data)7 self._max = np.amax(data)8 print("min: ", self._min, "max:", self._max)9 def transform(self, data):10 norm_data = 1. * (data - self._min) / (self._max - self._min)11 return norm_data12 def fit_transform(self, data):13 self.fit(data)14 return self.transform(data)15 def inverse_transform(self, data):16 inverse_norm_data = 1. * data * (self._max - self._min) + self._min17 return inverse_norm_data18 def real_loss(self, loss):19 # loss is rmse20 return loss*(self._max - self._min)21 #return real_loss22class MinMaxNormalization_neg_1_pos_1(object):23 def __init__(self):24 pass25 def fit(self, X):26 self._min = X.min()27 self._max = X.max()28 print("min:", self._min, "max:", self._max)29 def transform(self, X):30 X = 1. * (X - self._min) / (self._max - self._min)31 X = X * 2. - 1.32 return X33 def fit_transform(self, X):34 self.fit(X)35 return self.transform(X)36 def inverse_transform(self, X):37 X = (X + 1.)/2.38 X = 1. * X * (self._max - self._min) + self._min39 return X40 def real_loss(self, loss):41 # loss is rmse42 return loss*(self._max - self._min)/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!!