Best Python code snippet using localstack_python
smoothing.py
Source:smoothing.py
1import pyomo.environ as pe2import matplotlib.pyplot as plt3import numpy as np4import math5class SmoothFunction(object):6 def __init__(self,7 left_fun,8 right_fun,9 disc_point,10 start,11 end):12 self._f1 = left_fun13 self._f2 = right_fun14 self._b = disc_point15 self._n_points = 5016 self._start = start17 self._end = end18 self._band = 0.0519 self._k = 1.020 def f1(self, x):21 return self._f1(x)22 def f2(self, x):23 return self._f2(x)24 def discontinuous_f(self, x):25 if x <= self._b:26 return self.f1(x)27 else:28 return self.f2(x)29 def find_k(self, tee=False):30 m = pe.ConcreteModel()31 m.k = pe.Var(initialize=1.0, bounds=(0.0, None))32 m.s = pe.Var(initialize=0.0, bounds=(0.0, None))33 n_points = self._n_points34 start = self._start + 0.6*(self._b-self._start)35 end = self._b + 0.4*(self._end-self._b)36 data_p = np.linspace(start,37 end,38 n_points)39 difference = sum(abs(self.f1(p) - self.f2(p)) for p in data_p)40 if difference > 0:41 sampled = list(map(self.discontinuous_f, data_p))42 def init_y(m, i):43 return sampled[i]44 m.y = pe.Var(range(n_points), initialize=init_y)45 m.c_list = pe.ConstraintList()46 for i, x in enumerate(data_p):47 sigma = 1.0 / (1 + pe.exp(-m.k * (x - self._b)))48 m.c_list.add(m.y[i] == (1 - sigma) * self.f1(x) + sigma * self.f2(x))49 sigma = 1.0 / (1 + pe.exp(-m.k * self._band * self._b))50 m.smooth = pe.Constraint(expr=0.05 + m.s == sigma * (1 - sigma))51 m.obj = pe.Objective(expr=sum((m.y[i] - sampled[i]) ** 2 for i in range(n_points)) + m.s**2, sense=pe.minimize)52 opt = pe.SolverFactory('ipopt')53 opt.solve(m, tee=tee)54 self._k = pe.value(m.k)55 else:56 self._k = 1.057 def __call__(self, *args, **kwargs):58 x = args[0]59 sigma = 1.0/(1.0+pe.exp(-self._k*(x-self._b)))60 return (1-sigma)*self.f1(x) + sigma*self.f2(x)61class SmoothNamedFunction(object):62 def __init__(self,63 left_fun,64 right_fun,65 disc_point,66 start,67 end,68 name):69 self._f1 = left_fun70 self._f2 = right_fun71 self._b = disc_point72 self._n_points = 5073 self._start = start74 self._end = end75 self._band = 0.0576 self._k = 1.077 self._name = name78 def f1(self, x):79 return self._f1(self._name, x)80 def f2(self, x):81 return self._f2(self._name, x)82 def discontinuous_f(self, x):83 if x <= self._b:84 return self.f1(x)85 else:86 return self.f2(x)87 def find_k(self, tee=False):88 m = pe.ConcreteModel()89 m.k = pe.Var(initialize=1.0, bounds=(0.0, None))90 m.s = pe.Var(initialize=0.0, bounds=(0.0, None))91 n_points = self._n_points92 start = self._start + 0.6*(self._b-self._start)93 end = self._b + 0.4*(self._end-self._b)94 data_p = np.linspace(start,95 end,96 n_points)97 difference = sum(abs(self.f1(p) - self.f2(p)) for p in data_p)98 if difference > 0:99 sampled = list(map(self.discontinuous_f, data_p))100 def init_y(m, i):101 return sampled[i]102 m.y = pe.Var(range(n_points), initialize=init_y)103 m.c_list = pe.ConstraintList()104 for i, x in enumerate(data_p):105 sigma = 1.0 / (1 + pe.exp(-m.k * (x - self._b)))106 m.c_list.add(m.y[i] == (1 - sigma) * self.f1(x) + sigma * self.f2(x))107 sigma = 1.0 / (1 + pe.exp(-m.k * self._band * self._b))108 m.smooth = pe.Constraint(expr=0.05 + m.s == sigma * (1 - sigma))109 m.obj = pe.Objective(expr=sum((m.y[i] - sampled[i]) ** 2 for i in range(n_points)) + m.s**2, sense=pe.minimize)110 opt = pe.SolverFactory('ipopt')111 opt.solve(m, tee=tee)112 self._k = pe.value(m.k)113 else:114 self._k = 1.0115 def __call__(self, *args, **kwargs):116 name = args[0]117 x = args[1]118 sigma = 1.0/(1.0+pe.exp(-self._k*(x-self._b)))119 return (1-sigma)*self.f1(x) + sigma*self.f2(x)120def smooth_functions(list_functions, list_points, tee=False):121 n_functions = len(list_functions)122 my_fun = list_functions[0]123 fl = list_functions[0]124 tl = list_points[0]125 for i in range(1, n_functions):126 fr = list_functions[i]127 tm = list_points[i]128 tr = list_points[i+1]129 my_fun = SmoothFunction(fl, fr, tm, tl, tr)130 my_fun.find_k(tee=tee)131 fl = my_fun132 tl = list_points[i]133 return my_fun134def smooth_named_functions(list_functions, list_points, name, tee=False):135 n_functions = len(list_functions)136 my_fun = list_functions[0]137 fl = list_functions[0]138 tl = list_points[0]139 for i in range(1, n_functions):140 fr = list_functions[i]141 tm = list_points[i]142 tr = list_points[i+1]143 my_fun = SmoothNamedFunction(fl, fr, tm, tl, tr, name)144 my_fun.find_k(tee=tee)145 fl = my_fun146 tl = list_points[i]147 return my_fun148class PieceWiseFunction(object):149 def __init__(self, list_functions, list_points):150 self.functions = list_functions151 self.points = np.array(list_points)152 def __call__(self, *args, **kwargs):153 x = args[0]154 greater_than = self.points > x155 if np.all(greater_than == False):156 return self.functions[-1](x)157 idx = np.argmax(greater_than)158 if idx == 0:159 return self.functions[idx](x)160 else:161 idx = idx-1162 return self.functions[idx](x)163class PieceWiseNamedFunction(object):164 def __init__(self, list_functions, list_points, name):165 self.functions = list_functions166 self.points = np.array(list_points)167 self.name = name168 def __call__(self, *args, **kwargs):169 x = args[1]170 n = args[0]171 greater_than = self.points > x172 if np.all(greater_than == False):173 return self.functions[-1](n, x)174 idx = np.argmax(greater_than)175 if idx == 0:176 return self.functions[idx](n, x)177 else:178 idx = idx - 1179 return self.functions[idx](n, x)180if __name__ == "__main__":181 b = 100.0182 e = 200.0183 def f1(x):184 return 400 - 3 * x * 3185 def f2(x):186 return 50.0 + x187 def f3(x):188 return 50 + 2 * x + x * 3189 def jump_f(x):190 if x <= b:191 return f1(x)192 else:193 return f2(x)194 def s(x, x0, a):195 return 1.0 / (1 + math.exp(-a * (x - x0)))196 funcs = [f1, f2, f3]197 bpoints = [0.0, 22.0, 50.0, 70.0]198 smoothed = smooth_functions(funcs, bpoints)199 all_x = np.linspace(bpoints[0], bpoints[-1], 1000)200 all_y = list(map(smoothed, all_x))201 plt.plot(all_x, all_y)202 for i, fn in enumerate(funcs):203 l = np.linspace(bpoints[i], bpoints[i + 1], 100)204 z = list(map(fn, l))205 plt.plot(l, z, 'r')206 if i > 0:207 plt.plot([bpoints[i], bpoints[i]], [fn(bpoints[i]), funcs[i - 1](bpoints[i])], 'r')208 plt.show()209 x1 = np.linspace(0.0, b, 1000)210 y1 = list(map(f1, x1))211 x2 = np.linspace(b, e, 1000)212 y2 = list(map(f2, x2))213 my_fun = SmoothFunction(f1, f2, b, 0.0, e)214 my_fun.find_k(tee=True)215 sol_k = my_fun._k216 def sm(x):217 return s(x, b, sol_k)218 def ds(x):219 return sm(x) * (1 - sm(x))220 x3 = np.linspace(0.0, e, 1000)221 y3 = list(map(my_fun, x3))222 smooth = list(map(sm, x3))223 dsmooth = list(map(ds, x3))224 fig = plt.figure()225 ax1 = fig.add_subplot(1, 2, 1)226 ax1.plot(x1, y1)227 ax1.plot(x2, y2)228 ax1.plot(x3, y3)229 # ax2 = fig.add_subplot(1, 3, 2)230 # ax2.plot(x3, smooth)231 ax3 = fig.add_subplot(1, 2, 2)232 ax3.plot(x3, dsmooth)...
main.py
Source:main.py
1import tkinter2from tkinter import *3import numpy as np4from matplotlib import pyplot as plt5list_functions = []6def peres():7 res = []8 for i in range(len(list_functions[lbox.curselection()[0]])):9 min = list_functions[lbox.curselection()[0]][i]10 if list_functions[lbox.curselection()[1]][i] < min:11 min = list_functions[lbox.curselection()[1]][i]12 res.append(min)13 list_functions.append(res)14 listbox_update()15def obed():16 res = []17 for i in range(len(list_functions[lbox.curselection()[0]])):18 max = list_functions[lbox.curselection()[0]][i]19 if list_functions[lbox.curselection()[1]][i] > max:20 max = list_functions[lbox.curselection()[1]][i]21 res.append(max)22 list_functions.append(res)23 listbox_update()24def count_function(x, a, b, c, d, isTrapezoid):25 result = []26 if isTrapezoid:27 for i in x:28 if a <= i <= d:29 if a <= i <= b:30 result.append(1 - (b - i) / (b - a))31 continue32 if b <= i <= c:33 result.append(1)34 continue35 if c <= i <= d:36 result.append(1 - (i - c) / (d - c))37 continue38 else:39 result.append(0)40 else:41 for i in x:42 if a <= i <= c:43 if a <= i <= b:44 result.append(1 - (b - i) / (b - a))45 continue46 if b <= i <= c:47 result.append(1 - (i - b) / (c - b))48 else:49 result.append(0)50 return result51# Ð¿Ð¾Ð»Ñ Ð´Ð»Ñ Ð²Ð²Ð¾Ð´Ð° знаÑений52def clicked():53 x = np.arange(100)54 a = int(fn_1_1.get())55 b = int(fn_1_2.get())56 c = int(fn_1_3.get())57 d = int(fn_1_4.get())58 list_functions.append(count_function(x, a, b, c, d, CheckVar1.get()))59 listbox_update()60def show():61 for i in list_functions:62 plt.plot(i)63 plt.show()64def change():65 list_functions.pop(lbox.curselection()[0])66 x = np.arange(100)67 a = int(fn_1_1.get())68 b = int(fn_1_2.get())69 c = int(fn_1_3.get())70 d = int(fn_1_4.get())71 list_functions.append(count_function(x, a, b, c, d, CheckVar1.get()))72 listbox_update()73def del_function():74 list_functions.pop(lbox.curselection()[0])75 listbox_update()76def listbox_update():77 lbox.delete(0, tkinter.END)78 for i in range(len(list_functions)):79 lbox.insert(i, str(i))80# обÑединение гÑаÑиков пеÑеÑеÑение гÑаÑиков ÑÑÐ¾Ð±Ñ Ð¿Ð¾Ð»ÑзоваÑÐµÐ»Ñ Ñам вÑбиÑал, ÑедакÑиÑоваÑÑ81window = Tk() # окно82window.title("ÐÑенка обÑемов пÑодаж")83window.geometry('400x250') # ÑÐ°Ð·Ð¼ÐµÑ Ð¾ÐºÐ½Ð°84# function 185lbl_fn1 = Label(window, text="ФÑнкÑÐ¸Ñ 1:")86lbl_fn1.grid(column=0, row=1)87fn_1_1 = Entry(window, width=5)88fn_1_1.grid(column=1, row=1)89fn_1_2 = Entry(window, width=5)90fn_1_2.grid(column=1, row=2)91fn_1_3 = Entry(window, width=5)92fn_1_3.grid(column=1, row=3)93fn_1_4 = Entry(window, width=5)94fn_1_4.grid(column=1, row=4)95CheckVar1 = BooleanVar()96chk_fn1 = Checkbutton(window, text='ТÑапеÑевиднаÑ?', variable=CheckVar1)97chk_fn1.grid(column=2, row=1)98btn = Button(window, text="ÐажаÑÑ!", command=clicked)99btn.grid(column=2, row=2)100btn_del = Button(window, text="ÐоказаÑÑ!", command=show)101btn_del.grid(column=2, row=3)102btn_change = Button(window, text="РедакÑиÑоваÑÑ!", command=change)103btn_change.grid(column=2, row=4)104btn_del = Button(window, text="УдалиÑÑ!", command=del_function)105btn_del.grid(column=2, row=5)106lbox = Listbox(width=15, height=8, selectmode='multiple')107lbox.grid(column=1, row=8)108btn_obed = Button(window, text="ÐбÑедениÑÑ!", command=obed)109btn_obed.grid(column=3, row=5)110btn_peres = Button(window, text="ÐеÑеÑеÑÑ!", command=peres)111btn_peres.grid(column=3, row=6)...
3-10.py
Source:3-10.py
1# every function2list_functions = ['indexing', 'append', 'insert', 'pop', 'remove',3 'sorted', 'reverse', 'index', 'len', 'sort']4print(list_functions)5# index6list_functions[0] = 'index'7print(list_functions)8# append9list_functions.append('sorted')10print(list_functions)11# pop12popped_function = list_functions.pop(0)13print(list_functions)14print(f"We've lost the {popped_function} function")15# insert16list_functions.insert(0, 'index')17print(list_functions)18# remove19removed_function = list_functions.remove("index")20print(list_functions)21list_functions.insert(0, 'index')22print(list_functions)23# sorted - temporarily24print(sorted(list_functions))25print(list_functions)26# reverse27list_functions.reverse()28print(list_functions)29# len30print(len(list_functions))31# sort reverse - permanently32list_functions.sort(reverse=True)33print(list_functions)34# sort - permanently35list_functions.sort()...
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!!