-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (40 loc) · 1.4 KB
/
Copy pathmain.py
File metadata and controls
53 lines (40 loc) · 1.4 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
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
import csv, codecs
from io import StringIO
app = FastAPI()
@app.post("/files")
async def find_max_rate_product(data_file: UploadFile = File(...)):
file_name = data_file.filename
splitFlieNameAndExtension = file_name.split('.')
# check if filename hasn't an extension
# or the extenstion is not csv
if len(splitFlieNameAndExtension) <= 1 or splitFlieNameAndExtension[-1].lower() != "csv":
return {
"status" : "Faild",
"error_code" : 11,
'Error' : "Only .CSV file accepted"
}
csv_data = csv.reader(codecs.iterdecode(data_file.file,'utf-8'))
max_average_rate = -1
max_product_rate = ''
# to skip the header row
next(csv_data)
# loop rows in csv file
for id, product, rate in csv_data:
# convert rate form str to float to be comparable
rate = float(rate)
if rate > max_average_rate:
max_average_rate = rate
max_product_rate = product
if max_average_rate == -1 and max_product_rate == "":
return {
"status" : "Faild",
"error_code" : 12,
"Error" : "the uploaded file has no data"
}
return {
"status" : "Success",
"top_product": max_product_rate,
"product_rating": max_average_rate
}