Skip to content

Commit 0b2f019

Browse files
committed
Revert "Introducing feature variable accessors (#86)"
This reverts commit db4a9a6.
1 parent 939cabe commit 0b2f019

File tree

5 files changed

+12
-360
lines changed

5 files changed

+12
-360
lines changed

optimizely/optimizely.py

Lines changed: 1 addition & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import sys
1616

1717
from . import decision_service
18-
from . import entities
1918
from . import event_builder
2019
from . import exceptions
2120
from . import project_config
2221
from .error_handler import NoOpErrorHandler as noop_error_handler
2322
from .event_dispatcher import EventDispatcher as default_event_dispatcher
2423
from .helpers import enums
24+
from .helpers import event_tag_utils
2525
from .helpers import validator
2626
from .logger import NoOpLogger as noop_logger
2727
from .logger import SimpleLogger
@@ -182,64 +182,6 @@ def _send_impression_event(self, experiment, variation, user_id, attributes):
182182
self.notification_center.send_notifications(enums.NotificationTypes.ACTIVATE,
183183
experiment, user_id, attributes, variation, impression_event)
184184

185-
def _get_feature_variable_for_type(self, feature_key, variable_key, variable_type, user_id, attributes):
186-
""" Helper method to determine value for a certain variable attached to a feature flag based on type of variable.
187-
188-
Args:
189-
feature_key: Key of the feature whose variable's value is being accessed.
190-
variable_key: Key of the variable whose value is to be accessed.
191-
variable_type: Type of variable which could be one of boolean/double/integer/string.
192-
user_id: ID for user.
193-
attributes: Dict representing user attributes.
194-
195-
Returns:
196-
Value of the variable. None if:
197-
- Feature key is invalid.
198-
- Variable key is invalid.
199-
- Mismatch with type of variable.
200-
"""
201-
202-
feature_flag = self.config.get_feature_from_key(feature_key)
203-
if not feature_flag:
204-
return None
205-
206-
variable = self.config.get_variable_for_feature(feature_key, variable_key)
207-
if not variable:
208-
return None
209-
210-
# Return None if type differs
211-
if variable.type != variable_type:
212-
self.logger.log(
213-
enums.LogLevels.WARNING,
214-
'Requested variable type "%s", but variable is of type "%s". '
215-
'Use correct API to retrieve value. Returning None.' % (variable_type, variable.type)
216-
)
217-
return None
218-
219-
decision = self.decision_service.get_variation_for_feature(feature_flag, user_id, attributes)
220-
if decision.variation:
221-
variable_value = self.config.get_variable_value_for_variation(variable, decision.variation)
222-
self.logger.log(
223-
enums.LogLevels.INFO,
224-
'Value for variable "%s" of feature flag "%s" is %s for user "%s".' % (
225-
variable_key, feature_key, variable_value, user_id
226-
))
227-
else:
228-
variable_value = variable.defaultValue
229-
self.logger.log(
230-
enums.LogLevels.INFO,
231-
'User "%s" is not in any variation or rollout rule. '
232-
'Returning default value for variable "%s" of feature flag "%s".' % (user_id, variable_key, feature_key)
233-
)
234-
235-
try:
236-
actual_value = self.config.get_typecast_value(variable_value, variable_type)
237-
except:
238-
self.logger.log(enums.LogLevels.ERROR, 'Unable to cast value. Returning None.')
239-
actual_value = None
240-
241-
return actual_value
242-
243185
def activate(self, experiment_key, user_id, attributes=None):
244186
""" Buckets visitor and sends impression event to Optimizely.
245187
@@ -413,82 +355,6 @@ def get_enabled_features(self, user_id, attributes=None):
413355

414356
return enabled_features
415357

416-
def get_feature_variable_boolean(self, feature_key, variable_key, user_id, attributes=None):
417-
""" Returns value for a certain boolean variable attached to a feature flag.
418-
419-
Args:
420-
feature_key: Key of the feature whose variable's value is being accessed.
421-
variable_key: Key of the variable whose value is to be accessed.
422-
user_id: ID for user.
423-
attributes: Dict representing user attributes.
424-
425-
Returns:
426-
Boolean value of the variable. None if:
427-
- Feature key is invalid.
428-
- Variable key is invalid.
429-
- Mismatch with type of variable.
430-
"""
431-
432-
variable_type = entities.Variable.Type.BOOLEAN
433-
return self._get_feature_variable_for_type(feature_key, variable_key, variable_type, user_id, attributes)
434-
435-
def get_feature_variable_double(self, feature_key, variable_key, user_id, attributes=None):
436-
""" Returns value for a certain double variable attached to a feature flag.
437-
438-
Args:
439-
feature_key: Key of the feature whose variable's value is being accessed.
440-
variable_key: Key of the variable whose value is to be accessed.
441-
user_id: ID for user.
442-
attributes: Dict representing user attributes.
443-
444-
Returns:
445-
Double value of the variable. None if:
446-
- Feature key is invalid.
447-
- Variable key is invalid.
448-
- Mismatch with type of variable.
449-
"""
450-
451-
variable_type = entities.Variable.Type.DOUBLE
452-
return self._get_feature_variable_for_type(feature_key, variable_key, variable_type, user_id, attributes)
453-
454-
def get_feature_variable_integer(self, feature_key, variable_key, user_id, attributes=None):
455-
""" Returns value for a certain integer variable attached to a feature flag.
456-
457-
Args:
458-
feature_key: Key of the feature whose variable's value is being accessed.
459-
variable_key: Key of the variable whose value is to be accessed.
460-
user_id: ID for user.
461-
attributes: Dict representing user attributes.
462-
463-
Returns:
464-
Integer value of the variable. None if:
465-
- Feature key is invalid.
466-
- Variable key is invalid.
467-
- Mismatch with type of variable.
468-
"""
469-
470-
variable_type = entities.Variable.Type.INTEGER
471-
return self._get_feature_variable_for_type(feature_key, variable_key, variable_type, user_id, attributes)
472-
473-
def get_feature_variable_string(self, feature_key, variable_key, user_id, attributes=None):
474-
""" Returns value for a certain string variable attached to a feature.
475-
476-
Args:
477-
feature_key: Key of the feature whose variable's value is being accessed.
478-
variable_key: Key of the variable whose value is to be accessed.
479-
user_id: ID for user.
480-
attributes: Dict representing user attributes.
481-
482-
Returns:
483-
String value of the variable. None if:
484-
- Feature key is invalid.
485-
- Variable key is invalid.
486-
- Mismatch with type of variable.
487-
"""
488-
489-
variable_type = entities.Variable.Type.STRING
490-
return self._get_feature_variable_for_type(feature_key, variable_key, variable_type, user_id, attributes)
491-
492358
def set_forced_variation(self, experiment_key, user_id, variation_key):
493359
""" Force a user into a variation for a given experiment.
494360

