-
Notifications
You must be signed in to change notification settings - Fork 0
/
averageranker.py
144 lines (130 loc) · 4.02 KB
/
averageranker.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/user/bin/python
# gets ranks-sorted lists and creates a new list where the ranks are
# are average
#
import os
import sys
from statistics import mean
n = '\n'
def get_lists(file):
file_path = os.path.abspath(file)
f = open(file_path, 'r')
text = f.read()
alpha_list = text.split('\n')
list = []
for thing in alpha_list:
begotten_list = thing.split(';')
if len(begotten_list) < 2:
continue
else:
list.append(begotten_list)
return list
def get_university_super(i,list,range_option):
university_details_list = list[i]
if range_option == 'name':
university_details_result = university_details_list[1]
if range_option == 'details':
university_details_result = university_details_list[1:]
if range_option == 'rank':
university_details_result = university_details_list[0]
return university_details_result
def index_maker(list):
indeces = []
for thing in list:
indeces.append(thing[0])
print(' These universities are already in \'loa\':',indeces,n)
return indeces
def insert_rank(loa,rank,university_name):
thing_counter = -1
for thing in loa:
thing_counter += 1
if university_name in thing:
break
print('thing counter:',thing_counter,n)
if len(loa) > 1:
microlist = loa[thing_counter]
else:
microlist = []
microlist.append(rank)
loa.pop(thing_counter)
loa.insert(thing_counter,microlist)
def save_file(files):
file = files[0]
file_dir = os.path.dirname(file)
file_base = os.path.basename(file)
file_name = file_dir+'avgranker-tmp-'+file_base
g = open(file_name,'w')
g.write('Backup file for:'+n)
g.write(files)
g.write(n+'Begin loa:'+n)
for university in loa:
g.write(university.join(';')+n)
g.close
#########################
### add files to first line of save file so check_backup()
### can see what files were used for that run.
def check_backup(files):
filenames = []
for file in files:
extracted_files = os.listdir(os.path.dirname(file))
for i in extract_files:
if i not in filenames:
filesnames.append(i)
for filename in filenames:
backup_file = re.search('avgranker-tmp-',filename)
if backup_file:
backup_file_name = backup_file.group()
f = open(backup_file_name, 'r')
backup_text = f.read()
files_check = re.findall('Backup file for:\n(.+\n)?(Begin loa:){1}
files_check_list = files_check.group()
# check file to see if files is same as files in file.
def main():
files = sys.argv[1:]
check_backup(files)
lol = []
for file in files:
lol.append(get_lists(file))
print('lol:',n,lol,n)
loa = []
j = len(lol)
k = 1
for list in lol:
print(' Printing rank list',k,'of',j,n,list,n)
l = len(list)
m = 1
i = 0
for list_of_university_details in list:
print(' Digesting university list',m,'of',l)
print(' This is the raw data:',n,' ',list_of_university_details,n)
microlist = []
university_name = get_university_super(i,list,'name')
if university_name not in index_maker(loa):
print(' Inserting',university_name,'for the first time in \'loa\'',n)
for thing in get_university_super(i,list,'details'):
microlist.append(thing)
microlist.append(get_university_super(i,list,'rank'))
i += 1
loa.append(microlist)
else:
print(' ',university_name,'is already in \'loa\'; appending new rank.',n)
insert_rank(loa,microlist,university_name)
i += 1
print(' Microlist ready for appending to \'loa\'. This is what will be appended:',2*n,microlist,n)
m += 1
k += 1
print('Saving work thus far.',n)
save_file(files)
for university in loa:
average = mean(university[4:])
del university[4:]
university.append(average)
proto_file_name = str(os.path.abspath(files[0]))
file_name = proto_file_name[:proto_file_name.find('.txt')]
save_name = file_name+'-avgrank.txt'
f = open(save_name, 'w')
for item in loa:
f.write(';'.join(item)+'\n')
f.close
if __name__ == '__main__':
main()