Skip to content

Commit 3ab8915

Browse files
committed
refactor: use better value unpacking in statistics
this also means that we can catch exceptions more easily
1 parent fbd247c commit 3ab8915

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

database/statistics.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ def get_user_stats(user_id):
4747
user = Database().get_user(user_id)
4848
lang_id = Database().get_lang_id(user_id)
4949

50-
played_games = int(user[5]) or 1
51-
won_games = user[6]
52-
last_played = int(user[8])
50+
try:
51+
played_games, won_games, _, last_played = user[4:8]
52+
except ValueError as e:
53+
logger.warning("Cannot unpack user - {}".format(e))
54+
raise
55+
56+
if played_games == 0:
57+
# prevent division by zero errors
58+
played_games = 1
59+
5360
last_played_formatted = datetime.utcfromtimestamp(last_played).strftime('%d.%m.%y %H:%M')
5461
win_percentage = round(float(won_games) / float(played_games), 4)
5562
bar = generate_bar_chart(win_percentage * 100)

0 commit comments

Comments
 (0)