Best Python code snippet using stestr_python
timing.py
Source:timing.py
1# timing functions for the genetic algorithm2# Jan 20223import re4import time5import statistics6import multiprocessing7import config 8def do_match(regex, regex_input, pipe):9 time_started = time.process_time()10 regex.match(regex_input)11 time_ended = time.process_time()12 pipe.send(time_ended - time_started)13def match_time(regex, regex_input, iterations: int = 2, timeout: int = config.timeout):14 """15 Determine how long a string takes to match16 """17 times = []18 compiled_regex = re.compile(regex)19 for _ in range(iterations):20 parent_conn, child_conn = multiprocessing.Pipe()21 p = multiprocessing.Process(target=do_match, args=((compiled_regex, regex_input, child_conn, )))22 p.start()23 p.join(timeout=timeout)24 if p.is_alive():25 time_taken = timeout26 # print("TIMEOUT:", regex_input)27 p.kill()28 else:29 time_taken = parent_conn.recv()30 times.append(time_taken)31 # print("difference in time", total_time1, total_time2, abs(total_time1 - total_time2))32 return statistics.mean(times)33# def match_time(regex, regex_input):34# """35# Old version for testing 36# """37# compiled_regex = re.compile(regex)38# times = [] 39# for i in range(10):40# time_started = time.process_time()41# compiled_regex.match(regex_input)42# time_ended = time.process_time()43# times.append(time_ended-time_started)44# return statistics.mean(times)45# returns time taken per character46# no longer in use 47def average_match_time(regex, input):48 time_taken = match_time(regex, input)49 average_time = time_taken / len(input)50 return average_time51# returns slowest individual in a generation based on average matching time (per letter)52# TODO check if this is still in use 53def slowest_average_match_time(regex, generation):54 slowest_time = 055 slowest_string = ""56 for finalist in generation:57 time_taken = match_time(regex, finalist)58 if time_taken > slowest_time:59 slowest_string = finalist60 slowest_time = time_taken61 slowest_string = "".join(slowest_string)62 return slowest_string, slowest_time63# returns slowest individual in a generation based on total matching time 64def slowest_total_match_time(regex, generation):65 slowest_time = 066 slowest_string = ""67 for finalist in generation:68 time_taken = match_time(regex, finalist)69 if time_taken >= config.timeout:70 return finalist, config.timeout 71 elif time_taken > slowest_time:72 slowest_string = finalist73 slowest_time = time_taken74 return slowest_string, slowest_time75# an alternate to fittest_individual_total76# not currently in use77def slowest_individual(regex, generation):78 dict = {}79 ordered_generation = []80 for input in generation:81 if input != "":82 dict[input] = match_time(regex, input)83 sorted_inputs = sorted(dict.items(), key=lambda kv: kv[1])84 slowest_item = sorted_inputs[-1][0]85 slowest_time = sorted_inputs[-1][1]...
slowestKey.py
Source:slowestKey.py
1class Solution:2 def slowestKey(self, releaseTimes: List[int], keysPressed: str) -> str:3 slowestDuration, durationI=0,0 #durationI will contain the duration of the key pressed for the ith iteration4 slowestKey=''5 for i in range(len(keysPressed)):6 #calculate durationI7 if i==0: durationI = releaseTimes[i]8 else: durationI = releaseTimes[i] - releaseTimes[i-1]9 #only proceed if durationI is longer (greater) than the current slowest duration10 if durationI>=slowestDuration:11 if durationI==slowestDuration:12 #replace slowestKey if current key is lexicographically greater than the slowestKey13 if slowestKey<keysPressed[i]:14 slowestKey=keysPressed[i]15 slowestDuration=durationI #update slowestDuration value if slowestKEy has been replaced16 else:17 slowestKey=keysPressed[i]18 slowestDuration=durationI...
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!!