Best Python code snippet using fMBT_python
algorithm.py
Source:algorithm.py
1import parser2import copy3import random4import math5class Algorithm():6 """7 Base algorithm class8 """9 def __init__(self, Graph, workers_data:dict) -> None:10 self.Graph = Graph11 self.workers_data = workers_data12 self.wokrers_list = parser.preper_woreks(workers_data=workers_data)13 self.wn = len(workers_data)14 self.order = self.Graph.TOP_ORDER()15 self.preproces()16 17 def preproces(self):18 self.permutation = [[] for _ in range(self.wn+1)]19 self.Z = []20 for _ in range(self.wn + 1):21 self.Z.append(0)22 def generate_permutation(self):23 iteracje = 124 print("graph order: ", self.order)25 for e in self.order:26 if e == 0:27 continue28 29 USE_WORKERS = []30 ID_WORKERS = []31 32 for i in self.Graph.Res[e]:33 for k in range(1,self.wn + 1):34 if i.id in self.wokrers_list[k - 1]:35 if k not in USE_WORKERS:36 ID_WORKERS.append(k)37 for l in range(1,i.number+1):38 USE_WORKERS.append(ID_WORKERS.pop(0))39 iteracje += 140 # if iteracje == 4:41 # break42 for m in USE_WORKERS:43 self.permutation[m].append(e)44 print(self.permutation)45def easy_asign(workers, Graph):46 a = []47 a.append([])48 for op in range(1,Graph.n+1):49 USE_WORKERS = []50 FUNCTION_ID = []51 for r in Graph.Res[op]:52 r_id = r.id53 r_number = r.number54 ID_WORKERS = []55 for k in range(1,len(workers)):56 if r_id in workers[k - 1]:57 if k not in USE_WORKERS:58 ID_WORKERS.append(k)59 for l in range(1, r_number + 1):60 # print(ID_WORKERS)61 if len(ID_WORKERS) == 0:62 break63 USE_WORKERS.append(ID_WORKERS.pop(0))64 FUNCTION_ID.append(r_id)65 a.append({"USE_WORKERS":USE_WORKERS,66 "FUNCTION_ID":FUNCTION_ID})67 return a68def c_max(m, workers, Graph, a, pi):69 Z = [ 0 for _ in range(m + 1)]70 C = [ 0 for _ in range(Graph.n + 1)]71 for i in range(1,Graph.n +1):72 op = pi[i]73 z = 074 for j in a[op]["USE_WORKERS"]:75 z = max([z, Z[j]])76 for arc in Graph.Pred[op]:77 z = max([z, C[arc.nd] + arc.weight])78 79 z = z + Graph.p[op]80 C[op] = z81 for j in a[op]["USE_WORKERS"]:82 Z[j] = z83 return C84def c_max2(m, workers, Graph, a, pi):85 Z = [ 0 for _ in range(m + 1)]86 C = [ 0 for _ in range(Graph.n + 1)] #momenty zakonczenia87 # rozpoczecie C -graph.p88 for i in range(1,Graph.n +1):89 op = pi[i]90 z = 091 for j in a[op]["USE_WORKERS"]:92 z = max([z, Z[j]])93 for nx in range(len(a[op]["USE_WORKERS"])):94 ky = a[op]["USE_WORKERS"][nx]95 fun_w = a[op]["FUNCTION_ID"][nx]96 if z == Z[ky]:97 for k in range(1,m+1):98 if k in a[op]["USE_WORKERS"]:99 continue100 if Z[k] >= z:101 continue102 if fun_w in workers[k-1]:103 a[op]["USE_WORKERS"][nx] = k104 break105 continue106 z = 0107 for j in a[op]["USE_WORKERS"]:108 z = max([z, Z[j]])109 for arc in Graph.Pred[op]:110 z = max([z, C[arc.nd] + arc.weight])111 112 z = z + Graph.p[op]113 C[op] = z114 for j in a[op]["USE_WORKERS"]:115 Z[j] = z116 return C, max(C)117def move_elem(l, oldindex, newindex):118 l.insert(newindex, l.pop(oldindex))119def IS_TOP(pi, Graph):120 ps = [0 for _ in range(Graph.n + 1)]121 for i in range(1, Graph.n + 1):122 ps[pi[i]] = i123 # print("ps: ",ps)124 for nd in range(1, Graph.n + 1):125 for a in Graph.Succ[nd]:126 if ps[nd] > ps[a.nd]:127 return False128 return True129def insert_nbr(Graph, workers_list, ord):130 best_C, best_max_C = 0, 0131 a0 = easy_asign(Graph=Graph, workers=workers_list)132 C0, max_C0 = c_max2(m=len(workers_list),133 workers=workers_list,Graph=Graph, a=a0, pi=ord)134 best_C = C0135 best_max_C = max_C0136 best_ord = copy.deepcopy(ord)137 # print(best_ord)138 for i in range(1,len(ord)):139 for j in range(i+1,len(ord)):140 move_elem(l=ord,oldindex=i,newindex=j)141 if IS_TOP(pi=ord,Graph=Graph) == False:142 move_elem(l=ord,oldindex=j,newindex=i)143 break144 else:145 a1 = 0146 C1, max_C1 = 0, 0147 a1 = easy_asign(Graph=Graph, workers=workers_list)148 C1, max_C1 = c_max2(m=len(workers_list),149 workers=workers_list,Graph=Graph, a=a1, pi=ord)150 if best_max_C > max_C1:151 best_max_C = max_C1152 best_C = C1153 best_ord = copy.deepcopy(ord)154 move_elem(l=ord,oldindex=j,newindex=i)155 for j in range(i-1,1,-1):156 move_elem(l=ord,oldindex=i,newindex=j)157 if IS_TOP(pi=ord,Graph=Graph) == False:158 move_elem(l=ord,oldindex=j,newindex=i)159 break160 else:161 a1 = 0162 C1, max_C1 = 0, 0163 a1 = easy_asign(Graph=Graph, workers=workers_list)164 C1, max_C1 = c_max2(m=len(workers_list),165 workers=workers_list,Graph=Graph, a=a1, pi=ord)166 if best_max_C > max_C1:167 best_max_C = max_C1168 best_C = C1169 best_ord = copy.deepcopy(ord)170 move_elem(l=ord,oldindex=j,newindex=i)171 return best_C, best_max_C, best_ord172def insert_rand(Graph, workers_list, ord):173 best_C, best_max_C = 0, 0174 a0 = easy_asign(Graph=Graph, workers=workers_list)175 C0, max_C0 = c_max2(m=len(workers_list),176 workers=workers_list,Graph=Graph, a=a0, pi=ord)177 best_C = C0178 best_max_C = 1000_000179 best_max_C = max_C0180 best_ord = copy.deepcopy(ord)181 i = random.randint(1,len(ord)-1)182 # print(i)183 # print(len(ord))184 for j in range(i+1,len(ord)):185 # print("i: ",i,"j: ",j)186 move_elem(l=ord,oldindex=i,newindex=j)187 if IS_TOP(pi=ord,Graph=Graph) == False:188 move_elem(l=ord,oldindex=j,newindex=i)189 # print("1")190 break191 else:192 # print("TRUE")193 a1 = 0194 C1, max_C1 = 0, 0195 a1 = easy_asign(Graph=Graph, workers=workers_list)196 C1, max_C1 = c_max2(m=len(workers_list),197 workers=workers_list,Graph=Graph, a=a1, pi=ord)198 if best_max_C > max_C1:199 best_max_C = max_C1200 best_C = C1201 best_ord = copy.deepcopy(ord)202 move_elem(l=ord,oldindex=j,newindex=i)203 for j in range(i-1,1,-1):204 move_elem(l=ord,oldindex=i,newindex=j)205 if IS_TOP(pi=ord,Graph=Graph) == False:206 move_elem(l=ord,oldindex=j,newindex=i)207 # print("2")208 break209 else:210 a1 = 0211 C1, max_C1 = 0, 0212 a1 = easy_asign(Graph=Graph, workers=workers_list)213 C1, max_C1 = c_max2(m=len(workers_list),214 workers=workers_list,Graph=Graph, a=a1, pi=ord)215 if best_max_C > max_C1:216 best_max_C = max_C1217 best_C = C1218 best_ord = copy.deepcopy(ord)219 move_elem(l=ord,oldindex=j,newindex=i)220 return best_C, best_max_C, best_ord221def ds(ord, Graph, workers_list):222 best_ord = copy.deepcopy(ord)223 NX = 1000_000224 while True:225 best_C, best_max_C, best_ord = insert_nbr(Graph=Graph, workers_list=workers_list, ord=best_ord)226 if best_max_C < NX:227 NX = best_max_C228 else:229 break230 # print("best_max_C: ", best_max_C)231 return best_C, best_max_C, best_ord232def random_serge(ord, Graph, workers_list):233 best_ord = copy.deepcopy(ord)234 NX = 1000_000235 for i in range(1000):236 best_C, best_max_C, best_ord = insert_rand(Graph=Graph, workers_list=workers_list, ord=best_ord)237 # print("i: ",i)238 # print("best_max_C: ",best_max_C)239 if best_max_C < NX:240 NX = best_max_C241 # print("best_max_C: ", best_max_C)242 return best_C, best_max_C, best_ord243def symulowane_wyzazanie(pi,Graph,workers_list):244 t0 = 1000245 tk = 0.1246 lam = 0.995247 t = t0248 n=Graph.n249 m = len(workers_list)250 a0 = easy_asign(Graph=Graph, workers=workers_list)251 C0_best, Cmax_best = c_max2(m=m, workers=workers_list, Graph=Graph, a=a0, pi=pi)252 best_pi = []253 best_pi = copy.deepcopy(pi)254 while t > tk:255 l1 = random.randint(1,n)256 l2 = random.randint(1,n)257 C_tmp0, max_tmp0 = c_max2(m=m, workers=workers_list, Graph=Graph, a=a0, pi=pi)258 move_elem(l=pi, oldindex=l1, newindex=l2)259 if IS_TOP(pi=pi,Graph=Graph) == False:260 move_elem(l=pi,oldindex=l2,newindex=l1)261 continue262 else:263 C_tmp, max_tmp= c_max2(m=m, workers=workers_list, Graph=Graph, a=a0, pi=pi)264 if max_tmp < Cmax_best:265 C0_best = C_tmp266 Cmax_best = max_tmp267 # print("pom: ",Cmax_best)268 best_pi = copy.deepcopy(pi)269 if max_tmp > max_tmp0:270 delta = max_tmp-max_tmp0271 P = math.exp(-delta/t)272 Z = random.random()273 if Z <= P:274 i = 0275 else:276 move_elem(l=pi, oldindex=l2, newindex=l1)277 t = lam*t278 best_order = copy.deepcopy(best_pi)279 # print("end")...
psmap.py
Source:psmap.py
...13print psmap.psmap(fib, [32] * 16)14---152. Example, use all workers registered on two pythonshare-server hubs:16import psmap17psmap.use_workers(hub_1_hostspec)18psmap.use_workers(hub_2_hostspec)19...20psmap.psmap(fib, [32] * 16)21---223. Example, use explicitly listed workers, some may be local, some remote:23import psmap24psmap.use_worker(worker_1_hostspec)25psmap.use_worker(worker_2_hostspec)26...27psmap.use_worker(worker_n_hostspec)28psmap.psmap(fib, [32] * 16)29"""30import atexit31import Queue32import inspect33import os34import pythonshare35import subprocess36import thread37import time38g_hubs = []39g_hubs_kill_at_exit = []40g_workers = []41g_worker_conns = []42g_workers_free = Queue.Queue()43def soe(cmd, stdin="", cwd=None, env=None, bg=False):44 """Run cmd, return (status, stdout, stderr)"""45 run_env = dict(os.environ)46 if not env is None:47 run_env.update(env)48 try:49 p = subprocess.Popen(50 cmd,51 stdin=subprocess.PIPE,52 stdout=subprocess.PIPE,53 stderr=subprocess.PIPE,54 close_fds=True,55 cwd=cwd,56 env=run_env)57 if bg:58 return (p, None, None)59 out, err = p.communicate(input=stdin)60 except Exception, e:61 return (None, None, str(e))62 return (p.returncode, out, err)63def use_workers(hub_hostspec):64 g_hubs.append(hub_hostspec)65 conn = pythonshare.connect(hub_hostspec)66 for ns in conn.ls_remote():67 use_worker(hub_hostspec + "/" + ns)68def launch_workers(count, hub_port=9999):69 hub_addr = launch_hub(hub_port)70 for worker_id in xrange(count):71 launch_worker(worker_id, hub_addr)72def launch_hub(port=9999):73 hub_addr = "localhost:%s" % (port,)74 try:75 pythonshare.connect(hub_addr).kill_server()76 except:77 pass78 soe(["pythonshare-server", "-p", hub_addr.split(":")[-1]])79 g_hubs_kill_at_exit.append(hub_addr)80 g_hubs.append(hub_addr)81 return hub_addr82def launch_worker(worker_id, hub_addr):83 if hub_addr is None:84 hub_addr = g_hub_addr85 namespace = str(worker_id)86 soe(["pythonshare-server", "-p", "stdin", "-n", namespace, "-E", hub_addr],87 bg=True)88 worker_addr = hub_addr + "/" + namespace89 use_worker(worker_addr)90def use_worker(worker_hostspec):91 conn = pythonshare.connect(worker_hostspec)92 g_workers.append(worker_hostspec)93 g_worker_conns.append(conn)94 g_workers_free.put(conn)95 thread.start_new_thread(eval_thread, ())96def _clean_up():97 for _ in xrange(len(g_workers)):98 g_workers_free.put(None)99 eval_jobs.put(None)100 time.sleep(0.01)101 for conn in g_worker_conns:102 try:103 conn.close()104 except:105 pass106 for hub_addr in g_hubs_kill_at_exit:107 try:108 pythonshare.connect(hub_addr).kill_server()109 except:110 pass111atexit.register(_clean_up)112eval_results = Queue.Queue()113eval_jobs = Queue.Queue()114def eval_thread():115 while 1:116 conn = g_workers_free.get()117 if conn is None:118 break119 job = eval_jobs.get()120 if job is None:121 break122 try:123 job['conn'] = conn124 job['result'] = job['conn'].eval_(job['code'])125 eval_results.put(job)126 finally:127 g_workers_free.put(conn)128def psmap(func, params_list):129 func_source = inspect.getsource(func)130 results = [None] * len(params_list)131 for worker_conn in g_worker_conns:132 worker_conn.exec_(func_source)133 for job_index, params in enumerate(params_list):134 if isinstance(params, tuple):135 params_tuple = params136 else:137 params_tuple = (params,)138 eval_jobs.put({139 'conn': None,140 'job_index': job_index,141 'code': func.__name__ + repr(params_tuple)})142 for params in params_list:143 job = eval_results.get()144 results[job['job_index']] = job['result']145 return results146def fib(n):147 if n <= 1:148 return 1149 else:150 return fib(n-1) + fib(n-2)151if __name__ == "__main__":152 import sys153 if len(sys.argv) == 1:154 launch_workers(8)155 else:156 print "using workers from hubs: ", ", ".join(sys.argv[1:])157 for hub in sys.argv[1:]:158 use_workers(hub)159 print "running", len(g_worker_conns), "workers"160 job_count = 30161 print "will run", job_count, "jobs"162 print "running local map..."163 s = time.time()164 print map(fib, [31] * job_count)165 e = time.time()166 print "map:", e-s167 print "running psmap..."168 s = time.time()169 print psmap(fib, [31] * job_count)170 e = time.time()...
events_api.py
Source:events_api.py
1import asyncio2import concurrent.futures3from datetime import datetime4from typing import List, Union5import nest_asyncio6from models.events import EventList7from providers.fake_provider import FakeProvider8from utils.validator import Validator9class EventsApi:10 def __init__(self, use_workers: bool, max_workers: int) -> None:11 self.max_workers = max_workers12 self.use_workers = use_workers13 def get_available_events(14 self, starts_at: Union[str, datetime], ends_at: Union[str, datetime]15 ) -> EventList:16 starts_at = Validator.is_valid_date(starts_at)17 ends_at = Validator.is_valid_date(ends_at)18 # getting all days to make the search19 query_dates = Validator.range_of_dates(starts_at, ends_at)20 if self.use_workers:21 nest_asyncio.apply()22 # retrieve filtered events of most recent dates - asynchronously23 loop = asyncio.new_event_loop()24 asyncio.set_event_loop(loop)25 events = loop.run_until_complete(self.async_request(query_dates))26 else:27 # retrieve filtered events of most recent dates - synchronously28 events = FakeProvider.get_events_on_dates(query_dates)29 return FakeProvider.get_unique_most_recent_events(events)30 async def async_request(self, dates_to_query: List[datetime]) -> EventList:31 events = []32 with concurrent.futures.ThreadPoolExecutor(33 max_workers=self.max_workers34 ) as executor:35 loop = asyncio.get_event_loop()36 futures = [37 loop.run_in_executor(38 executor, FakeProvider.get_events_on_date, dates_to_query[i]39 )40 for i in range(0, len(dates_to_query))41 ]42 for events_on_date in await asyncio.gather(*futures):43 events.extend(events_on_date)...
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!!