-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv_formatting.py
104 lines (60 loc) · 2.13 KB
/
cv_formatting.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
#!/usr/bin/python3
def load_fields(file_name):
fil = open(file_name,'r')
data = fil.read().replace('\n','')
fil.close()
d = [elm.lstrip('[') for elm in data.split(']') if elm!='']
out = {}
for entry in d:
e = [[t.strip().lower() for t in elm.split("=")] for elm in entry.split(";") if elm != '']
tmp_dict = {}
for key,value in e:
if key == "keyword":
if value in out:
raise ValueError
keyword = value
else:
tmp_dict[key] = value
out[keyword] = tmp_dict
for key in out:
out[key]['required'] = [elm for elm in out[key]['required'].split(',') if elm != '']
try:
out[key]['optional'] = [elm for elm in out[key]['optional'].split(',') if elm != '']
except KeyError:
pass
return out
def create_entry(fields,keyword,data):
omit = []
for key in fields[keyword]['optional']:
if key not in data:
omit.append(key)
field_data = fields[keyword]['format'].split("'")
optional_locations = []
for elm in [[t.strip("}") for t in elm.split("{")] for elm in field_data[1::2]]:
for t in elm:
if t in fields[keyword]['optional']:
optional_locations.append(t)
continue
omit_indices = [2*optional_locations.index(elm)+1 for elm in omit]
print("".join([elm for elm in field_data if field_data.index(elm) not in omit_indices]).format(**data))
if __name__ == "__main__":
file_name = "cv_formatting.txt"
fields = load_fields(file_name)
test_data = {'university':'Texas A\&M University','degree':'Ph.D','state':'TX','city':'College Station',
'begin':'September 2009','end':'May 2015'}
create_entry(fields,'education',test_data)
"""
omit = []
for key in fields['education']['optional']:
if key not in test_data:
omit.append(key)
field_data = fields['education']['format'].split("'")
optional_locations = []
for elm in [[t.strip("}") for t in elm.split("{")] for elm in field_data[1::2]]:
for t in elm:
if t in fields['education']['optional']:
optional_locations.append(t)
continue
omit_indices = [2*optional_locations.index(elm)+1 for elm in omit]
print("".join([elm for elm in field_data if field_data.index(elm) not in omit_indices]).format(**test_data))
"""