-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
85 lines (70 loc) · 2.15 KB
/
model.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
from pathlib import Path
from typing import Dict, List
from dataclasses import dataclass
import pickle
import csv
@dataclass (frozen=True)
class Criterion:
name: str
description: str
scores: tuple
@dataclass
class Applicant:
name: str
cv: str #path to cv
scores: Dict[Criterion,str]
@dataclass
class Role:
job_title: str
job_id: str
criteria: List[Criterion]
@dataclass
class Shortlist:
role: Role
applicants: List[Applicant]
#functions
pickle_file_name = "shortlist.pickle"
def load_pickle(file_path):
"""load shortlist from existing pickle file"""
with open(file_path, "rb") as f:
shortlist = pickle.load(f)
return shortlist
def save_shortlist(path,shortlist):
"""save shortlist as a pickle file inside the role_directory"""
with open(path/pickle_file_name, "wb") as f:
pickle.dump(shortlist, f)
def load_shortlist(path):
"""import shortlist data from either pickle file or role directory if the former doesn't exist"""
file = path/pickle_file_name
if file.exists():
shortlist = load_pickle(file)
else:
criteria = load_criteria(path/"criteria.csv")
role = load_role(path,criteria)
applicants = load_applicants(path)
shortlist = Shortlist(role,applicants)
return shortlist
def load_role(path,criteria):
"""generates role object instance"""
role = Role(path,"0001",criteria)
return role
def load_applicants(path):
"""generate a list of applicant instances from pdf format CVs"""
p = Path(path)
files = p.glob("*.pdf")
applicants = []
for file in files:
name_parts = file.stem.split("_")
applicant = Applicant(" ".join(name_parts[0:2]),file,{})
applicants.append(applicant)
return applicants
def load_criteria(csv_file):
"""generate criteria(list of criterion instances) from csv file"""
criteria = []
with open(csv_file) as file:
reader= csv.reader(file)
next(reader)
for row in reader:
criterion = Criterion(name=row[0],description=row[1],scores= tuple(row[2].split(",")))
criteria.append(criterion)
return criteria