Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Dec 11, 2018
1 parent 992ce35 commit e9d3b48
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 67 deletions.
72 changes: 36 additions & 36 deletions optimizely/helpers/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from six import string_types

from .validator import is_finite_number, are_values_same_type
from . import validator


class ConditionOperatorTypes(object):
Expand All @@ -43,107 +43,107 @@ def __init__(self, condition_data, attributes):
self.attributes = attributes or {}

def is_value_valid_for_exact_conditions(self, value):
""" Method to validate if the value is valid for exact match type evaluation
""" Method to validate if the value is valid for exact match type evaluation.
Args:
value: Value to validate
value: Value to validate.
Returns:
Boolean: True if value is a string type, or a boolean, or is finite. Otherwise False
Boolean: True if value is a string type, or a boolean, or is finite. Otherwise False.
"""
if isinstance(value, string_types) or isinstance(value, bool) or is_finite_number(value):
if isinstance(value, string_types) or isinstance(value, bool) or validator.is_finite_number(value):
return True

return False

def exact_evaluator(self, index):
""" Evaluate the given exact match condition for the user attributes
""" Evaluate the given exact match condition for the user attributes.
Args:
index: Index of the condition to be evaluated
index: Index of the condition to be evaluated.
Returns:
Boolean:
- True if the user attribute value is equal (===) to the condition value
- False if the user attribute value is not equal (!==) to the condition value
- True if the user attribute value is equal (===) to the condition value.
- False if the user attribute value is not equal (!==) to the condition value.
None:
- if the condition value or user attribute value has an invalid type
- if there is a mismatch between the user attribute type and the condition value type
- if the condition value or user attribute value has an invalid type.
- if there is a mismatch between the user attribute type and the condition value type.
"""
condition_value = self.condition_data[index][1]
user_value = self.attributes.get(self.condition_data[index][0])

if not self.is_value_valid_for_exact_conditions(condition_value) or \
not self.is_value_valid_for_exact_conditions(user_value) or \
not are_values_same_type(condition_value, user_value):
not validator.are_values_same_type(condition_value, user_value):
return None

return condition_value == user_value

def exists_evaluator(self, index):
""" Evaluate the given exists match condition for the user attributes
""" Evaluate the given exists match condition for the user attributes.
Args:
index: Index of the condition to be evaluated
index: Index of the condition to be evaluated.
Returns:
Boolean: True if the user attributes have a non-null value for the given condition,
otherwise False
otherwise False.
"""
attr_name = self.condition_data[index][0]
return self.attributes.get(attr_name) is not None

def greater_than_evaluator(self, index):
""" Evaluate the given greater than match condition for the user attributes
""" Evaluate the given greater than match condition for the user attributes.
Args:
index: Index of the condition to be evaluated
index: Index of the condition to be evaluated.
Returns:
Boolean:
- True if the user attribute value is greater than the condition value
- False if the user attribute value is less than or equal to the condition value
None: if the condition value isn't finite or the user attribute value isn't finite
- True if the user attribute value is greater than the condition value.
- False if the user attribute value is less than or equal to the condition value.
None: if the condition value isn't finite or the user attribute value isn't finite.
"""
condition_value = self.condition_data[index][1]
user_value = self.attributes.get(self.condition_data[index][0])

if not is_finite_number(condition_value) or not is_finite_number(user_value):
if not validator.is_finite_number(condition_value) or not validator.is_finite_number(user_value):
return None

return user_value > condition_value

