-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonFile
More file actions
116 lines (90 loc) · 3.44 KB
/
PythonFile
File metadata and controls
116 lines (90 loc) · 3.44 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
global data
# Read data from file and store in global variable data
with open('data.json') as f:
data = json.load(f)
@app.route('/')
def hello_world():
return 'Hello, World!' # return 'Hello World' in response
@app.route('/students')
def get_students():
result = []
pref = request.args.get('pref') # get the parameter from url
if pref:
for student in data: # iterate dataset
if student[
'pref'] == pref: # select only the students with a given meal preference
result.append(student) # add matching student to the result
return jsonify(result) # return filtered set if parameter is supplied
return jsonify(data) # return entire dataset if no parameter supplied
@app.route('/students/<id>')
def get_student(id):
for student in data:
if student[
'id'] == id: # filter out the students without the specified id
return jsonify(student)
@app.route('/stats', methods=['GET'])
def get_stats():
# Total preferences
pref_count = {"Vegetable": 0, "Chicken": 0, "Fish": 0}
# Counters sorted by program selections
programme_pref_count = {
"Chicken": {
"Computer Science (Special)": 0,
"Computer Science (Major)": 0,
"Information Technology (Special)": 0,
"Information Technology (Major)": 0
},
"Vegetable": {
"Computer Science (Special)": 0,
"Computer Science (Major)": 0,
"Information Technology (Special)": 0,
"Information Technology (Major)": 0
},
"Fish": {
"Computer Science (Special)": 0,
"Computer Science (Major)": 0,
"Information Technology (Special)": 0,
"Information Technology (Major)": 0
}
}
# Loop to count preferences and program selections
for record in data:
pref = record["pref"] # Get the preference (Chicken, Vegetable, Fish)
programme = record["programme"] # Get the selected program
# Count total preferences
if pref in pref_count:
pref_count[pref] += 1
# Count program selections based on each preference
if pref in programme_pref_count:
if programme in programme_pref_count[pref]:
programme_pref_count[pref][programme] += 1
# Return the counts for preferences and programs as JSON
return jsonify({
"preferences": pref_count, # Total for each meal preference
"programmes_by_preference":
programme_pref_count # Breakdown of programmes by preference
})
@app.route('/add/<int:a>/<int:b>', methods=['GET'])
def add(a, b):
result = a + b
return jsonify({"a": a, "b": b, "result": result}) #result was used over =
@app.route('/subtract/<int:a>/<int:b>', methods=['GET'])
def subtract(a, b):
result = a - b
return jsonify({"a": a, "b": b, "result": result})
@app.route('/multiply/<int:a>/<int:b>', methods=['GET'])
def multiply(a, b):
result = a * b
return jsonify({"a": a, "b": b, "result": result})
@app.route('/divide/<int:a>/<int:b>', methods=['GET'])
def divide(a, b):
if b == 0:
return jsonify(
{"ERROR": "Division by zero is not mathematically possible"})
result = a / b
return jsonify({"a": a, "b": b, "result": result})
# Run the app
app.run(host='0.0.0.0', port=8080)