Repository navigation
Expand file tree
/
Copy pathworker.py
More file actions
95 lines (80 loc) · 2.41 KB
/
Copy pathworker.py
File metadata and controls
95 lines (80 loc) · 2.41 KB
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
from xmlrpc.server import SimpleXMLRPCServer
import sys
import json
# Storage of data
data_table = {}
def ping():
return {
'error': False,
'result': True
}
def load_data(group):
# load data based which portion it handles (am or nz)
global data_table
if group == 'am':
with open('data-am.json') as f1, open('data-nz.json') as f2:
#open the two JSON files, and the | operator is used to merge the dictionaries loaded from each file.
data_table = json.load(f1) | json.load(f2)
elif group == "nz":
with open('data-nz.json') as f1, open('data-am.json') as f2:
data_table = json.load(f1) | json.load(f2)
def getbyname(name):
# Look up person by name
try:
result = data_table[name]
return {
'error': False,
'result': [result]
}
except KeyError:
return {
'error': False,
'result': []
}
def getbylocation(location):
# Filter data to include only records with specified location
if not data_table:
return {
'error': True,
'result': 'No data loaded'
}
results = []
for record in data_table.values():
if record['location'] == location:
results.append(record)
return {
'error': False,
'result': results
}
def getbyyear(location, year):
# filter data to include only records with specified location & year
if not data_table:
return {
'error': True,
'result': 'No data loaded'
}
results = []
for record in data_table.values():
if record['location'] == location and record['year'] == year:
results.append(record)
return {
'error': False,
'result': results
}
def main():
if len(sys.argv) < 3:
print('Usage: worker.py <port> <group: am or nz>')
sys.exit(0)
port = int(sys.argv[1])
group = sys.argv[2]
server = SimpleXMLRPCServer(("localhost", port))
print(f"Listening on port {port}...")
load_data(group) # load the data based on the group parameter
# register RPC functions
server.register_function(getbyname)
server.register_function(getbylocation)
server.register_function(getbyyear)
server.register_function(ping)
server.serve_forever()
if __name__ == '__main__':
main()