-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvhandler.py
124 lines (100 loc) · 4.55 KB
/
csvhandler.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
from model.SupplySite import SupplySite
from model.Depot import Depot
import csv
import constants
import pandas as pd
def readLocalizations():
localizations = {}
with open(constants.LOCATIONS_PATH, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader, None) # skip header
for row in reader:
instalationCode, latitude, longitude = row[0], row[1], row[2]
localizations[instalationCode] = (latitude, longitude)
return localizations
def readDistances():
distances = {}
with open(constants.DISTANCE_RESULT_PATH, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader, None) # skip header
for row in reader:
originCode, destinyCode, distance = row[0], row[3], row[6]
key = originCode + ":" + destinyCode
distances[key] = distance
return distances
def readDataset():
supplySites = {}
depots = {}
localizations = readLocalizations()
with open(constants.DATASET_PATH, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader, None) # skip header
for row in reader:
supplySiteCode = row[0]
SKU = row[1]
locationCode = row[2]
averageDemand = row[3]
locationType = row[4]
minDOC = row[5]
reorderPoint = row[6]
maxDOC = row[7]
closingStock = row[8]
availableToDeploy = row[9]
distributorOrders = row[10]
# parse quantitative vars to float
averageDemand = float(averageDemand.replace(',','.'))
minDOC = float(minDOC.replace(',','.'))
reorderPoint = float(reorderPoint.replace(',','.'))
maxDOC = float(maxDOC.replace(',','.'))
closingStock = float(closingStock.replace(',','.'))
availableToDeploy = float(availableToDeploy.replace(',','.'))
distributorOrders = float(distributorOrders.replace(',','.'))
if supplySiteCode in supplySites:
supplySite = supplySites[supplySiteCode]
else:
latitude = localizations[supplySiteCode][0]
longitude = localizations[supplySiteCode][1]
supplySite = SupplySite(supplySiteCode, latitude, longitude)
if locationCode in depots:
depot = depots[locationCode]
else:
latitude = localizations[locationCode][0]
longitude = localizations[locationCode][1]
depot = Depot(locationCode,locationType, latitude, longitude)
supplySite.setSKUsToDeploy(SKU, availableToDeploy)
supplySite.addRecipient(SKU, depot.code)
depot.setSKUCapacity(SKU, minDOC, reorderPoint, maxDOC)
depot.setSKUClosingStock(SKU, closingStock)
depot.setDistributorOrder(SKU,distributorOrders)
depot.setAverageDemand(SKU, averageDemand)
# Update or add a supplySite/Depot on data structure
supplySites[supplySiteCode] = supplySite
depots[locationCode] = depot
return (supplySites, depots)
def saveResult(name, supplySites, depots):
csvFileName = constants.RESULTS_DIR + "/" +name + ".csv"
f = open(csvFileName, 'w')
writer = csv.writer(f)
header = ["supplySiteCode", "SKU" , "locationCode", "averageDemand", "locationType", "minDOC", "reorderPoint", "maxDOC", "old closingStock", "new closingStock", "availableToDeploy", "distributorOrders"]
writer.writerow(header)
with open(constants.DATASET_PATH, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader, None) # skip header
for row in reader:
supplySiteCode = row[0]
SKU = row[1]
locationCode = row[2]
averageDemand = row[3]
locationType = row[4]
minDOC = row[5]
reorderPoint = row[6]
maxDOC = row[7]
closingStock = row[8]
availableToDeploy = supplySites[supplySiteCode].availableToDeploy[SKU]
distributorOrders = row[10]
newClosingStock = depots[locationCode].closingStock[SKU]
writer.writerow([supplySiteCode, SKU , locationCode,
averageDemand, locationType, minDOC, reorderPoint, maxDOC,
closingStock, newClosingStock, availableToDeploy, distributorOrders])
xlsxFileName = constants.RESULTS_DIR + "/" +name + ".xlsx"
pd.read_csv(csvFileName).to_excel(xlsxFileName)