Best Python code snippet using yandex-tank
main.py
Source:main.py
1import csv2import json3import tkinter as tk4folder_dict = {}5# parses input file and outputs a json object of folder names corresponding to the elements inside6def parse(file_name, delimiter='_'):7 with open(file_name) as csv_file:8 temp_names = csv.reader(csv_file)9 names = []10 folders = []11 index = 012 for name in temp_names:13 names.append(name[0].split(delimiter))14 folders.append(name[0].split(delimiter))15 # possible folder names should not have the last word to avoid having folders that contain empty strings16 folders[index].pop(len(folders[index])-1)17 index += 118 global folder_dict19 folder_names = group_folders(folders)20 for i in range(len(names)):21 folder_name = delimiter.join(folder_names[i])22 elt_name = delimiter.join(names[i][len(folder_names[i]):])23 if folder_name in folder_dict:24 folder_dict[folder_name].append(elt_name)25 else:26 folder_dict[folder_name] = [elt_name]27 return json.dumps(folder_dict)28# given an array of possible folder names return an array of folder name that should be used for each name29def group_folders(folders):30 folder_names = []31 for j in range(len(folders)):32 name = folders[j]33 max_folder_name = []34 for i in range(len(name)):35 for k in range(len(folders)):36 other_name = folders[k]37 if j == k:38 continue39 if len(other_name) > i and name[:i+1] == other_name[:i+1]:40 max_folder_name = name[:i+1]41 break42 if not max_folder_name:43 folder_names.append(name)44 else:45 folder_names.append(max_folder_name)46 return folder_names47# will create new folder at the bottom of the list48def create_folder(folder_name):49 global folder_dict50 if folder_name in folder_dict:51 print("A folder with that name already exists: please try a different name.")52 else:53 folder_dict[folder_name] = []54# will add file at the bottom of the list inside the folder55def move_file(init_folder, dest_folder, file_name):56 global folder_dict57 if not dest_folder in folder_dict:58 print("A folder with that name does not exist: please select the correct folder or create a new folder.")59 else:60 folder_dict[init_folder].remove(file_name)61 folder_dict[dest_folder].append(file_name)62class createFolderWindow(object):63 def __init__(self, master):64 w = self.top = tk.Toplevel(master)65 self.t = tk.Label(w, text="New Folder Name:")66 self.t.pack(side=tk.LEFT)67 self.b = tk.Button(w, text="Add", command=self.cleanup)68 self.b.pack(side=tk.RIGHT)69 self.e = tk.Entry(w)70 self.e.pack(side=tk.RIGHT)71 def cleanup(self):72 self.value = self.e.get()73 create_folder(self.value)74 self.top.destroy()75class createFileWindow(object):76 def __init__(self, master, init_folder, file_name):77 w = self.top = tk.Toplevel(master)78 self.t = tk.Label(w, text="Destination Folder Name:")79 self.t.pack(side=tk.LEFT)80 self.b = tk.Button(w, text="Move", command=self.cleanup)81 self.b.pack(side=tk.RIGHT)82 self.e = tk.Entry(w)83 self.e.pack(side=tk.RIGHT)84 self.init_folder = init_folder85 self.file_name = file_name86 def cleanup(self):87 self.value = self.e.get()88 move_file(self.init_folder, self.value, self.file_name)89 self.top.destroy()90class mainWindow(object):91 def __init__(self, master):92 m = self.m = master93 self.scrollbar = tk.Scrollbar(m)94 self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)95 self.folder_list = tk.Listbox(96 m, selectmode=tk.SINGLE, width=32, yscrollcommand=self.scrollbar.set)97 for folder_name in folder_dict:98 self.folder_list.insert(tk.END, folder_name)99 for file_name in folder_dict[folder_name]:100 self.folder_list.insert(tk.END, " > " + file_name)101 self.folder_list.pack(side=tk.LEFT, fill=tk.BOTH)102 self.scrollbar.config(command=self.folder_list.yview)103 self.mb = tk.Menubutton(m, text="Options")104 self.mb.pack()105 self.mb.menu = tk.Menu(self.mb, tearoff=0)106 self.mb["menu"] = self.mb.menu107 self.mb.menu.add_command(label="Create New Folder",108 command=self.create_new_folder)109 self.mb.menu.add_command(110 label="Move Selected File", command=self.move_file)111 self.mb.pack()112 def create_new_folder(self):113 self.w = createFolderWindow(self.m)114 self.mb.wait_window(self.w.top)115 self.folder_list.delete(0, self.folder_list.size()-1)116 for folder_name in folder_dict:117 self.folder_list.insert(tk.END, folder_name)118 for file_name in folder_dict[folder_name]:119 self.folder_list.insert(tk.END, " > " + file_name)120 def move_file(self):121 selected = self.folder_list.curselection()122 if not selected:123 return124 index = 0125 init_folder = ""126 file_name1 = ""127 is_file = False128 global folder_dict129 for folder_name in folder_dict:130 if index == selected[0]:131 break132 index += 1133 for file_name in folder_dict[folder_name]:134 if index == selected[0]:135 is_file = True136 init_folder = folder_name137 file_name1 = file_name138 break139 index += 1140 if is_file:141 break142 if is_file:143 self.w = createFileWindow(self.m, init_folder, file_name1)144 self.mb.wait_window(self.w.top)145 self.folder_list.delete(0, self.folder_list.size()-1)146 for folder_name in folder_dict:147 self.folder_list.insert(tk.END, folder_name)148 for file_name in folder_dict[folder_name]:149 self.folder_list.insert(tk.END, "\t> " + file_name)150if __name__ == "__main__":151 print(parse("names.csv"))152 root = tk.Tk()153 m = mainWindow(root)...
pathmanager.py
Source:pathmanager.py
1import os2def init_folder(folder_path):3 """Initialize a folder"""4 if not os.path.exists(folder_path):5 os.makedirs(folder_path)6class PathManager():7 """A class for managing all of the paths for an experiment"""8 def __init__(self, config_dict):9 """10 Create all of the necessary folders and store the paths11 inputs:12 config_dict - a dictionary with the fields "experiment name",13 "trial name", "dataset"14 """15 self.config_dict = config_dict16 # Set up the experiment specific folders17 exp_path = os.path.join(18 'Experiments', self.config_dict['experiment name'])19 trial_path = os.path.join(exp_path, self.config_dict['trial name'])20 self.path_dict = {21 'exp': exp_path,22 'trial': trial_path,23 'figures': os.path.join(trial_path, 'figures'),24 'models': os.path.join(trial_path, 'models'),25 'results': os.path.join(trial_path, 'results'),26 'predictions': os.path.join(trial_path, 'predictions')27 }28 # Initialize data specific folders29 self.preprocessing_str = (30 "lpf_fc{}_hpf_fc{}_notch{}_clip{}_normalize{}".format(31 self.config_dict['lpf fc'], self.config_dict['hpf fc'],32 self.config_dict['notch'], self.config_dict['clip level'],33 self.config_dict['normalize']34 ))35 self.window_str = "window_length{}_overlap{}".format(36 self.config_dict['window length'], self.config_dict['overlap']37 )38 self.path_dict.update({39 'raw data': os.path.join('EDF', self.config_dict['dataset']),40 'buffers': os.path.join(41 'Buffers', self.config_dict['dataset'], self.preprocessing_str42 ),43 'data': os.path.join(44 'WindowedBuffers', self.config_dict['dataset'],45 self.preprocessing_str, self.window_str46 ),47 'labels': os.path.join(48 'Labels', self.config_dict['dataset'], self.window_str49 ),50 'features': os.path.join(51 'Features', self.config_dict['dataset'],52 self.preprocessing_str, self.window_str53 )54 })55 def initialize_experiment_folders(self):56 """Initialize folder tree for a given experiment"""57 # Initialize all of the folders58 init_folder(self.path_dict['exp'])59 init_folder(self.path_dict['trial'])60 init_folder(self.path_dict['figures'])61 init_folder(self.path_dict['models'])62 init_folder(self.path_dict['results'])63 init_folder(self.path_dict['predictions'])64 def initialize_folder(self, key):65 """Initialize a specific folder"""66 init_folder(self.path_dict[key])67 def add_feature_folder(self, key):68 """Add a feature folder"""69 self.path_dict['features'] = os.path.join(70 'Features', self.config_dict['dataset'], self.preprocessing_str,71 self.window_str)72 init_folder(self.path_dict['features'])73 self.path_dict[key] = os.path.join(self.path_dict['features'], key)74 init_folder(self.path_dict[key])75 def __getitem__(self, key):76 return self.path_dict[key]77 def __setitem__(self, key, value):78 self.path_dict[key] = value...
__init__.py
Source:__init__.py
...3from .trainer import Trainer4from .sampler import Sampler, Dataset5from .eval import Eval6import os7def init_folder():8 needed_folders = [9 "./data",10 "./data/train/",11 "./data/log",12 "./data/eval",13 "./data/models",14 "./data/result",15 "./cache"16 ]17 for path in needed_folders:18 if not os.path.exists(path):19 os.mkdir(path)...
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!!