-
Notifications
You must be signed in to change notification settings - Fork 0
/
commodity.py
125 lines (101 loc) · 4.75 KB
/
commodity.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
125
#==========================================================================================================================================
# Commodity Class
#==========================================================================================================================================
class Commodity:
"""
Class: Commodity
Description:
Represents a commodity class.
Instance Variables:
int ID:
Unique ID of the commodity class.
list<TargetProductOffer> tpos:
List of TPO IDs that fall within the commodity class.
"""
#--------------------------------------------------------------------------------------------------------------------------------------
# Constructor
#--------------------------------------------------------------------------------------------------------------------------------------
def __init__(self, ID: int):
"""
Constructor:
Outlet
(
int ID
)
Description:
Constructs an Commodity object.
Arguments:
int ID:
Unique identifier of the commodity class.
"""
self.ID = int(ID)
self.tpoIDs = set()
#--------------------------------------------------------------------------------------------------------------------------------------
# containsUnassignedTPOs Method
#--------------------------------------------------------------------------------------------------------------------------------------
def containsUnassignedTPOs(self, tpos: dict, periodID: int) -> bool:
"""
Method:
bool containsUnassignedTPOs
(
dict<int, TargetProductOffer> tpos,
int periodID
)
Description:
Returns True if any unassigned TPOs are associated with the commodity class.
Arguments:
dict<int, TargetProductOffer>:
Map of all TargetProductOffer objects.
Key: TPO ID
Value: TargetProductOffer object
int periodID:
Unique identifier of a reference period.
Output:
True if it contains unassigned TPOs
False otherwise
"""
for tpoID in self.tpoIDs:
if not tpos[tpoID].isAssigned(periodID):
return True
return False
#--------------------------------------------------------------------------------------------------------------------------------------
# getAssignedProductIDs Method
#--------------------------------------------------------------------------------------------------------------------------------------
def getAssignedProductIDs(self, tpos: dict, periodID: int, outletID: int = None) -> set:
"""
Method:
set<int> getAssignedProductIDs
(
dict<int, TargetProductOffer> tpos,
int periodID,
int outletID
)
Description:
Returns the set of IDs belonging to products within this commodity class that were assigned during the given period.
Arguments:
dict<int, TargetProductOffer>:
Map of all TargetProductOffer objects.
Key: TPO ID
Value: TargetProductOffer object
int periodID:
Unique identifier of a reference period.
int outletID: If not None, the search is limited to the given outlet.
Output:
set<int>
"""
assignedProductIDs = list()
# Iterate over all TPOs associated with this commodity class.
if outletID == None:
for tpoID in self.tpoIDs:
tpo = tpos[tpoID]
tpoProp = tpo.properties[periodID]
if tpo.isAssigned(periodID) and tpoProp.productID not in assignedProductIDs:
assignedProductIDs.append(tpoProp.productID)
# Limit the search to TPOs at the given outlet.
else:
for tpoID in self.tpoIDs:
tpo = tpos[tpoID]
tpoProp = tpo.properties[periodID]
if tpo.isAssigned(periodID) and tpo.outletID == outletID and tpoProp.productID not in assignedProductIDs:
assignedProductIDs.append(tpoProp.productID)
return set(assignedProductIDs)