Skip to content

Commit e016cd8

Browse files
added ge le
1 parent 4a5e75d commit e016cd8

File tree

2 files changed

+446
-6
lines changed

2 files changed

+446
-6
lines changed

optimizely/helpers/condition.py

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class ConditionMatchTypes(object):
3131
EXACT = 'exact'
3232
EXISTS = 'exists'
3333
GREATER_THAN = 'gt'
34+
GREATER_THAN_OR_EQUAL = 'ge'
3435
LESS_THAN = 'lt'
36+
LESS_THAN_OR_EQUAL = 'le'
3537
SEMVER_EQ = 'semver_eq'
3638
SEMVER_GE = 'semver_ge'
3739
SEMVER_GT = 'semver_gt'
@@ -177,6 +179,40 @@ def greater_than_evaluator(self, index):
177179

178180
return user_value > condition_value
179181

182+
def greater_than_or_equal_evaluator(self, index):
183+
""" Evaluate the given greater than or equal to match condition for the user attributes.
184+
185+
Args:
186+
index: Index of the condition to be evaluated.
187+
188+
Returns:
189+
Boolean:
190+
- True if the user attribute value is greater than or equal to the condition value.
191+
- False if the user attribute value is less than the condition value.
192+
None: if the condition value isn't finite or the user attribute value isn't finite.
193+
"""
194+
condition_name = self.condition_data[index][0]
195+
condition_value = self.condition_data[index][1]
196+
user_value = self.attributes.get(condition_name)
197+
198+
if not validator.is_finite_number(condition_value):
199+
self.logger.warning(audience_logs.UNKNOWN_CONDITION_VALUE.format(self._get_condition_json(index)))
200+
return None
201+
202+
if not self.is_value_a_number(user_value):
203+
self.logger.warning(
204+
audience_logs.UNEXPECTED_TYPE.format(self._get_condition_json(index), type(user_value), condition_name)
205+
)
206+
return None
207+
208+
if not validator.is_finite_number(user_value):
209+
self.logger.warning(
210+
audience_logs.INFINITE_ATTRIBUTE_VALUE.format(self._get_condition_json(index), condition_name)
211+
)
212+
return None
213+
214+
return user_value >= condition_value
215+
180216
def less_than_evaluator(self, index):
181217
""" Evaluate the given less than match condition for the user attributes.
182218
@@ -211,6 +247,40 @@ def less_than_evaluator(self, index):
211247

212248
return user_value < condition_value
213249

250+
def less_than_or_equal_evaluator(self, index):
251+
""" Evaluate the given less than or equal to match condition for the user attributes.
252+
253+
Args:
254+
index: Index of the condition to be evaluated.
255+
256+
Returns:
257+
Boolean:
258+
- True if the user attribute value is less than or equal to the condition value.
259+
- False if the user attribute value is greater than the condition value.
260+
None: if the condition value isn't finite or the user attribute value isn't finite.
261+
"""
262+
condition_name = self.condition_data[index][0]
263+
condition_value = self.condition_data[index][1]
264+
user_value = self.attributes.get(condition_name)
265+
266+
if not validator.is_finite_number(condition_value):
267+
self.logger.warning(audience_logs.UNKNOWN_CONDITION_VALUE.format(self._get_condition_json(index)))
268+
return None
269+
270+
if not self.is_value_a_number(user_value):
271+
self.logger.warning(
272+
audience_logs.UNEXPECTED_TYPE.format(self._get_condition_json(index), type(user_value), condition_name)
273+
)
274+
return None
275+
276+
if not validator.is_finite_number(user_value):
277+
self.logger.warning(
278+
audience_logs.INFINITE_ATTRIBUTE_VALUE.format(self._get_condition_json(index), condition_name)
279+
)
280+
return None
281+
282+
return user_value <= condition_value
283+
214284
def substring_evaluator(self, index):
215285
""" Evaluate the given substring match condition for the given user attributes.
216286
@@ -331,7 +401,8 @@ def split_semantic_version(self, target):
331401
target_parts = []
332402

333403
if self.has_white_space(target):
334-
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
404+
self.logger.warning(Errors.INVALID_ATTRIBUTE_FORMAT)
405+
return None
335406

336407
if self.is_pre_release(target):
337408
target_parts = target.split(SemverType.IS_PRE_RELEASE)
@@ -340,20 +411,24 @@ def split_semantic_version(self, target):
340411

341412
if target_parts:
342413
if len(target_parts) < 1:
343-
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
414+
self.logger.warning(Errors.INVALID_ATTRIBUTE_FORMAT)
415+
return None
344416
target_prefix = str(target_parts[0])
345417
target_suffix = target_parts[1:]
346418

347419
dot_count = target_prefix.count(".")
348420
if dot_count > 2:
349-
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
421+
self.logger.warning(Errors.INVALID_ATTRIBUTE_FORMAT)
422+
return None
350423

351424
target_version_parts = target_prefix.split(".")
352425
if len(target_version_parts) != dot_count + 1:
353-
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
426+
self.logger.warning(Errors.INVALID_ATTRIBUTE_FORMAT)
427+
return None
354428
for part in target_version_parts:
355429
if not part.isdigit():
356-
raise Exception(Errors.INVALID_ATTRIBUTE_FORMAT)
430+
self.logger.warning(Errors.INVALID_ATTRIBUTE_FORMAT)
431+
return None
357432

358433
if target_suffix:
359434
target_version_parts.extend(target_suffix)
@@ -453,7 +528,9 @@ def compare_user_version_with_target_version(self, index):
453528
ConditionMatchTypes.SEMVER_GT: semver_greater_than_evaluator,
454529
ConditionMatchTypes.SEMVER_LE: semver_less_than_or_equal_evaluator,
455530
ConditionMatchTypes.SEMVER_LT: semver_less_than_evaluator,
456-
ConditionMatchTypes.SUBSTRING: substring_evaluator
531+
ConditionMatchTypes.SUBSTRING: substring_evaluator,
532+
ConditionMatchTypes.LESS_THAN_OR_EQUAL: less_than_or_equal_evaluator,
533+
ConditionMatchTypes.GREATER_THAN_OR_EQUAL: greater_than_or_equal_evaluator,
457534
}
458535

459536
def evaluate(self, index):

0 commit comments

Comments
 (0)