-
Notifications
You must be signed in to change notification settings - Fork 0
/
geography.py
201 lines (156 loc) · 7.56 KB
/
geography.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import targetproductoffer as tpro
#==========================================================================================================================================
# Outlet Class
#==========================================================================================================================================
class Outlet:
"""
Class: Outlet
Description:
Represents an outlet.
Instance Variables:
int ID: Unique identifier of an outlet in Phoenix.
dict<int, int> siteIDs:
Corresponding site IDs over the course of given time frame.
Key: Period ID
Value: Site ID
"""
#--------------------------------------------------------------------------------------------------------------------------------------
# Constructor
#--------------------------------------------------------------------------------------------------------------------------------------
def __init__(self, ID):
"""
Constructor:
Outlet
(
int ID
)
Description:
Constructs an Outlet object.
Arguments:
int ID:
Unique identifier of the outlet.
"""
self.ID = ID
self.siteIDs = {}
#--------------------------------------------------------------------------------------------------------------------------------------
# addSiteID Method
#--------------------------------------------------------------------------------------------------------------------------------------
def addSiteID(self, periodID: int, siteID: int):
"""
Method:
void addSiteID
(
int periodID,
int siteID
)
Description:
Links the given site ID to the ID of this outlet for the given period ID.
Arguments:
int periodID:
Unique identifier of a reference period.
int siteID:
Unique retailer identifier of an outlet.
"""
self.siteIDs[periodID] = int(siteID)
#--------------------------------------------------------------------------------------------------------------------------------------
# toString Method
#--------------------------------------------------------------------------------------------------------------------------------------
def toString(self, escChars: str = "\n") -> str:
response = escChars + "------------------------------"
response += escChars + "Data type: Outlet"
response += escChars + "ID: " + str(self.ID)
response += escChars + "Site IDs that correspond to this outlet:"
for periodID, siteID in self.siteIDs.items():
response += escChars + str(periodID) + ". " + str(siteID)
response += escChars + "------------------------------"
return response
#==========================================================================================================================================
# Geography Class
#==========================================================================================================================================
class Geography:
"""
Class: Geography
Description:
Represents a geographic class.
Instance Variables:
list<T> properties
dict<int, Outlet>:
Map of Outlet objects belonging to this geographic class.
Key: Outlet ID
Value: Outlet object
"""
#--------------------------------------------------------------------------------------------------------------------------------------
# Constructor
#--------------------------------------------------------------------------------------------------------------------------------------
def __init__(self, properties: list):
"""
Constructor:
Geography
(
list<T> properties
)
Description:
Constructs a Geography object.
Arguments:
list<T> properties:
List of properties belonging to the geographic class. Can be of any type.
"""
self.properties = list(properties)
self.outlets = {}
#--------------------------------------------------------------------------------------------------------------------------------------
# addSiteID Method
#--------------------------------------------------------------------------------------------------------------------------------------
def addOutlet(self, outletIDs):
"""
Method:
void addOutlet
(
list<int> outletIDs
)
Description:
Creates Outlet objects for each identifier given in outletIDs.
Arguments:
list<int> outletIDs: List of identifiers of the Outlet objects to be created.
"""
for outletID in outletIDs:
if outletID not in self.outlets.keys():
self.outlets[outletID] = Outlet(outletID)
#--------------------------------------------------------------------------------------------------------------------------------------
# equals Method
#--------------------------------------------------------------------------------------------------------------------------------------
def equals(self, props: list) -> bool:
"""
Method:
bool equals
(
list<T> props
)
Description:
Checks whether the given property array is equal to the properties of the Geography object.
Arguments:
list<T> props: List of properties.
"""
# Return False if the property vectors are of unequal lengths
if len(props) < len(self.properties):
return False
# Return False if any of the properties are not equal to their counterparts
for i in range(0, len(self.properties)):
if self.properties[i] != props[i]:
return False
# Return True otherwise.
return True
#--------------------------------------------------------------------------------------------------------------------------------------
# toString Method
#--------------------------------------------------------------------------------------------------------------------------------------
def toString(self, escChars: str = "\n") -> str:
response = escChars + "------------------------------"
response += escChars + "Data type: Geography"
for geoProperty in self.properties:
response += escChars + str(geoProperty)
response += escChars + "Outlets that belong to this geography:"
for i, ID in enumerate(self.outlets):
response += escChars + str(i + 1) + ".\n" + str(self.outlets[ID].toString(escChars + "\t"))
if i == 9:
break
response += escChars + "------------------------------"
return response