def less_than_evaluator(self, index):
""" Evaluate the given less than match condition for the user attributes
""" Evaluate the given less than match condition for the user attributes.
Args:
index: Index of the condition to be evaluated
index: Index of the condition to be evaluated.
Returns:
Boolean:
- True if the user attribute value is less than the condition value
- False if the user attribute value is greater than or equal to the condition value
None: if the condition value isn't finite or the user attribute value isn't finite
- True if the user attribute value is less than the condition value.
- False if the user attribute value is greater than or equal to the condition value.
None: if the condition value isn't finite or the user attribute value isn't finite.
"""
condition_value = self.condition_data[index][1]
user_value = self.attributes.get(self.condition_data[index][0])

if not is_finite_number(condition_value) or not is_finite_number(user_value):
if not validator.is_finite_number(condition_value) or not validator.is_finite_number(user_value):
return None

return user_value < condition_value

def substring_evaluator(self, index):
""" Evaluate the given substring match condition for the given user attributes
""" Evaluate the given substring match condition for the given user attributes.
Args:
index: Index of the condition to be evaluated
index: Index of the condition to be evaluated.
Returns:
Boolean:
- True if the condition value is a substring of the user attribute value
- False if the condition value is not a substring of the user attribute value
None: if the condition value isn't a string or the user attribute value isn't a string
- True if the condition value is a substring of the user attribute value.
- False if the condition value is not a substring of the user attribute value.
None: if the condition value isn't a string or the user attribute value isn't a string.
"""
condition_value = self.condition_data[index][1]
user_value = self.attributes.get(self.condition_data[index][0])
Expand All @@ -166,13 +166,13 @@ def evaluate(self, index):
condition against the attributes.
Args:
index: Index of the condition to be evaluated
index: Index of the condition to be evaluated.
Returns:
Boolean:
- True if the user attributes match the given condition
- False if the user attributes don't match the given condition
None: if the user attributes and condition can't be evaluated
- True if the user attributes match the given condition.
- False if the user attributes don't match the given condition.
None: if the user attributes and condition can't be evaluated.
"""

if self.condition_data[index][2] != self.CUSTOM_ATTRIBUTE_CONDITION_TYPE:
Expand Down
44 changes: 22 additions & 22 deletions optimizely/helpers/condition_tree_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

def and_evaluator(conditions, leaf_evaluator):
""" Evaluates a list of conditions as if the evaluator had been applied
to each entry and the results AND-ed together
to each entry and the results AND-ed together.
Args:
conditions: List of conditions ex: [operand_1, operand_2]
leaf_evaluator: Function which will be called to evaluate leaf condition values
conditions: List of conditions ex: [operand_1, operand_2].
leaf_evaluator: Function which will be called to evaluate leaf condition values.
Returns:
Boolean:
- True if all operands evaluate to True
- False if a single operand evaluates to False
None: if conditions couldn't be evaluated
- True if all operands evaluate to True.
- False if a single operand evaluates to False.
None: if conditions couldn't be evaluated.
"""
saw_null_result = False

Expand All @@ -42,17 +42,17 @@ def and_evaluator(conditions, leaf_evaluator):

def or_evaluator(conditions, leaf_evaluator):
""" Evaluates a list of conditions as if the evaluator had been applied
to each entry and the results OR-ed together
to each entry and the results OR-ed together.
Args:
conditions: List of conditions ex: [operand_1, operand_2]
leaf_evaluator: Function which will be called to evaluate leaf condition values
conditions: List of conditions ex: [operand_1, operand_2].
leaf_evaluator: Function which will be called to evaluate leaf condition values.
Returns:
Boolean:
- True if any operand evaluates to True
- False if all operands evaluate to False
None: if conditions couldn't be evaluated
- True if any operand evaluates to True.
- False if all operands evaluate to False.
None: if conditions couldn't be evaluated.
"""
saw_null_result = False

Expand All @@ -71,14 +71,14 @@ def not_evaluator(conditions, leaf_evaluator):
to a single entry and NOT was applied to the result.
Args:
conditions: List of conditions ex: [operand_1, operand_2]
leaf_evaluator: Function which will be called to evaluate leaf condition values
conditions: List of conditions ex: [operand_1, operand_2].
leaf_evaluator: Function which will be called to evaluate leaf condition values.
Returns:
Boolean:
- True if the operand evaluates to False
- False if the operand evaluates to True
None: if conditions is empty or condition couldn't be evaluated
- True if the operand evaluates to False.
- False if the operand evaluates to True.
None: if conditions is empty or condition couldn't be evaluated.
"""
if not len(conditions) > 0:
return None
Expand All @@ -94,24 +94,24 @@ def not_evaluator(conditions, leaf_evaluator):


def evaluate(conditions, leaf_evaluator):
""" Top level method to evaluate conditions
""" Top level method to evaluate conditions.
Args:
conditions: Nested array of and/or conditions, or a single leaf condition value of any type
conditions: Nested array of and/or conditions, or a single leaf condition value of any type.
Example: ['and', '0', ['or', '1', '2']]
leaf_evaluator: Function which will be called to evaluate leaf condition values
leaf_evaluator: Function which will be called to evaluate leaf condition values.
Returns:
Boolean: Result of evaluating the conditions using the operator rules and the leaf evaluator.
None: if conditions couldn't be evaluated
None: if conditions couldn't be evaluated.
"""

if isinstance(conditions, list):
if conditions[0] in list(EVALUATORS_BY_OPERATOR_TYPE.keys()):
return EVALUATORS_BY_OPERATOR_TYPE[conditions[0]](conditions[1:], leaf_evaluator)
else:
# assume OR when operator is not explicit
# assume OR when operator is not explicit.
return EVALUATORS_BY_OPERATOR_TYPE[ConditionOperatorTypes.OR](conditions, leaf_evaluator)

leaf_condition = conditions
Expand Down
18 changes: 9 additions & 9 deletions optimizely/helpers/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ def is_attribute_valid(attribute_key, attribute_value):


def is_finite_number(value):
""" Method to validate if the given value is a number and not one of NAN, INF, -INF
""" Method to validate if the given value is a number and not one of NAN, INF, -INF.
Args:
value: Value to be validated
value: Value to be validated.
Returns:
Boolean: True if value is a number and not NAN, INF or -INF else False
Boolean: True if value is a number and not NAN, INF or -INF else False.
"""
if not isinstance(value, (numbers.Integral, float)):
# numbers.Integral instead of int to accomodate long integer in python 2
Expand All @@ -222,25 +222,25 @@ def are_values_same_type(first_val, second_val):
considered as same type.
Args:
first_val: Value to validate
second_Val: Value to validate
first_val: Value to validate.
second_Val: Value to validate.
Returns:
Boolean: True if both values belong to same type. Otherwise False
Boolean: True if both values belong to same type. Otherwise False.
"""

first_val_type = type(first_val)
second_val_type = type(second_val)

# use isinstance to accomodate Python 2 unicode and str types
# use isinstance to accomodate Python 2 unicode and str types.
if isinstance(first_val, string_types) and isinstance(second_val, string_types):
return True

# Compare types if one of the values is bool because bool is a subclass on Integer
# Compare types if one of the values is bool because bool is a subclass on Integer.
if isinstance(first_val, bool) or isinstance(second_val, bool):
return first_val_type == second_val_type

# Treat ints and floats as same type
# Treat ints and floats as same type.
if isinstance(first_val, (numbers.Integral, float)) and isinstance(second_val, (numbers.Integral, float)):
return True

Expand Down

0 comments on commit e9d3b48

Please sign in to comment.