-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.py
237 lines (169 loc) · 7.47 KB
/
Rules.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from collections import namedtuple
from typing import *
def IsTypeQualified(order: dict)-> bool:
"""
IsTypeQualified function checks whether the order belongs to a specific list of types.
:param: order: a dictionary represents an order with some attributes
:return: a boolean value based on the given product type
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>IsTypeQualified(order)
True
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
return order["type"] in ["dress", "skirt"]
def GetTypeDiscount(order: dict)->float:
"""
GetTypeDiscount function calculates discount for the given order.
:param: order: a dictionary represents an order with some attributes
:return: discount value
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>GetTypeDiscount(order)
200.0
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
#Discount value is 10% of one quantity price
discount = order["price"] * 10 / 100
return discount
def IsPriceQualified(order: dict)-> bool:
"""
IsPriceQualified function checks whether the order price is higher than a specific value.
:param: order: a dictionary represents an order with some attributes
:return: a boolean value based on the given product price
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>IsPriceQualified(order)
True
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
return order["price"] >= 1600
def GetPriceDiscount(order: dict)->float:
"""
GetPriceDiscount function calculates discount for the given order.
:param: order: a dictionary represents an order with some attributes
:return: discount value
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>GetPriceDiscount(order)
500.0
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
#Discount value is 25% of one quantity price
discount = order["price"] * 25 / 100
return discount
def IsBrandQualified(order: dict)-> bool:
"""
IsBrandQualified function checks whether the order belongs to a specific list of brands.
:param: order: a dictionary represents an order with some attributes
:return: a boolean value based on the given product brand
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>IsBrandQualified(order)
True
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
return order["brand"] in ["mango", "lipsy", "zara"]
def GetBrandDiscount(order: dict)->float:
"""
GetBrandDiscount function calculates discount for the given order.
:param: order: a dictionary represents an order with some attributes
:return: discount value
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>GetBrandDiscount(order)
300.0
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
#Discount value is 15% of one quantity price
discount = order["price"] * 15 / 100
return discount
def IsSizeQualified(order: dict)-> bool:
"""
IsSizeQualified function checks whether the order belongs to a specific list of sizes.
:param: order: a dictionary represents an order with some attributes
:return: a boolean value based on the given product size
- Example:
>> order = {"type": "dress", "size": "L", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>IsSizeQualified(order)
False
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
return order["size"] in ["xs", "2xl"]
def GetSizeDiscount(order: dict)->float:
"""
GetSizeDiscount function calculates discount for the given order.
:param: order: a dictionary represents an order with some attributes
:return: discount value
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>GetSizeDiscount(order)
700.0
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
#Discount value is 35% of one quantity price
discount = order["price"] * 35 / 100
return discount
def IsQuantityQualified(order: dict)-> bool:
"""
IsQuantityQualified function checks whether the product quantity in the given order is greater than a specific value
:param: order: a dictionary represents an order with some attributes
:return: a boolean value based on product quantity in the given order
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 1, "discount": 0}
>>IsQuantityQualified(order)
False
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
return order["quantity"] > 1
def GetQuantityDiscount(order: dict)->float:
"""
GetTypeDiscount function calculates discount for the given order.
:param: order: a dictionary represents an order with some attributes
:return: discount value
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>GetTypeDiscount(order)
360.0
"""
#Input types assertion:
assert (isinstance(order, dict)), "order must be a dictionary."
#Discount value is 18% of one quantity price
discount = order["price"] * 18 / 100
return discount
def get_rules()->List[namedtuple]:
"""
get_rules function returns a list of discount qualifying conditions and discount rules pairs.
:return: a list of named tuples, each of which holds a qualifying condition function and the corresponding discount
calculation function
- Example:
>> order = {"type": "dress", "size": "xs", "brand": "mango","price": 2000.0, "quantity": 2, "discount": 0}
>>get_rules()
[
DiscountPair(IsTypeQualified, GetTypeDiscount),
DiscountPair(IsPriceQualified, GetPriceDiscount),
DiscountPair(IsBrandQualified, GetBrandDiscount),
DiscountPair(IsSizeQualified, GetSizeDiscount),
DiscountPair(IsQuantityQualified, GetQuantityDiscount)
]
"""
#Creating a namedtuple that has a name "DiscountPair", a first item with name "QualfyingCondition",
#and a second item with name "DiscountRule":
DiscountPair = namedtuple("DiscountPair", "QualifyingCondition DiscountRule")
conditions_rules_pairs = [
DiscountPair(IsTypeQualified, GetTypeDiscount),
DiscountPair(IsPriceQualified, GetPriceDiscount),
DiscountPair(IsBrandQualified, GetBrandDiscount),
DiscountPair(IsSizeQualified, GetSizeDiscount),
DiscountPair(IsQuantityQualified, GetQuantityDiscount)
]
return conditions_rules_pairs