Today I leant about error handling and what JSON files are. And as for my project, I updated two previous projects, the NATO Alphabet with thr try:, except:, else:, and finally: commands and the password manager but with JSON files rather than text ones. I also added an additional feature that allows you to search for the details of the website you enter into the entry. I also plan to start a GitHub account soon for my upcoming projects, as they are going to get more and more advanced and it is better to post them in GitHub rather than copy paste them here. I will still make posts talking about the projects though.
NATO Alphabet
import csv
#TODO 1. Create a dictionary using dictionary comprehension:
with open("nato_phonetic_alphabet.csv") as file:
reader = csv.reader(file)
alphabet = {row[0]: row[1] for row in reader if row[0] != "letter"}
print(alphabet)
#TODO 2. Create a list of the phonetic code words from a word that the user inputs.
name = input("Enter your name: ")
on = True
while on:
try:
broken_name = [letter.upper() for letter in name]
words = [alphabet[assigned_letter] for assigned_letter in broken_name]
except KeyError:
print("Only letters in the alphabet are accepted.")
else:
print("The NATO phonetic words for you name is: \n", words)
finally:
run = str(input("Would you like to continue? (y/n)")).lower()
if run == "y":
name = input("Enter your name: ")
else:
on = False
Password Manager
from tkinter import *
from tkinter import messagebox
from random import choice, randint, shuffle
import pyperclip
import json
#---------------------------- PASSWORD GENERATOR ------------------------------- #
def generate_password():
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
password_letters = [choice(letters) for _ in range(randint(8, 10))]
password_symbols = [choice(symbols) for _ in range(randint(2, 4))]
password_numbers = [choice(numbers) for _ in range(randint(2, 4))]
password_list = password_letters + password_symbols + password_numbers
shuffle(password_list)
password = "".join(password_list)
password_entry.insert(0, password)
pyperclip.copy(password)
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save_data():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
dict = {website: {"email": email, "password": password}}
if len(website) == 0 or len(email) == 0 or len(password) == 0:
messagebox.showerror("Error", "Please fill in all fields")
else:
is_ok = messagebox.askokcancel(title=website, message=f"Here are the details: \nPassword: {password}"
f"\nEmail: {email} \n Is it okay to save?")
if is_ok:
try:
with open("data.json", "r") as d_file:
dictt = json.load(d_file)
except FileNotFoundError:
with open("data.json", "w") as file:
json.dump(dict, file, indent=4)
else:
dictt.update(dict)
with open("data.json", "w") as file:
json.dump(dictt, file, indent=4)
finally:
website_entry.delete(0, END)
password_entry.delete(0, END)
def find_password():
website=str(website_entry.get())
print(website)
with open("data.json", "r") as d_file:
data = json.load(d_file)
if website in data:
messagebox.showinfo(f"Search: {website}", f"email : {data[website]["email"]} \n password : {data[website]["password"]}")
else:
messagebox.showerror("Website Error", "This website is currently not in the list.")
# ---------------------------- UI SETUP ------------------------------- #
window = Tk()
window.title("Password Manager")
window.minsize(500, 250)
window.config(padx=30, pady=30)
canvas = Canvas(width=200, height=200)
logo = PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=logo)
canvas.grid(column=1, row=0)
website_label = Label(window, text="Website:")
website_label.grid(column=0, row=1)
website_entry = Entry(width=50)
website_entry.focus()
website_entry.grid(column=1,row=1, columnspan=2)
website_search = Button(text=" Search ", command=find_password)
website_search.grid(column=1, row=1, padx=5,sticky="e")
email_label = Label(window, text="Email/Username:")
email_label.grid(column=0, row=2)
email_entry = Entry(width=50)
email_entry.insert(0,"example@gmail.com")
email_entry.grid(column=1,row=2, columnspan=2)
password_label = Label(window, text="Password:")
password_label.grid(column=0, row=3)
password_entry = Entry(width=50)
password_entry.grid(column=1,row=3, padx=5, sticky="w")
password_button = Button(text="Generate Password", command=generate_password)
password_button.grid(column=1, row=3, padx=5,sticky="e")
add_button = Button(text="Add", width=42, command=save_data)
add_button.grid(column=1, row=4, columnspan=2)
window.mainloop()
Top comments (0)