-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappliance.py
54 lines (49 loc) · 1.91 KB
/
appliance.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
class Appliance:
def __init__(self, name):
"""
Initialize the Appliance with a string name, which will be used to import the relevant data from a json file
"""
from json import loads
f = open(str(name)+".json", 'r')
self.database = loads(f.readline())
self.parameterValues = self.database['parameterValues']
self.database['parameterValues'] = {}
f.close()
def getModel(self, modelNumber):
"""
Return a dictionary containing all the information for the given model.
"""
return self.database[modelNumber]
def filterBy(self, parameters):
"""
return a dictionary of appliances, filtered by the parameters of the form {param:value}
"""
result = {}
for appliance in self.database:
flag = False
for param,value in parameters.items():
if self.database[appliance][param] != value:
flag = True
break
if not flag:
result[appliance] = self.database[appliance]
return result
def getParameters(self):
"""
Return a dictionary of the form {'parameter' : [val1, val2, ...]}
"""
return self.parameterValues
def upgrade(self, modelNumber, filters={}, k=10):
"""
Return a list of the k best substitutes for the appliance filtered to contain only appliances matching the given filters
filters is a list of parameters
"""
currentModel = self.getModel(modelNumber)
filterDict = {}
for param in filters:
filterDict[param] = currentModel[param]
filteredApps = self.filterBy(filterDict)
filteredList = []
for app in filteredApps:
filteredList.append((int(filteredApps[app]['energy_consumption']), filteredList[app))
return filteredList.sorted()[:k]