-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpa_values.py
180 lines (151 loc) · 5.75 KB
/
gpa_values.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# pylint: disable=line-too-long import-error
"""
Get GPA values from results.csv for all users and filter according to the request
Functions:
get_gpa(user_id: str, step: int) -> List[str]
calculate_gpa(user_nic: str) -> str
academic_status(cgpa: float) -> str
Author: @dilshan-h (https://github.com/dilshan-h)
"""
import os
from typing import List
import csv
from random import choice
from functools import lru_cache
from dotenv import load_dotenv
from cryptography.fernet import Fernet
load_dotenv()
BASE_DIR: str = os.path.dirname(os.path.abspath(__file__))
data_path: str = os.path.join(BASE_DIR, "DATA", "results.csv.crypt")
fernet = Fernet(os.environ["SECRET_KEY"])
@lru_cache(maxsize=16)
def get_gpa(user_id: str, step: int) -> List[str]:
"""Get GPA data from results.csv for a specific user"""
with open(file=data_path, mode="rb") as data_file:
stream = data_file.read()
decrypted_data = fernet.decrypt(stream).decode().strip().split("\n")
reader = csv.reader(decrypted_data)
for row in reader:
if step == 1:
if row[0].lower() == user_id.lower():
return row
elif step == 2:
if row[1].lower() == user_id.lower():
return row
return []
@lru_cache(maxsize=16)
def calculate_gpa(user_nic: str, admin: bool = False) -> str:
"""Construct and return reply-message body with GPA values"""
if admin:
results = get_gpa(user_nic, 1)
else:
results = get_gpa(user_nic, 2)
message_body: str = ""
warnings: str = ""
if results == []:
return "Invalid NIC detected! - Sorry, You are not authorized to continue..."
if results[11] == "ERROR":
return (
"I can't validate your NIC because it's not registered in database.\n"
"Please mention/ping admin to update your NIC"
)
if results[11] == "HOLD":
warnings += "🔵 Your final results are on hold. Partially calculated GPA values are shown.\n\n"
elif results[11] == "NEW":
warnings += (
"🔵 Since calculated GPA values are based on your current results within this batch;"
"OGPA, CGPA and Academic Status will not represent accurate information.\n\n"
)
# Semester GPAs
count: int = 1
message_body += "<b><u>Semester GPA</u></b>\n\n"
for item in results[3:11]:
if item in ["", "\n"]:
count += 1
continue
sgpa: float = round(float(item), 2)
if sgpa < 1.50:
warnings += (
f"🔴 Semester {count} GPA: <b>{sgpa}</b> - <b>Academic Probation</b>\n"
)
elif sgpa < 2.00:
warnings += (
f"🔴 Semester {count} GPA: <b>{sgpa}</b> - <b>Academic Warning</b>\n"
)
message_body += f"🔹Semester {count} GPA: <b>{sgpa}</b>\n"
count += 1
message_body += "\n"
# Level GPAs
count = 1
message_body += "<b><u>Level GPA</u></b>\n\n"
for item in results[12:16]:
if item in ["", "\n"]:
count += 1
continue
message_body += f"🔹Level {count} GPA: <b>{round(float(item), 2)}</b>\n"
count += 1
message_body += "\n"
# Current GPA
cgpa: float = round(float(results[16]), 2)
message_body += f"🔹Your Cumulative GPA is <b>{cgpa}</b>\n\n"
# Academic Status
message_body += academic_status(cgpa)
# Add academic Warnings
if warnings != "":
message_body += "\n\n<b><u>Warnings</u></b>\n\n" + warnings
else:
message_body += (
"\n\n<b><u>Warnings</u></b>\n\nCool! 👍 No Academic Warnings for you"
)
# Add disclaimer info
message_body += "\n\n<i>🔹Please note that these data might not reflect the finalized GPA values in case of the usage of weighted average GPA.</i>\n\n"
return message_body
def get_leaderboard() -> str:
"""Construct and return reply-message body with leaderboard"""
with open(file=data_path, mode="rb") as data_file:
stream = data_file.read()
decrypted_data = fernet.decrypt(stream).decode().strip().split("\n")
reader = csv.reader(decrypted_data)
data: List[List[str]] = []
for row in reader:
data.append(row)
# Sort data by CGPA
# print(data)
# data[1:].sort(key=lambda x: float(x[16]), reverse=True)
sorted_data = sorted(data[1:], key=lambda x: float(x[16]), reverse=True)
# Construct message body
message_body: str = "<b><u>Leaderboard [Cumulative GPA]</u></b>\n\n"
count: int = 1
for row in sorted_data[:10]:
if row[16] == "":
continue
message_body += f"{count}. <b>{row[2]}</b> 🔸 {row[16]}\n\n"
count += 1
return message_body
def academic_status(cgpa: float) -> str:
"""Construct the academic status message based on cgpa value"""
status_msg: str = ""
greetings: List[str] = [
"Cheers!",
"Yay!",
"Cool!",
"Awesome!",
]
greeting: str = choice(greetings)
# spoiler: str = "<span class='tg-spoiler'>"
if cgpa >= 3.70:
status_msg += (
f"{greeting} 🎉✨ You currently have a <b>First Class</b> 🔥🔥\nKeep it up!"
)
elif cgpa >= 3.30:
status_msg += f"{greeting} 🎉✨ You currently have a <b>Second Class Upper</b> 🔥🔥\nKeep it up!"
elif cgpa >= 3.00:
status_msg += f"{greeting} 🎉✨ You currently have a <b>Second Class Lower</b> 🔥🔥\nKeep it up!"
elif cgpa >= 2.00:
status_msg += (
f"{greeting} 🎉✨ You have a "
f"<b>pass</b>... \nKeep it up! ✨ - You can achieve a class!"
)
else:
status_msg += "GPA is less than 2.0 😢 - or did I make any mistake?"
return status_msg