Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit c60fde5

Browse files
committed
Fix code style issues with Black
1 parent 755db98 commit c60fde5

8 files changed

Lines changed: 351 additions & 224 deletions

File tree

projects/CRUD-with-postgresql/main.py

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
from rich.text import Text
1414
from rich.panel import Panel
1515
from rich.prompt import Prompt, Confirm
16+
1617
# For working with postgres
1718
import psycopg2
1819
from psycopg2 import Error
1920
import json
2021
import string
2122
import random
23+
2224
console = Console()
2325

2426
try:
@@ -33,25 +35,29 @@
3335
# If you are building something serious, use a more secure method
3436
# Like a .env file
3537
# The creds.json is only being used to make the code more readable
36-
connection = psycopg2.connect(user=str(secrets["username"]),
37-
password=str(secrets["password"]),
38-
host=str(secrets["host"]),
39-
port=str(secrets["port"]),
40-
database=str(secrets["database"]))
41-
38+
connection = psycopg2.connect(
39+
user=str(secrets["username"]),
40+
password=str(secrets["password"]),
41+
host=str(secrets["host"]),
42+
port=str(secrets["port"]),
43+
database=str(secrets["database"]),
44+
)
45+
4246
# Get the info of the connection using the get_dsn_parameters() method
4347
info = connection.get_dsn_parameters()
4448
except:
4549
console.print(
46-
":red_circle:Sorry, something went wrong! Maybe a wrong credential? Check stuff.json's info correctly")
50+
":red_circle:Sorry, something went wrong! Maybe a wrong credential? Check stuff.json's info correctly"
51+
)
4752

4853
# Define a new cursor object by calling the cursor() method
4954
# This is the object we use to interact with the database
5055
# Think of it as a pointer to a specific place in the database
5156
cursor = connection.cursor()
5257

5358
print(
54-
f'{info["user"]} is connected to PostgreSQL server on {info["host"]}:{info["port"]}')
59+
f'{info["user"]} is connected to PostgreSQL server on {info["host"]}:{info["port"]}'
60+
)
5561

5662
# Generate hash for assigning to a entry
5763
def hash_gen():
@@ -69,73 +75,83 @@ def hash_gen():
6975
# The name, url, author have to be filled
7076
# Rest is auto generated by the program and the database
7177
def check_table():
72-
cursor.execute("""
78+
cursor.execute(
79+
"""
7380
CREATE TABLE IF NOT EXISTS dbview(
7481
name TEXT NOT NULL,
7582
url TEXT NOT NULL,
7683
author TEXT,
7784
hash TEXT NOT NULL,
7885
date TIMESTAMP
7986
);
80-
""")
87+
"""
88+
)
8189
console.log(":green_circle: Connected to Table :man: dbview")
8290

8391
# Insert a new entry into the table
8492
# The hash is auto generated
8593
def insert(name, url, author):
86-
cursor.execute(f"""
94+
cursor.execute(
95+
f"""
8796
INSERT INTO dbview (name, url, author, hash, date) VALUES ('{name}', '{url}', '{author}', '{str(hash_gen())}', CURRENT_TIMESTAMP)
88-
""")
97+
"""
98+
)
8999
search("name", name)
90100

91101
# View the table using a rich table
92102
def table():
93103
cursor.execute("SELECT * from dbview")
94104
records = cursor.fetchall()
95105
try:
96-
table = Table(title=Panel(
97-
f'Query DB [red]{info["host"]}:{info["port"]}'), show_header=True, header_style="bold green")
106+
table = Table(
107+
title=Panel(f'Query DB [red]{info["host"]}:{info["port"]}'),
108+
show_header=True,
109+
header_style="bold green",
110+
)
98111
table.add_column("Name", style="purple", width=20)
99112
table.add_column("Url", style="yellow")
100113
table.add_column("Author", style="cyan")
101114
table.add_column("Hash", style="dim")
102115
table.add_column("Date", no_wrap=True, style="dim blue")
103116
for record in records:
104117
table.add_row(
105-
record[0],
106-
record[1],
107-
record[2],
108-
record[3],
109-
str(record[4])
118+
record[0], record[1], record[2], record[3], str(record[4])
110119
)
111120
console.print(table)
112121
except:
113122
# Some terminals don't support rich
114123
print("Name\t\t | URL\t\t | Channel\t\t | Hash\t\t | Date")
115124
for record in records:
116125
print(
117-
f'{record[0]}\t\t | {record[1]}\t\t | {record[2]}\t\t | {record[3]}\t\t| {str(record[4])}\t\t')
126+
f"{record[0]}\t\t | {record[1]}\t\t | {record[2]}\t\t | {record[3]}\t\t| {str(record[4])}\t\t"
127+
)
118128

