-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-creator.py
94 lines (82 loc) · 2.84 KB
/
json-creator.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
import json
def addCareer(career, group, path):
with open(path, "a") as fd:
fd.write(f"\t\"{career}\": \"{group}\",\n")
def addPerformance(career, list, path):
with open(path, "a") as fd:
pers = ""
for v in list:
pers += ", " + v.replace(",", ".")
pers = pers[2:]
fd.write(f"\t\"{career}\": [{pers}],\n")
def addGraduate(group, list, path):
with open(path, "a") as fd:
l = [ int(x.replace(".", "")) for x in list]
fd.write(f"\t\"{group}\": {l},\n")
def parseMatriculas(path):
groups = {}
careers = {}
data = []
with open(path + "grupos.json", "r") as fd:
groups = json.load(fd)
with open(path + "carreras.json", "r") as fd:
careers = json.load(fd)
for k, _ in careers.items():
careers[k] = [0, 0]
with open(path + "matri", "r") as fd:
data = fd.readlines()
for l in data:
i = parseLine(l)
try:
key = i[0]
groups[key][0] += i[1]
groups[key][1] += i[2]
except KeyError:
try:
careers[key][0] += i[1]
careers[key][1] += i[2]
except KeyError:
print(l, "ERROR")
continue
with open(path + "grupos.json", "w") as fd:
json.dump(groups, fd, ensure_ascii=False)
with open(path + "matriculasCarreras.json", "w") as fd:
json.dump(careers, fd, ensure_ascii=False)
acum = 0
for k,v in groups.items():
acum += v[0]
print(acum)
acum = 0
for k,v in careers.items():
acum += v[0]
print(acum)
def parseLine(line):
aux = line.split(" ")
aux[-1] = aux[-1].replace("\n", "")
career = ""
for s in aux[:-2]:
if s == " ":
continue
career += s + " "
i = -1
while career[i] == " ":
i -= 1
career = career[:i+1]
return [career, int(aux[-2]), int(aux[-1])]
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='json-creator')
parser.add_argument('-c', '--career', type=str, default='', help='career')
parser.add_argument('-g', '--group', type=str, default='', help='group of career')
parser.add_argument('-p', '--path', type=str, default='./resources/data/', help='path of json')
parser.add_argument('-m', '--method', type=str, default='parseMatriculas', help='method to call')
parser.add_argument('-l', '--list', nargs='+', required=False)
args = parser.parse_args()
if args.method == "addCareer":
addCareer(args.career, args.group, args.path)
elif args.method == "addPerformance":
addPerformance(args.career, args.list, args.path)
elif args.method == "addGraduate":
addGraduate(args.group, args.list, args.path)
elif args.method == "parseMatriculas":
parseMatriculas(args.path)