Best Python code snippet using hypothesis
ui.py
Source:ui.py
1from tkinter import *2from tkinter import messagebox3from scripts import data_manager4from scripts import password_generator5import pyperclip6BACKGROUND_COLOUR = '#495664'7FOREGROUND_COLOUR = '#f6f7d3'8DARK_TEXT_COLOUR = '#333c4a'9class UI:10 def __init__(self):11 # Init objects12 self.data_manager_obj = data_manager.DataManager()13 # Setup files if first start14 if self.data_manager_obj.is_first_start():15 self.data_manager_obj.setup_obj.run_setup()16 # Create login window if not first start17 if not self.data_manager_obj.is_first_start():18 self.login_window = Tk()19 self.login_window.title('Login to Password Manager')20 self.login_window.config(padx=60, pady=50, bg=BACKGROUND_COLOUR)21 # Master user credentials22 self.master_username = StringVar()23 self.master_password = StringVar()24 # Init other variables required25 self.main_window = None26 self.main_image = None27 self.symbols_checked = None28 self.letters_checked = None29 self.numbers_checked = None30 self.spinbox_pass_length = None31 self.add_new_tag = None32 self.add_new_username = None33 self.add_new_pass = None34 self.select_tag = None35 self.tags_option_menu = None36 self.user_listbox = None37 self.pass_listbox = None38 # Init login window objects39 self.init_login_window()40 self.login_window.mainloop()41 def init_login_window(self):42 user_label = Label(text='Username: ', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=20)43 user_label.grid(row=0, column=0)44 user_entry = Entry(width=30, textvariable=self.master_username)45 user_entry.grid(row=0, column=1)46 pass_label = Label(text='Password: ', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=20)47 pass_label.grid(row=1, column=0)48 pass_entry = Entry(width=30, textvariable=self.master_password, show='*')49 pass_entry.grid(row=1, column=1)50 go_btn = Button(text='Go', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, command=self.login_pressed, pady=10)51 go_btn.grid(row=2, column=2)52 user_entry.focus()53 def login_pressed(self):54 username = self.master_username.get()55 password = self.master_password.get()56 if username and password:57 self.master_username.set('')58 self.master_password.set('')59 check_username, check_password = self.data_manager_obj.get_master_details()60 if str(check_username) == str(username):61 if str(check_password) == str(password):62 self.create_main_window()63 else:64 messagebox.showerror(title='Incorrect', message='Please check the password.')65 else:66 messagebox.showerror(title='Incorrect', message='Please check the username.')67 else:68 messagebox.showerror(title='Empty field(s)?', message='Please don\'t leave any field(s) empty.')69 def create_main_window(self):70 self.login_window.destroy()71 self.main_window = Tk()72 self.main_window.title('Password Manager')73 self.main_window.config(padx=50, pady=50, bg=BACKGROUND_COLOUR)74 main_canvas = Canvas(width=600, height=600)75 main_canvas.config(bg=BACKGROUND_COLOUR, highlightthickness=0)76 self.main_image = PhotoImage(file='images/password-manager.png')77 main_canvas.create_image(300, 300, image=self.main_image)78 main_canvas.grid(row=0, column=1)79 tags_label = Label(text='TAG:', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=50)80 tags_label.grid(row=1, column=0)81 self.select_tag = StringVar()82 tags_list = self.data_manager_obj.get_saved_password_tags()83 self.tags_option_menu = OptionMenu(self.main_window, self.select_tag, *tags_list)84 self.select_tag.set(tags_list[0])85 self.tags_option_menu.grid(row=1, column=1)86 search_btn = Button(text='Search', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=10,87 command=self.list_passwords_clicked)88 search_btn.grid(row=1, column=3)89 add_btn = Button(text='Add a new entry', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=10,90 command=self.add_new_password_clicked)91 add_btn.grid(row=2, column=0)92 gen_pass_btn = Button(text='Generate Password', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=10,93 command=self.generate_password_clicked)94 gen_pass_btn.grid(row=2, column=1)95 def list_passwords_clicked(self):96 self.create_list_pass_window(master=self.main_window)97 def create_list_pass_window(self, master):98 list_pass_window = Toplevel(master=master)99 tag_choice = str(self.select_tag.get()).lower()100 list_pass_window.title(f'List of passwords for {tag_choice}')101 list_pass_window.config(padx=50, pady=50, bg=BACKGROUND_COLOUR)102 intruct_label = Label(master=list_pass_window, text='Click on item to copy', bg=BACKGROUND_COLOUR,103 fg=FOREGROUND_COLOUR, pady=10)104 intruct_label.grid(row=0, column=0)105 count, user_list, pass_list = self.data_manager_obj.get_all_passwords(tag_choice)106 self.user_listbox = Listbox(master=list_pass_window, height=count)107 for i in range(count):108 self.user_listbox.insert(i, user_list[i])109 self.user_listbox.grid(row=1, column=0)110 self.pass_listbox = Listbox(master=list_pass_window, height=count)111 for i in range(count):112 self.pass_listbox.insert(i, pass_list[i])113 self.pass_listbox.grid(row=1, column=1)114 self.user_listbox.bind("<<ListboxSelect>>", self.user_listbox_used)115 self.pass_listbox.bind("<<ListboxSelect>>", self.pass_listbox_used)116 def user_listbox_used(self, event):117 if self.user_listbox.curselection():118 pyperclip.copy(self.user_listbox.get(self.user_listbox.curselection()))119 messagebox.showinfo(title='Copied',120 message='Copied this item!')121 def pass_listbox_used(self, event):122 if self.pass_listbox.curselection():123 pyperclip.copy(self.pass_listbox.get(self.pass_listbox.curselection()))124 messagebox.showinfo(title='Copied',125 message='Copied this item!')126 def generate_password_clicked(self):127 self.create_gen_pass_window(master=self.main_window)128 def create_gen_pass_window(self, master):129 generate_pass_window = Toplevel(master=master)130 generate_pass_window.title('Generate a new password')131 generate_pass_window.config(padx=50, pady=50, bg=BACKGROUND_COLOUR)132 self.symbols_checked = IntVar()133 self.letters_checked = IntVar()134 self.numbers_checked = IntVar()135 symbols_check = Checkbutton(master=generate_pass_window, text='Symbols', variable=self.symbols_checked, pady=10)136 symbols_check.config(bg=BACKGROUND_COLOUR, highlightthickness=0)137 symbols_check.grid(row=0, column=0)138 letters_check = Checkbutton(master=generate_pass_window, text='Letters', variable=self.letters_checked, pady=10)139 letters_check.config(bg=BACKGROUND_COLOUR, highlightthickness=0)140 letters_check.grid(row=1, column=0)141 numbers_check = Checkbutton(master=generate_pass_window, text='Numbers', variable=self.numbers_checked, pady=10)142 numbers_check.config(bg=BACKGROUND_COLOUR, highlightthickness=0)143 numbers_check.grid(row=2, column=0)144 self.spinbox_pass_length = Spinbox(master=generate_pass_window, from_=8, to=128, width=5)145 self.spinbox_pass_length.grid(row=3, column=0)146 go_btn = Button(master=generate_pass_window, text='Go', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR,147 command=self.generate_password, pady=10)148 go_btn.grid(row=4, column=1)149 def generate_password(self):150 symbols = self.symbols_checked.get()151 letters = self.letters_checked.get()152 numbers = self.numbers_checked.get()153 pass_length = self.spinbox_pass_length.get()154 password = password_generator.generate_password(has_symbols=bool(symbols),155 has_letters=bool(letters),156 has_numbers=bool(numbers),157 pass_length=int(pass_length))158 messagebox.showinfo(title='Password Generated!',159 message=f'Password is copied to clipboard! \nYour password is: {password}')160 def add_new_password_clicked(self):161 self.create_add_new_password_window(master=self.main_window)162 def create_add_new_password_window(self, master):163 add_new_pass_window = Toplevel(master=master)164 add_new_pass_window.title('Add a new password entry')165 add_new_pass_window.config(padx=50, pady=50, bg=BACKGROUND_COLOUR)166 tag_label = Label(master=add_new_pass_window, text='TAG: ', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR, pady=20)167 tag_label.grid(row=0, column=0)168 self.add_new_tag = StringVar()169 tag_entry = Entry(master=add_new_pass_window, width=30, textvariable=self.add_new_tag)170 tag_entry.grid(row=0, column=1)171 user_label = Label(master=add_new_pass_window, text='USERNAME: ', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR,172 pady=20)173 user_label.grid(row=1, column=0)174 self.add_new_username = StringVar()175 user_entry = Entry(master=add_new_pass_window, width=30, textvariable=self.add_new_username)176 user_entry.grid(row=1, column=1)177 pass_label = Label(master=add_new_pass_window, text='PASSWORD: ', bg=BACKGROUND_COLOUR, fg=FOREGROUND_COLOUR,178 pady=20)179 pass_label.grid(row=2, column=0)180 self.add_new_pass = StringVar()181 pass_entry = Entry(master=add_new_pass_window, width=30, textvariable=self.add_new_pass)182 pass_entry.grid(row=2, column=1)183 add_pass_btn = Button(master=add_new_pass_window, text='Add this password', bg=BACKGROUND_COLOUR,184 fg=FOREGROUND_COLOUR, pady=10, command=self.password_add_clicked)185 add_pass_btn.grid(row=3, column=1)186 def password_add_clicked(self):187 tag_value = str(self.add_new_tag.get())188 user_value = str(self.add_new_username.get())189 pass_value = str(self.add_new_pass.get())190 if tag_value and user_value and pass_value:191 tag_value = tag_value.lower()192 is_okay = messagebox.askokcancel(title='Confirm save?',193 message=f'Are you sure you want to proceed with this info?\n' +194 f'Tag: {tag_value}\n' +195 f'Username: {user_value}\n' +196 f'Password: {pass_value}')197 if is_okay:198 self.data_manager_obj.add_new_password(tag=tag_value, user=user_value, password=pass_value)199 messagebox.showinfo(title='Success!',200 message='The save operation was successful!')201 # Refresh tags list in the main app screen202 self.tags_option_menu['menu'].delete(0, "end")203 for string in self.data_manager_obj.get_saved_password_tags():204 self.tags_option_menu['menu'].add_command(label=string,205 command=lambda value=string: self.select_tag.set(value))206 self.add_new_tag.set('')207 self.add_new_username.set('')208 self.add_new_pass.set('')209 else:210 self.add_new_tag.set('')211 self.add_new_username.set('')...
pass_manager.py
Source:pass_manager.py
...19 pass_map = dict()20 def __init__(self):21 pass22 @staticmethod23 def add_new_pass(name, pass_):24 if name not in PassManager.pass_map:25 PassManager.pass_map[name] = pass_26 @staticmethod27 def clear():28 PassManager.passes = list()29 @staticmethod30 def lookup(name):31 return PassManager.pass_map[name]32def pass_register(cls):33 name = cls.get_name()34 PassManager.add_new_pass(name, cls)...
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!!