119129
# Edit a entry in the table
120130
def edit(key, value):
121131
console.print(
122-
"Enter The New Values to Update in the form of a name url channel")
132+
"Enter The New Values to Update in the form of a name url channel"
133+
)
123134
new_record = str(Prompt.ask("Enter Here: "))
124135
new_records = new_record.split()
125136
# Execute the query to update the table
126137
cursor.execute(
127-
f"UPDATE dbview SET name='{new_records[0]}', url='{new_records[1]}', author='{new_records[2]}' WHERE {key}='{value}'")
138+
f"UPDATE dbview SET name='{new_records[0]}', url='{new_records[1]}', author='{new_records[2]}' WHERE {key}='{value}'"
139+
)
128140
console.log(":green_circle: Successfully Updated Database")
129141
table()
130142

131143
# Get the hash of a entry
132144
def hash():
133145
console.print("Enter A Key and Value to get Hash")
134-
record_key = str(Prompt.ask("Enter a Key", choices=[
135-
"name", "url", "author", "hash", "date"], default="name"))
146+
record_key = str(
147+
Prompt.ask(
148+
"Enter a Key",
149+
choices=["name", "url", "author", "hash", "date"],
150+
default="name",
151+
)
152+
)
136153
record_value = str(Prompt.ask("Enter a Value"))
137-
cursor.execute(
138-
f"SELECT * from dnview WHERE {record_key}='{record_value}'")
154+
cursor.execute(f"SELECT * from dnview WHERE {record_key}='{record_value}'")
139155
records = cursor.fetchone()
140156
console.print(f"The Hash is {records[3]}")
141157

@@ -149,12 +165,14 @@ def delete(key, value):
149165
# Drop the table
150166
def drop():
151167
console.print(
152-
"Are you sure you want to Drop the database? This will delete the whole table")
168+
"Are you sure you want to Drop the database? This will delete the whole table"
169+
)
153170
confirmation = Confirm.ask("Are you sure")
154171
if confirmation:
155172
cursor.execute("DROP table dbview;")
156173
console.print(
157-
"Dropped Table dbview: Restart the application to start a new main table")
174+
"Dropped Table dbview: Restart the application to start a new main table"
175+
)
158176
else:
159177
console.print("Skipped the drop operation")
160178

