Best Python code snippet using selene_python
automated_answer_file.py
Source:automated_answer_file.py
1from pandas import DataFrame2class AutomatedAnswerFile:3 def __init__(self,4 file_name: str):5 self.answers_file = file_name6 with open(self.answers_file, 'w') as answersFile:7 answersFile.write('Final Project Automated Answers\n')8 def _write_question_number(self,9 question_number: str):10 with open(self.answers_file, 'a') as answersFile:11 answersFile.write(f'\nQuestion {question_number}\n')12 def write_answer_from_dict(self,13 answer_dict: dict,14 section: str = None,15 answer_description: str = None,16 data_description: str = None,17 unwanted_fields_in_dict: list = None,18 more_info: str = None,19 question_number: str = None):20 tab = '\t'21 tabs_number = 122 if unwanted_fields_in_dict is None:23 unwanted_fields_in_dict = []24 if question_number is not None:25 self._write_question_number(question_number=question_number)26 with open(self.answers_file, 'a') as answersFile:27 if section:28 if answer_description:29 answersFile.write(f'\n\t{section}. {answer_description}\n')30 else:31 answersFile.write(f'\n\t{section}.\n')32 if data_description:33 answersFile.write(f'\t{data_description}\n')34 tabs_number = 235 for key, value in answer_dict.items():36 if key not in unwanted_fields_in_dict:37 answersFile.write(f'{tab * tabs_number}{key} : {value}\n')38 if more_info:39 answersFile.write(f'\n\t{more_info} \n')40 def write_answer_from_string(self,41 answer: str,42 section: str = None,43 answer_description: str = None,44 data_description: str = None,45 more_info: str = None,46 question_number: str = None):47 tab = '\t'48 tabs_number = 149 if question_number is not None:50 self._write_question_number(question_number=question_number)51 with open(self.answers_file, 'a') as answersFile:52 if section:53 if answer_description:54 answersFile.write(f'\n\t{section}. {answer_description}\n')55 else:56 answersFile.write(f'\n\t{section}.\n')57 if data_description:58 answersFile.write(f'\t{data_description}\n')59 tabs_number = 260 answersFile.write(f'{tab * tabs_number}{answer}\n')61 if more_info:62 answersFile.write(f'\n\t{more_info} \n')63 def write_answer_from_dataframe(self,64 answer_dataframe: DataFrame,65 section: str = None,66 answer_description: str = None,67 data_description: str = None,68 unwanted_columns_in_dataframe: list = None,69 specific_wanted_columns_in_dataframe: list = None,70 write_dataframe_header: bool = True,71 write_dataframe_index: bool = False,72 more_info: str = None,73 question_number: str = None):74 tab = '\t'75 tabs_number = 176 if unwanted_columns_in_dataframe:77 answer_dataframe.drop(unwanted_columns_in_dataframe, axis=1)78 if specific_wanted_columns_in_dataframe:79 answer_dataframe = answer_dataframe[specific_wanted_columns_in_dataframe]80 if question_number is not None:81 self._write_question_number(question_number=question_number)82 with open(self.answers_file, 'a') as answersFile:83 if section:84 if answer_description:85 answersFile.write(f'\n\t{section}. {answer_description}\n')86 else:87 answersFile.write(f'\n\t{section}.\n')88 if data_description:89 answersFile.write(f'\t{data_description}\n')90 dataframe_as_string = answer_dataframe.to_string(header=write_dataframe_header,91 index=write_dataframe_index,92 justify='center')93 answersFile.write(f'\n{dataframe_as_string}\n\n')94 if more_info:...
main.py
Source:main.py
1import multiprocessing2import traceback3import xlsxwriter4import csv5import time6import math7import os8import pandas as pd9from tkinter import *10from tkinter import filedialog11RECORDS_PER_TAB = 100000012def convert_csv_xlsx(file_path):13 try:14 file_path = os.path.join(os.getcwd(), file_path)15 print('Reading file: ' + file_path)16 f = open(file_path, encoding="utf_8")17 qty_rows = get_qty_rows_from_file(f)18 data = csv.DictReader(f)19 new_file_path = file_path.replace('.csv', '')20 new_file_path += '.xlsx'21 print('Writting new file')22 create_xlsx(data, new_file_path, qty_rows)23 except Exception as e:24 print('Error while reading file {} : {}'.format(file_path, e))25def create_xlsx(data, file_path, qty_records):26 try:27 current = 128 tabs_number = math.ceil(qty_records / RECORDS_PER_TAB) if qty_records > RECORDS_PER_TAB else 129 workbook = xlsxwriter.Workbook(file_path, {'constant_memory':True})30 print('Splitting {} records into {} tabs'.format(qty_records, tabs_number))31 tabs = []32 index_cell_format = workbook.add_format({33 'bold': True, 34 'bg_color': '#f5f5f5',35 'align': 'center', 36 'valign': 'vcenter'})37 headers_format = workbook.add_format({38 'italic': True,39 'bold': True, 40 'bg_color': '#b39e66',41 'align': 'center', 42 'valign': 'vcenter'})43 for index in range(tabs_number):44 print('{} / {}'.format(index + 1, tabs_number))45 worksheet = workbook.add_worksheet('Sheet{}'.format(index + 1))46 limit = RECORDS_PER_TAB * (index + 1) if (index < (tabs_number - 1)) else qty_records47 print('From {} To {}'.format(current, limit))48 worksheet.write_row(0, 1, data.fieldnames, headers_format)49 row_pos = 150 for row_data in data:51 worksheet.write(row_pos, 0, current, index_cell_format)52 worksheet.write_row(row_pos, 1, row_data.values())53 row_pos += 154 current += 155 if current > limit:56 break57 for tab in tabs:58 tab.start()59 for tab in tabs:60 tab.join() 61 print('Saving File')62 workbook.close()63 print('File created: {}'.format(file_path))64 except Exception as e:65 print('Error while creating xlsx file {} : {}'.format(file_path, e))66def get_qty_rows_from_file(f):67 qty_rows = 068 for _ in f:69 qty_rows += 170 f.seek(0)71 return qty_rows -1 if qty_rows > 0 else qty_rows72class App:73 def __init__(self):74 self.window = Tk()75 self.window.title("csv to xlsx converter") # to define the title76 self.window.resizable(0, 0)77 self.window.configure(background='#ffffff')78 self.filename = None79 self.loaded = False80 self.label = Label(self.window , text = "File Explorer",81 width = 75, height = 4, background='#b39e66')82 btn_design = {'background':'#000000', 'foreground':'#ffffff', 'activebackground':'#b39e66',83 'width':20}84 self.button_browse = Button(self.window, text="Browse File", command=self.browse_file, **btn_design)85 self.button_exit = Button(self.window, text="Exit", command=self.window.destroy, **btn_design)86 87 self.label.grid(row=0, column=0, pady=10, columnspan=3)88 self.button_browse.grid(row=1, column=0, columnspan=3) 89 self.button_exit.grid(row=2, column=1, pady=5)90 91 def run(self):92 self.window.mainloop()93 94 def browse_file(self):95 self.loaded = False96 self.filename = filedialog.askopenfilename(97 parent = self.window,98 initialdir = "/",99 defaultextension='.csv',100 filetypes=[('CSV file','*.csv'), ('All files','*.*')],101 title = "Select a File")102 103 if self.filename:104 try:105 self.label.configure(text = 'Processing file: {self.filename}')106 convert_csv_xlsx(self.filename)107 self.label.configure(text = 'File converted successfully \n\n File saved in container folder')108 except Exception as e:109 self.label.configure(text = 'Error: {}'.format(e))110 else:111 self.label.configure(text = 'No file selected')112if __name__ == '__main__':...
06. Salary.py
Source:06. Salary.py
1tabs_number = int(input())2salary = int(input())3lost_money = 04if 1 <= tabs_number <= 10:5 for i in range(1, tabs_number + 1):6 tab_name = input()7 if tab_name == "Facebook":8 lost_money += 1509 elif tab_name == "Instagram":10 lost_money += 10011 elif tab_name == "Reddit":12 lost_money += 5013 if salary - lost_money <= 0:14 print("You have lost your salary.")15 exit(0)16money_left = salary - lost_money...
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!!