Skip to content

Commit 9a66272

Browse files
committed
feat: reformat files
1 parent 3b280dc commit 9a66272

File tree

6 files changed

+76
-34
lines changed

6 files changed

+76
-34
lines changed

.vscode/settings.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.formatting.provider": "black"
3+
}

Week9/0-birthday/app.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import os
2-
31
from cs50 import SQL
4-
from flask import Flask, flash, jsonify, redirect, render_template, request, session
2+
from flask import Flask, redirect, render_template, request
53

64
# Configure application
75
app = Flask(__name__)

Week9/0-birthday/birthdays.db

0 Bytes
Binary file not shown.

Week9/1-finance/app.py

+56-27
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
from datetime import datetime
33

44
from cs50 import SQL
5-
from flask import Flask, flash, redirect, render_template, request, session
5+
from flask import Flask, redirect, render_template, request, session
66
from flask_session import Session
7-
from tempfile import mkdtemp
87
from werkzeug.security import check_password_hash, generate_password_hash
98

109
from helpers import apology, login_required, lookup, usd
@@ -44,10 +43,11 @@ def after_request(response):
4443
@login_required
4544
def index():
4645
"""Show portfolio of stocks"""
47-
cash = db.execute("SELECT cash FROM users WHERE id = ?",
48-
session["user_id"])
46+
cash = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
4947
orders = db.execute(
50-
"SELECT symbol, name, sum(shares) as sumshares, price FROM orders WHERE user_id = ? GROUP BY symbol", session["user_id"])
48+
"SELECT symbol, name, sum(shares) as sumshares, price FROM orders WHERE user_id = ? GROUP BY symbol",
49+
session["user_id"],
50+
)
5151
account = cash[0]["cash"]
5252
for order in orders:
5353
name = order["name"]
@@ -56,7 +56,9 @@ def index():
5656
order["total"] = total
5757
order["price"] = order["price"]
5858
account += total
59-
return render_template("index.html", orders=orders, cash=cash[0]["cash"], account=account)
59+
return render_template(
60+
"index.html", orders=orders, cash=cash[0]["cash"], account=account
61+
)
6062

6163

6264
@app.route("/buy", methods=["GET", "POST"])
@@ -81,17 +83,24 @@ def buy():
8183
except ValueError:
8284
return apology("Invalid number")
8385

84-
row = db.execute("SELECT cash FROM users where id = ?",
85-
session["user_id"])
86+
row = db.execute("SELECT cash FROM users where id = ?", session["user_id"])
8687
cash = row[0]["cash"]
8788
balance = cash - shares * quote["price"]
8889
if balance < 0:
8990
return apology("insufficient balance")
9091

91-
db.execute("UPDATE users SET cash = ? WHERE id = ?",
92-
balance, session["user_id"])
93-
db.execute("INSERT INTO orders (user_id, symbol, shares, name, price, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
94-
session["user_id"], symbol.upper(), shares, quote["name"], quote["price"], datetime.now())
92+
db.execute(
93+
"UPDATE users SET cash = ? WHERE id = ?", balance, session["user_id"]
94+
)
95+
db.execute(
96+
"INSERT INTO orders (user_id, symbol, shares, name, price, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
97+
session["user_id"],
98+
symbol.upper(),
99+
shares,
100+
quote["name"],
101+
quote["price"],
102+
datetime.now(),
103+
)
95104
return redirect("/")
96105

97106

@@ -100,7 +109,9 @@ def buy():
100109
def history():
101110
"""Show history of transactions"""
102111
orders = db.execute(
103-
"SELECT symbol, name, shares, price, timestamp FROM orders WHERE user_id = ?", session["user_id"])
112+
"SELECT symbol, name, shares, price, timestamp FROM orders WHERE user_id = ?",
113+
session["user_id"],
114+
)
104115
if not orders:
105116
return apology("No history", 403)
106117
else:
@@ -131,11 +142,14 @@ def login():
131142
return apology("must provide password", 403)
132143

133144
# Query database for username
134-
rows = db.execute("SELECT * FROM users WHERE username = ?",
135-
request.form.get("username"))
145+
rows = db.execute(
146+
"SELECT * FROM users WHERE username = ?", request.form.get("username")
147+
)
136148

137149
# Ensure username exists and password is correct
138-
if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
150+
if len(rows) != 1 or not check_password_hash(
151+
rows[0]["hash"], request.form.get("password")
152+
):
139153
return apology("invalid username and/or password", 403)
140154

