-
Notifications
You must be signed in to change notification settings - Fork 427
/
filter.py
80 lines (57 loc) · 1.68 KB
/
filter.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
import json
import os
import string
import sys
def _country_filter(src, scope, out):
"""
Search the data for a single country
:arg src: source dictionary
:arg scope: source selector
"""
def filter(entry, item):
matching = entry["country"]
if item == matching or item == matching.lower() or item == matching.upper():
return True
else:
return False
return [entry for entry in src if filter(entry, scope)]
def country_filter(src, scopes):
"""
Either make multiple data searches or
execute one. {NEEDS IMPROVEMENT, O(kN) => O(n)}
:arg src: source dictionary
:arg scopes: source selectors
"""
out = []
if type(scopes) is list:
[out.extend(_country_filter(src, scope, out)) for scope in scopes]
else:
out = _country_filter(src, scopes, out)
return out
def main():
args = []
temp_arg = ""
first_word = True
# Retrieve our selecting countries (seperated by commas)
for arg in sys.argv[1:]:
temp_arg += arg if first_word else " " + arg
first_word = False
if arg[-1] == ",":
args.append(temp_arg[:-1])
temp_arg = ""
first_word = True
if temp_arg:
args.append(temp_arg)
if not args:
return
# Load the source
src = None
with open("./world_universities_and_domains.json") as src_file:
src = json.load(src_file)
if src is None:
return
# Write the filtered result
with open("./filtered_world_universities_and_domains.json", "w") as dest_file:
json.dump(country_filter(src, args), dest_file)
if __name__ == "__main__":
main()