-
Notifications
You must be signed in to change notification settings - Fork 0
/
employee.py
99 lines (77 loc) · 2.58 KB
/
employee.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
import bisect
import json
import re
from dataclasses import asdict, astuple, dataclass, fields
from util import dataclassToJson
@dataclass
class Employee:
name: str
title: str
email: str
phone: str
buildingId: int
room: str
departmentId: int
def __lt__(self, e) -> bool:
return self.email < e.email
# https://docs.python.org/3/library/bisect.html#searching-sorted-lists
def bisectIndex(ls: list, value):
if value is None:
return -1
index = bisect.bisect_left(ls, value)
if index != len(ls) and ls[index] == value:
return index
return -1
if __name__ == '__main__':
buildings = [b['code'] for b in json.load(open('building.json', 'r'))]
departments = [d['code'] for d in json.load(open('department.json', 'r'))]
titles: list = json.load(open('title.json', 'r'))
sections: list = json.load(open('_pawsSection.raw.json', 'r'))
employees: list = json.load(open('_employee.raw.json', 'r'))
emails = []
for employee in employees:
try:
buildingText: str = employee['building']
duplets = re.match(r'(.+) \((\d{3}\w{3})\)', buildingText).groups()
buildingCode = duplets[1]
except TypeError:
buildingCode = None
employee['buildingId'] = bisectIndex(buildings, buildingCode)
departmentCode = employee['departmentCode']
employee['departmentId'] = bisectIndex(departments, departmentCode)
emails.append(employee['email'])
emails.sort()
for section in sections:
if section['instructor'] is None:
continue
name: str = section['instructor'][0]
email: str = section['instructor'][1]
if name != '' and bisectIndex(emails, email) == -1:
employees.append({
'name': name,
'title': None,
'email': email,
'phone': None,
'buildingId': None,
'room': None,
'departmentId': None
})
bisect.insort(emails, email)
keys = [f.name for f in fields(Employee)]
employees = [
Employee(**{key: e[key] for key in keys})
for e in employees
]
dataclassToJson(Employee, employees, 'employee')
# employees.sort()
# values = [list(astuple(e)) for e in employees]
# json.dump(
# [asdict(e) for e in employees],
# open('employee.json', 'w'),
# indent=4
# )
# json.dump(
# {'keys': keys, 'values': values},
# open('employee.min.json', 'w'),
# separators=(',', ':')
# )