Skip to content

Commit 2b92cd1

Browse files
committed
Add type annotations
1 parent e4ef7cc commit 2b92cd1

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

app.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import math
66
from flask import Flask, render_template, redirect, url_for, request
77
from flask_sqlalchemy import SQLAlchemy
8+
from werkzeug.wrappers import Response
89

910
app = Flask(__name__)
1011
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///users.db"
@@ -16,17 +17,20 @@ class User(db.Model): # user class
1617
A user model to store the level and experience points (XP).
1718
"""
1819

19-
id = db.Column(db.Integer, primary_key=True,
20-
unique=True, nullable=False) # user id
21-
username = db.Column(db.String(80), unique=True,
22-
nullable=False) # username
23-
xp = db.Column(db.Float, default=0, nullable=False) # user XP
24-
xp_required = db.Column(db.Float, default=1,
25-
nullable=False) # user XP required
26-
total_xp = db.Column(db.Float, default=0, nullable=False) # user total XP
27-
level = db.Column(db.Integer, default=1, nullable=False) # user level
28-
29-
def add_xp(self, amount): # add XP
20+
id: int = db.Column(
21+
db.Integer, primary_key=True, unique=True, nullable=False
22+
) # user id
23+
username: str = db.Column(
24+
db.String(80), unique=True, nullable=False) # username
25+
xp: float = db.Column(db.Float, default=0, nullable=False) # user XP
26+
xp_required: float = db.Column(
27+
db.Float, default=1, nullable=False
28+
) # user XP required
29+
total_xp: float = db.Column(
30+
db.Float, default=0, nullable=False) # user total XP
31+
level: int = db.Column(db.Integer, default=1, nullable=False) # user level
32+
33+
def add_xp(self, amount: float) -> None: # add XP
3034
"""
3135
Add XP (experience points) to the user.
3236
amount - the amount to add XP.
@@ -35,7 +39,7 @@ def add_xp(self, amount): # add XP
3539
self.total_xp += amount # add total XP by amount
3640
self.check_level_up() # check if user has leveled up
3741

38-
def check_level_up(self): # check if user has leveled up
42+
def check_level_up(self) -> None: # check if user has leveled up
3943
"""
4044
Check if the user has leveled up.
4145
"""
@@ -52,27 +56,28 @@ def check_level_up(self): # check if user has leveled up
5256
) # increase XP required exponentially with slower growth at higher levels
5357
self.level += 1 # increase level
5458

55-
def get_xp_required(self): # get required XP to next level
59+
def get_xp_required(self) -> float: # get required XP to next level
5660
"""
5761
Get the required XP for the user to level up.
5862
"""
5963
return self.xp_required
6064

61-
def get_level_progress(self): # get level progress
65+
def get_level_progress(self) -> float: # get level progress
6266
"""
6367
Get the level progress as a percentage.
6468
"""
6569
return (self.xp / self.xp_required) * 100
6670

6771

6872
@app.template_filter("short_numeric") # short numeric filter
69-
# get number in short numeric form with abbreviations
70-
def short_numeric_filter(value):
73+
def short_numeric_filter(
74+
value: float,
75+
) -> str: # get number in short numeric form with abbreviations
7176
"""
7277
Get the abbreviated numeric value.
7378
value - the numeric value to convert.
7479
"""
75-
units = [
80+
units: list[str] = [
7681
"",
7782
"K",
7883
"M",
@@ -97,7 +102,7 @@ def short_numeric_filter(value):
97102
"V",
98103
] # list of units with abbreviations
99104
exponent = 0
100-
mantissa = value # mantissa value from 1 to 999
105+
mantissa: float = value # mantissa value from 1 to 999
101106
while mantissa >= 1000: # repeat until mantissa is within 1 to 999
102107
mantissa /= 1000
103108
exponent += 1
@@ -111,27 +116,27 @@ def short_numeric_filter(value):
111116

112117

113118
@app.route("/") # index page
114-
def index(): # get index page template
119+
def index() -> str: # get index page template
115120
"""
116121
Return the index page containing a user.
117122
"""
118-
user = User.query.first() # get first user
123+
user: User | None = User.query.first() # get first user
119124
# redirect to index page template
120125
return render_template("index.html", user=user)
121126

122127

123128
@app.route("/add_xp", methods=["POST"]) # add XP from POST method
124-
def add_xp(): # add XP
129+
def add_xp() -> Response: # add XP
125130
"""
126131
Add XP (experience points) based on entered amount.
127132
"""
128-
user = User.query.first() # get first user
133+
user: User | None = User.query.first() # get first user
129134
user.add_xp(float(request.form["amount"])) # parse amount as float
130135
db.session.commit() # commit database changes
131136
return redirect(url_for("index")) # redirect to index page template
132137

133138

134-
def init_db(): # initialize database
139+
def init_db() -> None: # initialize database
135140
"""
136141
Initialize the user database.
137142
"""

0 commit comments

Comments
 (0)