-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverseasStudents.py
46 lines (38 loc) · 1.25 KB
/
overseasStudents.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
#!/usr/bin/python3
# Track proportion of overseas students
import sys
import psycopg2
import re
#helper functions
def get_student_counts(cursor, TermCode):
query = """
SELECT
SUM(CASE WHEN distinct_students_of_term_code.status = 'INTL' THEN 1 ELSE 0 END) AS Internationals,
SUM(CASE WHEN distinct_students_of_term_code.status != 'INTL' THEN 1 ELSE 0 END) AS Locals
FROM
(SELECT DISTINCT s.id, s.status
FROM Program_enrolments pe
JOIN Students s ON pe.student = s.id
JOIN Terms t ON pe.term = t.id
WHERE t.code = %s) AS distinct_students_of_term_code
"""
cursor.execute(query, (TermCode,))
result = cursor.fetchone()
return result
# global vairables
db = None
try:
db = psycopg2.connect("dbname=ass2")
cursor = db.cursor()
# the header
print("Term #Locl #Intl Proportion")
terms = [f"{year}T{term}" for year in range(19, 24) for term in range(4) if not (year == 19 and term == 0)]
for TermCode in terms:
Internationals, Locals = get_student_counts(cursor, TermCode)
Proportion = Locals / Internationals
print(f"{TermCode} {Locals:6d} {Internationals:6d} {Proportion:6.1f}")
except Exception as err:
print(err)
finally:
if db:
db.close()