@@ -165,30 +183,33 @@ def drop():
165183
def search(key, value):
166184
cursor.execute(f"SELECT * from dbview WHERE {key}='{value}'")
167185
records = cursor.fetchall()
168-
table = Table(title=Panel(
169-
f'Query DB [red]{info["host"]}:{info["port"]}'), show_header=True, header_style="bold green")
186+
table = Table(
187+
title=Panel(f'Query DB [red]{info["host"]}:{info["port"]}'),
188+
show_header=True,
189+
header_style="bold green",
190+
)
170191
table.add_column("Name", style="purple", width=20)
171192
table.add_column("Url", style="yellow")
172193
table.add_column("Channel", style="cyan")
173194
table.add_column("Hash", style="dim")
174195
table.add_column("Date", no_wrap=True, style="dim blue")
175196
for record in records:
176-
table.add_row(
177-
record[0],
178-
record[1],
179-
record[2],
180-
record[3],
181-
str(record[4])
182-
)
197+
table.add_row(record[0], record[1], record[2], record[3], str(record[4]))
183198
console.print(table)
199+
184200
loop = 1
185201
# 12 is a rather arbitrary number
186202
# I know. But after a while, you don't want the program to run forever
187203
while loop != 12:
188204
check_table()
189205
# Just the normal CRUD operations
190-
action = str(Prompt.ask("Enter your name", choices=[
191-
"view", "new", "edit", "delete", "drop", "quit", "hash"], default="view"))
206+
action = str(
207+
Prompt.ask(
208+
"Enter your name",
209+
choices=["view", "new", "edit", "delete", "drop", "quit", "hash"],
210+
default="view",
211+
)
212+
)
192213
if action == "view":
193214
table()
194215
if action == "new":
@@ -197,15 +218,25 @@ def search(key, value):
197218
author = Prompt.ask("Enter The Author")
198219
insert(name, url, author)
199220
if action == "delete":
200-
record_key = str(Prompt.ask("Enter the key", choices=[
201-
"name", "url", "author", "hash", "date"], default="name"))
221+
record_key = str(
222+
Prompt.ask(
223+
"Enter the key",
224+
choices=["name", "url", "author", "hash", "date"],
225+
default="name",
226+
)
227+
)
202228
record_value = str(Prompt.ask("Enter the Value"))
203229
delete(record_key, record_value)
204230
if action == "drop":
205231
drop()
206232
if action == "edit":
207-
record_key = str(Prompt.ask("Enter the key", choices=[
208-
"name", "url", "author", "hash", "date"], default="name"))
233+
record_key = str(
234+
Prompt.ask(
235+
"Enter the key",
236+
choices=["name", "url", "author", "hash", "date"],
237+
default="name",
238+
)
239+
)
209240
record_value = str(Prompt.ask("Enter the Value"))
210241
edit(record_key, record_value)
211242
if action == "hash":
@@ -218,8 +249,8 @@ def search(key, value):
218249
print("Error while connecting to PostgreSQL", error)
219250
# We finally close the connection
220251
finally:
221-
if (connection):
252+
if connection:
222253
connection.commit()
223254
cursor.close()
224255
connection.close()
225-
console.print(":red_heart: Thanks for using db.py")
256+
console.print(":red_heart: Thanks for using db.py")

projects/Captcha_Genrator/Audio_Captcha.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,41 @@
77

88
audio = AudioCaptcha()
99
pygame.mixer.init()
10-
captcha_text=""
10+
captcha_text = ""
11+
1112

1213
def create_audio_captcha():
1314
global captcha_text
14-
captcha_text=str(randint(1000, 9999))
15+
captcha_text = str(randint(1000, 9999))
1516
print(captcha_text)
16-
audio.write(captcha_text, "audio"+'.wav')
17+
audio.write(captcha_text, "audio" + ".wav")
18+
1719

1820
create_audio_captcha()
1921

20-
root=Tk()
22+
root = Tk()
2123
root.title("Audio Captcha")
2224

25+
2326
def play_audio():
24-
pygame.mixer.music.load("audio"+'.wav')
27+
pygame.mixer.music.load("audio" + ".wav")
2528
pygame.mixer.music.play()
2629

30+
2731
def verify_audio():
2832
text = check.get("1.0", "end-1c")
2933
global captcha_text
30-
if(text==captcha_text):
34+
if text == captcha_text:
3135
messagebox.showinfo("SUCCESS", "Verified")
3236
else:
3337
messagebox.showinfo("ALERT", "Not Verified")
3438

39+
3540
heading_label = Label(root, text="Enter the Audio Captcha", height=2, width=50)
3641
check = Text(root, height=2, width=50)
3742
play_button = Button(root, text="Play Audio", command=play_audio)
38-
submit=Button(root, text="Submit", command=verify_audio)
39-
renew=Button(root, text="Renew", command=create_audio_captcha)
43+
submit = Button(root, text="Submit", command=verify_audio)
44+
renew = Button(root, text="Renew", command=create_audio_captcha)
4045

4146
heading_label.pack()
4247
check.pack()
@@ -45,4 +50,3 @@ def verify_audio():
4550
submit.pack(side=RIGHT, padx=35, pady=5)
4651

4752
root.mainloop()
48-

0 commit comments

Comments
 (0)