Best Python code snippet using lisa_python
generate_experiments.py
Source:generate_experiments.py
...23 '\t-a $(ATTRIBUTES) -n {:d} -m {:f} -s {:f} -b {:f} -p {:f} -o 0 -u \\\n'24 '\t-d \'\\\n\t{}\\\n\t\'"\n\n').format(stream.count, name, clusters, minority_share,25 safe, borderline, rare, drifts)26stream.count = 027def write_to_file(str):28 if write_to_file.streams_written in [0, args.b]:29 if write_to_file.file is not None:30 write_to_file.file.close()31 write_to_file.curr_file_nr += 132 write_to_file.file = open(args.output_file if args.b == -133 else '{}.part{:d}'.format(args.output_file, write_to_file.curr_file_nr), 'w')34 write_to_file.file.write(legend)35 write_to_file.streams_written = 036 write_to_file.file.write(str)37 write_to_file.streams_written += 138write_to_file.file = None39write_to_file.curr_file_nr = 040write_to_file.streams_written = 041def instance_type_drift(type, drift_start, drift_end, start_ratio, end_ratio, divisor = 1):42 return '{}-ratio/{},start={:d},end={:d},value-start=1,value-end={:f}'.format(43 type, 'sudden' if drift_start == drift_end else 'incremental', drift_start, drift_end,44 round(end_ratio / (1 - end_ratio), 6) / round(start_ratio / (1 - start_ratio), 6) / divisor)45def borderline_drift(drift_start, drift_end, start_ratio, end_ratio):46 return instance_type_drift('borderline', drift_start, drift_end, start_ratio, end_ratio)47def rare_drift(drift_start, drift_end, start_ratio, end_ratio):48 return instance_type_drift('rare', drift_start, drift_end, start_ratio, end_ratio)49def borderline_rare_drift(drift_start, drift_end, start_ratio, end_ratio):50 return '{}:\\\n\t{}'.format(51 instance_type_drift('borderline', drift_start, drift_end, start_ratio, end_ratio, 2),52 instance_type_drift('rare', drift_start, drift_end, start_ratio, end_ratio, 2))53def imbalance_drift(drift_start, drift_end, start_ratio, end_ratio):54 return 'minority-share/{},start={:d},end={:d},value-start=1,value-end={:f}'.format(55 'sudden' if drift_start == drift_end else 'incremental', drift_start, drift_end, end_ratio / start_ratio)56def merge_clusters_at_beggining():57 return 'splitting-clusters/sudden,start=1,end=1,value-start=0,value-end=0'58def move_drift(drift_start, drift_end):59 return ('clusters-movement/incremental,start={0:d},end={1:d},value-start=0,value-end=1:\\\n\t'60 'clusters-movement/incremental,start={1:d},end={2:d},value-start=0,value-end=1').format(61 drift_start, int(drift_start + (drift_end - drift_start) / 2), drift_end)62def split_drift(drift_start, drift_end):63 return merge_clusters_at_beggining() + ':\\\n\t' + move_drift(drift_start, drift_end)64def join_drift(drift_start, drift_end):65 return 'splitting-clusters/incremental,start={:d},end={:d},value-start=1,value-end=0'.format(drift_start, drift_end)66#-----------------------------------------------------------------------------------------------------------------------67drift_start, drift_end = 70000, 10000068pd_start, pd_end = 2, 469epsilon = 0.00000170#-----Imbalance ratio streams-------------------------------------------------------------------------------------------71imbalance = [.5, .4, .3, .2, .1, .05, .03, .02, .01]72for im1 in imbalance:73 for im2 in imbalance:74 name, drift = 'StaticIm{:d}'.format(int(im1 * 100)), ['']75 if im1 > im2:76 name = '{}Im{:d}'.format('' if im1 == .5 else name + '_', int(im2 * 100))77 drift = [imbalance_drift(drift_start, drift_end, im1, im2)]78 if im1 < im2: continue79 write_to_file(stream(name, 1, im1, 1, 0, 0, drift))80#----- Class swaps (minority -> majority)-------------------------------------------------------------------------------81for im1 in imbalance[1:]:82 for im2 in {1 - im1, .6, .7, .8, .9}:83 write_to_file(stream('StaticIm{:d}_Im{:d}'.format(int(im1 * 100), int(im2 * 100)), 1, im1, 1, 0, 0,84 [imbalance_drift(drift_start, drift_end, im1, im2)]))85#-----Split vs Move vs Join streams---------------------------------------------------------------------------------------------86for static_im in [.5, .1, .01]:87 for clusters in [3, 5, 7]:88 static = 'StaticIm{:d}_'.format(int(static_im * 100)) if static_im != .5 else ''89 split_name = static + 'Split{}'.format(clusters)90 move_name = static + 'Move{}'.format(clusters)91 join_name = static + 'Join{}'.format(clusters)92 write_to_file(stream(split_name, clusters, static_im, 1, 0, 0, [split_drift(drift_start, drift_end)]))93 write_to_file(stream(split_name + post_drift_suffix, clusters, static_im, 1, 0, 0, [split_drift(pd_start, pd_end)]))94 write_to_file(stream(move_name, clusters, static_im, 1, 0, 0, [move_drift(drift_start, drift_end)]))95 write_to_file(stream(move_name + post_drift_suffix, clusters, static_im, 1, 0, 0, [move_drift(pd_start, pd_end)]))96 write_to_file(stream(join_name, clusters, static_im, 1, 0, 0, [join_drift(drift_start,drift_end)]))97 write_to_file(stream(join_name + post_drift_suffix, clusters, static_im, 1, 0, 0, [join_drift(pd_start, pd_end)]))98#-----instance type streams---------------------------------------------------------------------------------------------99# for static_im in [.5, .1, .01]:100# static = 'StaticIm{:d}+'.format(int(static_im * 100)) if static_im != .5 else ''101# for borderline in [.2, .5, 1]:102# write_to_file(stream('{}StaticBorderline{}'.format(static, int(100 * borderline)),103# 1, static_im, 1 - borderline, borderline, 0, [""]))104#105# for static_im in [.5, .1, .01]:106# static = 'StaticIm{:d}+'.format(int(static_im * 100)) if static_im != .5 else ''107# for rare in [.2, .5, 1]:108# write_to_file(stream('{}StaticRare{}'.format(static, int(100 * rare)),109# 1, static_im, 1 - rare, 0, rare, [""]))110#111# for static_im in [.5, .1, .01]:112# static = 'StaticIm{:d}_'.format(int(static_im * 100)) if static_im != .5 else ''113# write_to_file(stream('{}Borderline20_Borderline50_Borderline100'.format(static), 1, static_im, 1, epsilon, 0,114# [borderline_drift(50000, 50000, epsilon, .2),115# borderline_drift(80000, 80000, .2, .5),116# borderline_drift(110000, 110000, .5, .999999)]))117# for static_im in [.5, .1, .01]:118# static = 'StaticIm{:d}_'.format(int(static_im * 100)) if static_im != .5 else ''119# write_to_file(stream('{}Rare20_Rare50_Rare100'.format(static), 1, static_im, 1, 0, epsilon,120# [rare_drift(50000, 50000, epsilon, .2),121# rare_drift(80000, 80000, .2, .5),122# rare_drift(110000, 110000, .5, .999999)]))123#-----StaticIm+Split5+Im+Borderline/Rare streams------------------------------------------------------------------------124clusters = 5125borderline_levels = .2, .4, .6, .8, 1 - epsilon126rare_levels = borderline_levels127borderline_rare_levels = .4, .8128static_ims = {'': .5,129 'StaticIm10': .1}130splits = {'': lambda ds, de: merge_clusters_at_beggining(),131 'Split{}'.format(clusters): lambda ds, de: split_drift(ds, de)}132ims = {'': lambda ds, de, x: "",133 'Im10': lambda ds, de, x: imbalance_drift(ds, de, x, .1),134 'Im1': lambda ds, de, x: imbalance_drift(ds, de, x, .01)}135types_borderline = {'Borderline{:d}'.format(int((val + epsilon) * 100)):136 (epsilon, 0, lambda ds, de, v = val: borderline_drift(ds, de, epsilon, v))137 for val in borderline_levels}138types_rare = {'Rare{:d}'.format(int((val + epsilon) * 100)):139 (0, epsilon, lambda ds, de, v = val: rare_drift(ds, de, epsilon, v)) for val in rare_levels}140types_borderline_rare = {'Borderline{0:d}+Rare{0:d}'.format(int((val / 2 + epsilon) * 100)):141 (epsilon, epsilon, lambda ds, de, v = val: borderline_rare_drift(ds, de, epsilon, v))142 for val in borderline_rare_levels}143types = {'': (0, 0, lambda ds, de: ''), **types_borderline, **types_rare, **types_borderline_rare}144for static_im in static_ims.keys():145 for split in splits.keys():146 for im in ims.keys():147 for type in types.keys():148 name = '+'.join(list(filter(None, [split, im, type])))149 name = '_'.join(list(filter(None, [static_im, name])))150 if (static_im == 'StaticIm10' and im == 'Im10') or name is "": continue151 drifts = [splits[split](drift_start, drift_end),152 ims[im](drift_start, drift_end, static_ims[static_im]),153 types[type][2](drift_start, drift_end)]154 write_to_file(stream(name, clusters, static_ims[static_im],155 1, types[type][0], types[type][1], drifts))156 post_drifts = [splits[split](pd_start, pd_end), ims[im](pd_start, pd_end, static_ims[static_im]), types[type][2](pd_start, pd_end)]157 write_to_file(stream(name + post_drift_suffix, clusters, static_ims[static_im],158 1, types[type][0], types[type][1], post_drifts))159#-----------------------------------------------------------------------------------------------------------------------160print("Written {} streams to {}{}".format(stream.count, args.output_file, '' if write_to_file.curr_file_nr == 1 else '({} parts)'.format(write_to_file.curr_file_nr)))...
main.py
Source:main.py
1from asyncore import write2import os3import platform4import traceback5from pynput import keyboard6"""7 Shell Simulator - should work on both windows and unix-like8"""9VALID_COMMANDS = ["cd", "clr", "dir",10 "environ", "echo", "help", "pause", "quit"]11HELP_TEXT = """12PyShell, version 1.013These shell commands are defined internally, type help to see this list14cd <directory> change the current directory to <directory> if it exists15clr clear the screen16dir <directory list the contents of <directory>, including hidden files17environ list all environment variables18echo <comment> display <comment> on a new line19help help text20pause pause operation of the shell21quit quit the shell22Optional arguments command [> | >> filename] - executes command and redirects output23 either truncating (>) or appending (>>) to [filename]24"""25def clear_screen(write_to_file=(False, None, 'w')):26 try:27 if write_to_file[0]: # If > or >> appears28 with open(write_to_file[1], write_to_file[2]) as f:29 pass # Creates an empty file30 if platform.system() == "Windows":31 os.system("cls")32 else: # For unix-like33 os.system("clear")34 except Exception:35 print("Error:", traceback.format_exc())36def list_dir(directory: str, write_to_file=(False, None, 'w')):37 try:38 if write_to_file[0]:39 with open(write_to_file[1], write_to_file[2]) as f:40 for item in os.listdir(directory):41 if not item.startswith("."): # Hide dotfolders by default42 print(item, file=f)43 else:44 for item in os.listdir(directory):45 if not item.startswith("."): # Hide dotfolders by default46 print(item)47 except:48 print("Error: ", traceback.format_exc())49def echo_text(text, write_to_file=(False, None, 'w')):50 try:51 if write_to_file[0]:52 with open(write_to_file[1], write_to_file[2]) as f:53 # Echo Text, multiple spaces reduced to 154 print(" ".join(text[:]), file=f)55 else:56 print(" ".join(text[:]))57 except Exception:58 print("Error:", traceback.format_exc())59def on_press(key):60 # try:61 # print('alphanumeric key {0} pressed'.format(62 # key.char))63 # except AttributeError:64 # print('special key {0} pressed'.format(65 # key))66 if key == keyboard.Key.enter:67 return False68def print_envs(write_to_file=(False, None, "w")):69 try:70 if write_to_file[0]:71 with open(write_to_file[1]) as f:72 for var, value in os.environ.items():73 print(f"{var}={value}", file=f)74 else:75 for var, value in os.environ.items():76 print(f"{var}={value}")77 except Exception:78 print("Error: ", traceback.format_exc())79def change_dir(directory: str, write_to_file=(False, None, "w")):80 try:81 if write_to_file[0]: # Write to file flag check82 with open(write_to_file[1], write_to_file[2]) as f:83 if directory == ".":84 print(os.getcwd(), file=f)85 else:86 os.chdir(directory)87 os.environ['pwd'] = directory88 else:89 if directory == ".":90 print(os.getcwd())91 else:92 os.chdir(directory)93 os.environ['pwd'] = directory94 except KeyError:95 print("Error: Can't set pwd environment variable value")96 except OSError:97 print("Error: No such file or directory")98 except Exception:99 print("Error:", traceback.format_exc())100def pause_shell(write_to_file=(False, None, "w")):101 if write_to_file[0]:102 with open(write_to_file[1], write_to_file[2]) as f:103 print("Press Enter to continue...:", file=f)104 else:105 print("Press Enter to continue...:")106 with keyboard.Listener(107 on_press=on_press, suppress=True) as listener:108 listener.join()109def display_help(write_to_file=(False, None, 'w')):110 try:111 if write_to_file[0]:112 with open(write_to_file[1], write_to_file[2]) as f:113 print(HELP_TEXT, file=f)114 else:115 print(HELP_TEXT)116 except Exception:117 print("Error: ", traceback.format_exc())118def parse_command(command: list[str]):119 # 3-tuple of (flag, file_name, file_mode)120 write_to_file = (False, None, "w")121 directory = "." # Default cd, dir as current directory122 text = ""123 # Redirect operator checking, better and more correctly done with regex which handles124 # multiple redirect files and redirect operators but this works for simple cases125 try:126 if len(command) >= 2:127 # cd and dir specific, which take directory parameter128 if command[0] == "cd" or command[0] == "dir":129 if len(command) == 2 or len(command) > 3:130 directory = command[1]131 132 if command[1] == ">":133 write_to_file = (True, command[2], "w")134 elif command[1] == ">>":135 write_to_file = (True, command[2], "a")136 if len(command) > 3:137 if command[2] == ">":138 write_to_file = (True, command[3], "w")139 elif command[2] == ">>":140 write_to_file = (True, command[3], "a")141 # echo specific which can take n string arguments142 elif command[0] == "echo":143 try:144 if ">" in command[1:]:145 redir_position = command.index(">")146 text = command[1:redir_position]147 write_to_file = (148 True, command[redir_position + 1], "w")149 elif ">>" in command[1:]:150 redir_position = command.index(">>")151 text = command[1:redir_position]152 write_to_file = (153 True, command[redir_position + 1], "a")154 except IndexError: # Not necessary but a safeguard155 pass156 else:157 if command[1] == ">":158 write_to_file = (True, command[2], "w")159 elif command[1] == ">>":160 write_to_file = (True, command[2], "a")161 if command[0] == "clr":162 clear_screen(write_to_file=write_to_file)163 if command[0] == "cd":164 change_dir(directory, write_to_file=write_to_file)165 if command[0] == "dir":166 list_dir(directory, write_to_file=write_to_file)167 if command[0] == "environ":168 print_envs(write_to_file=write_to_file)169 if command[0] == "echo":170 echo_text(text, write_to_file=write_to_file)171 if command[0] == "pause":172 pause_shell(write_to_file=write_to_file)173 if command[0] == "quit": # Quit Shell174 if write_to_file[0]:175 with open(write_to_file[1], write_to_file[2]):176 pass # Does not write anything to file177 exit()178 if command[0] == "help":179 display_help(write_to_file=write_to_file)180 except IndexError:181 print("Error: Invalid number of arguments")182 print(traceback.format_exc())183 except Exception:184 print("Error: Syntax error unexpected tokens")185 print(traceback.format_exc())186def run_cli():187 while True:188 print("PyShell>", end=' ')189 command = input().split()190 if command:191 if command[0] not in VALID_COMMANDS: # First non-space element is command192 print("Command is not defined/invalid input")193 else:194 parse_command(command)195if __name__ == "__main__":...
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!!