141155
# Remember which user has logged in
@@ -188,12 +202,15 @@ def register():
188202
if not name or not ps1 or not ps1:
189203
return apology("please provide username and password!")
190204
if ps1 != ps2:
191-
return apology("password and confirmation don't match~")
205+
return apology("password and confirmation don't match")
192206
if db.execute("SELECT * FROM users WHERE username = ?", name):
193207
return apology("user already exist!")
194208

195-
db.execute("INSERT INTO users (username, hash) VALUES (?, ?)",
196-
name, generate_password_hash(ps1))
209+
db.execute(
210+
"INSERT INTO users (username, hash) VALUES (?, ?)",
211+
name,
212+
generate_password_hash(ps1),
213+
)
197214
return redirect("/")
198215

199216

@@ -203,7 +220,9 @@ def sell():
203220
"""Sell shares of stock"""
204221
if request.method == "GET":
205222
symbols = db.execute(
206-
"SELECT symbol FROM orders WHERE user_id = ? GROUP BY symbol", session["user_id"])
223+
"SELECT symbol FROM orders WHERE user_id = ? GROUP BY symbol",
224+
session["user_id"],
225+
)
207226
return render_template("sell.html", symbol=symbols)
208227
else:
209228
symbol = request.form.get("symbol")
@@ -216,17 +235,27 @@ def sell():
216235
return apology("Invalid symbol or shares")
217236

218237
sumshares = db.execute(
219-
"SELECT symbol, price, name, SUM(shares) as sumshares FROM orders WHERE user_id = ? AND symbol = ?", session["user_id"], symbol)
238+
"SELECT symbol, price, name, SUM(shares) as sumshares FROM orders WHERE user_id = ? AND symbol = ?",
239+
session["user_id"],
240+
symbol,
241+
)
220242

221243
if shares > sumshares[0]["sumshares"]:
222244
return apology("You don't have so many shares")
223245

224-
db.execute("INSERT INTO orders (user_id, symbol, shares, name, price, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
225-
session["user_id"], symbol, -shares, sumshares[0]["name"], sumshares[0]["price"], datetime.now())
246+
db.execute(
247+
"INSERT INTO orders (user_id, symbol, shares, name, price, timestamp) VALUES (?, ?, ?, ?, ?, ?)",
248+
session["user_id"],
249+
symbol,
250+
-shares,
251+
sumshares[0]["name"],
252+
sumshares[0]["price"],
253+
datetime.now(),
254+
)
226255
sold = shares * sumshares[0]["price"]
227256

228-
cash = db.execute("select cash FROM users WHERE id = ?",
229-
session["user_id"])[0]["cash"]
230-
db.execute("UPDATE users SET cash = ? WHERE id = ?",
231-
cash + sold, session["user_id"])
257+
cash = db.execute("select cash FROM users WHERE id = ?", session["user_id"])[0]["cash"]
258+
db.execute(
259+
"UPDATE users SET cash = ? WHERE id = ?", cash + sold, session["user_id"]
260+
)
232261
return redirect("/")

Week9/1-finance/finance.db

0 Bytes
Binary file not shown.

Week9/1-finance/helpers.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22
import requests
33
import urllib.parse
44

5-
from flask import redirect, render_template, request, session
5+
from flask import redirect, render_template, session
66
from functools import wraps
77

88

99
def apology(message, code=400):
1010
"""Render message as an apology to user."""
11+
1112
def escape(s):
1213
"""
1314
Escape special characters.
1415
1516
https://github.com/jacebrowning/memegen#special-characters
1617
"""
17-
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"),
18-
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]:
18+
for old, new in [
19+
("-", "--"),
20+
(" ", "-"),
21+
("_", "__"),
22+
("?", "~q"),
23+
("%", "~p"),
24+
("#", "~h"),
25+
("/", "~s"),
26+
('"', "''"),
27+
]:
1928
s = s.replace(old, new)
2029
return s
30+
2131
return render_template("apology.html", top=code, bottom=escape(message)), code
2232

2333

@@ -27,11 +37,13 @@ def login_required(f):
2737
2838
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
2939
"""
40+
3041
@wraps(f)
3142
def decorated_function(*args, **kwargs):
3243
if session.get("user_id") is None:
3344
return redirect("/login")
3445
return f(*args, **kwargs)
46+
3547
return decorated_function
3648

3749

@@ -53,7 +65,7 @@ def lookup(symbol):
5365
return {
5466
"name": quote["companyName"],
5567
"price": float(quote["latestPrice"]),
56-
"symbol": quote["symbol"]
68+
"symbol": quote["symbol"],
5769
}
5870
except (KeyError, TypeError, ValueError):
5971
return None

0 commit comments

Comments
 (0)