optimizely/project_config.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _deserialize_audience(audience_map):
154154

155155
return audience_map
156156

157-
def get_typecast_value(self, value, type):
157+
def _get_typecast_value(self, value, type):
158158
""" Helper method to determine actual value based on type of feature variable.
159159
160160
Args:
@@ -421,13 +421,12 @@ def get_variable_value_for_variation(self, variable, variation):
421421
""" Get the variable value for the given variation.
422422
423423
Args:
424-
variable: The Variable for which we are getting the value.
425-
variation: The Variation for which we are getting the variable value.
424+
Variable: The Variable for which we are getting the value.
425+
Variation: The Variation for which we are getting the variable value.
426426
427427
Returns:
428-
The variable value or None if any of the inputs are invalid.
428+
The type-casted variable value or None if any of the inputs are invalid.
429429
"""
430-
431430
if not variable or not variation:
432431
return None
433432

@@ -439,13 +438,13 @@ def get_variable_value_for_variation(self, variable, variation):
439438
variable_usages = self.variation_variable_usage_map[variation.id]
440439

441440
# Find usage in given variation
442-
variable_usage = variable_usages.get(variable.id)
441+
variable_usage = variable_usages[variable.id]
443442

444-
# Return default value in case there is no variable usage for the variable.
445-
return variable_usage.value if variable_usage else variable.defaultValue
443+
value = self._get_typecast_value(variable_usage.value, variable.type)
444+
return value
446445

447446
def get_variable_for_feature(self, feature_key, variable_key):
448-
""" Get the variable with the given variable key for the given feature.
447+
""" Get the variable with the given variable key for the given feature
449448
450449
Args:
451450
feature_key: The key of the feature for which we are getting the variable.

tests/base.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,10 @@ def setUp(self):
176176
'id': '127', 'value': 'false'
177177
}, {
178178
'id': '128', 'value': 'prod'
179-
}, {
180-
'id': '129', 'value': '10.01'
181-
}, {
182-
'id': '130', 'value': '4242'
183179
}]
184180
}, {
185181
'key': 'variation',
186-
'id': '111129',
187-
'variables': [{
188-
'id': '127', 'value': 'true'
189-
}, {
190-
'id': '128', 'value': 'staging'
191-
}, {
192-
'id': '129', 'value': '10.02'
193-
}, {
194-
'id': '130', 'value': '4243'
195-
}]
182+
'id': '111129'
196183
}]
197184
}],
198185
'groups': [{
@@ -337,21 +324,6 @@ def setUp(self):
337324
'key': 'environment',
338325
'defaultValue': 'devel',
339326
'type': 'string',
340-
}, {
341-
'id': '129',
342-
'key': 'cost',
343-
'defaultValue': '10.99',
344-
'type': 'double',
345-
}, {
346-
'id': '130',
347-
'key': 'count',
348-
'defaultValue': '999',
349-
'type': 'integer',
350-
}, {
351-
'id': '131',
352-
'key': 'variable_without_usage',
353-
'defaultValue': '45',
354-
'type': 'integer',
355327
}]
356328
}, {
357329
'id': '91112',

tests/test_config.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,14 +1197,14 @@ def test_get_rollout_from_id__valid_rollout_id(self):
11971197
self.assertEqual(expected_rollout, project_config.get_rollout_from_id('211111'))
11981198

11991199
def test_get_variable_value_for_variation__returns_valid_value(self):
1200-
""" Test that the right value is returned. """
1200+
""" Test that the right value and type are returned. """
12011201
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
12021202
project_config = opt_obj.config
12031203

12041204
variation = project_config.get_variation_from_id('test_experiment', '111128')
12051205
is_working_variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'is_working')
12061206
environment_variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'environment')
1207-
self.assertEqual('false', project_config.get_variable_value_for_variation(is_working_variable, variation))
1207+
self.assertEqual(False, project_config.get_variable_value_for_variation(is_working_variable, variation))
12081208
self.assertEqual('prod', project_config.get_variable_value_for_variation(environment_variable, variation))
12091209

12101210
def test_get_variable_value_for_variation__invalid_variable(self):
@@ -1226,17 +1226,6 @@ def test_get_variable_value_for_variation__no_variables_for_variation(self):
12261226
is_working_variable = project_config.get_variable_for_feature('test_feature_in_experiment', 'is_working')
12271227
self.assertIsNone(project_config.get_variable_value_for_variation(is_working_variable, variation))
12281228

1229-
def test_get_variable_value_for_variation__no_usage_of_variable(self):
1230-
""" Test that a variable with no usage will return default value for variable. """
1231-
1232-
opt_obj = optimizely.Optimizely(json.dumps(self.config_dict_with_features))
1233-
project_config = opt_obj.config
1234-
1235-
variation = project_config.get_variation_from_id('test_experiment', '111128')
1236-
variable_without_usage_variable = project_config.get_variable_for_feature('test_feature_in_experiment',
1237-
'variable_without_usage')
1238-
self.assertEqual('45', project_config.get_variable_value_for_variation(variable_without_usage_variable, variation))
1239-
12401229
def test_get_variable_for_feature__returns_valid_variable(self):
12411230
""" Test that the feature variable is returned. """
12421231

0 commit comments

Comments
 (0)