Skip to content

Commit c8d68f7

Browse files
Added comments to stringify_conditions, added testcase to test stringy for another edge case ['and'], changed method to combine typed_audiences and audiences from project_config to use lookup map rather than build a list and check length. Reduces time complexity.
1 parent a273d11 commit c8d68f7

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

optimizely/optimizely_config.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,33 +123,31 @@ def __init__(self, project_config):
123123

124124
'''
125125
Merging typed_audiences with audiences from project_config.
126-
The typed_audiences has higher presidence.
126+
The typed_audiences has higher precidence.
127127
'''
128128

129129
typed_audiences = project_config.typed_audiences[:]
130130
optly_typed_audiences = []
131+
id_lookup_dict = {}
131132
for typed_audience in typed_audiences:
132133
optly_audience = OptimizelyAudience(
133134
typed_audience.get('id'),
134135
typed_audience.get('name'),
135136
typed_audience.get('conditions')
136137
)
137138
optly_typed_audiences.append(optly_audience)
139+
id_lookup_dict[typed_audience.get('id')] = typed_audience.get('id')
138140

139141
for old_audience in project_config.audiences:
140142
# check if old_audience.id exists in new_audiences.id from typed_audiences
141-
if len([new_audience for new_audience in project_config.typed_audiences
142-
if new_audience.get('id') == old_audience.get('id')]) == 0:
143-
if old_audience.get('id') == "$opt_dummy_audience":
144-
continue
145-
else:
146-
# Convert audiences lists to OptimizelyAudience array
147-
optly_audience = OptimizelyAudience(
148-
old_audience.get('id'),
149-
old_audience.get('name'),
150-
old_audience.get('conditions')
151-
)
152-
optly_typed_audiences.append(optly_audience)
143+
if old_audience.get('id') not in id_lookup_dict and old_audience.get('id') != "$opt_dummy_audience":
144+
# Convert audiences lists to OptimizelyAudience array
145+
optly_audience = OptimizelyAudience(
146+
old_audience.get('id'),
147+
old_audience.get('name'),
148+
old_audience.get('conditions')
149+
)
150+
optly_typed_audiences.append(optly_audience)
153151

154152
self.audiences = optly_typed_audiences
155153

@@ -193,13 +191,14 @@ def stringify_conditions(self, conditions, audiences_map):
193191
list of conditions.
194192
'''
195193
ARGS = ConditionOperatorTypes.operators
196-
condition = 'OR'
194+
operand = 'OR'
197195
conditions_str = ''
198196
length = len(conditions)
199197

198+
# Edge cases for lengths 0, 1 or 2
200199
if length == 0:
201200
return ''
202-
if length == 1:
201+
if length == 1 and conditions[0] not in ARGS:
203202
return '"' + self.lookup_name_from_id(conditions[0], audiences_map) + '"'
204203
if length == 2 and conditions[0] in ARGS and \
205204
type(conditions[1]) is not list and \
@@ -209,24 +208,31 @@ def stringify_conditions(self, conditions, audiences_map):
209208
else:
210209
return conditions[0].upper() + \
211210
' "' + self.lookup_name_from_id(conditions[1], audiences_map) + '"'
211+
# If length is 2 (where the one elemnt is a list) or greater
212212
if length > 1:
213213
for i in range(length):
214+
# Operand is handled here and made Upper Case
214215
if conditions[i] in ARGS:
215-
condition = conditions[i].upper()
216+
operand = conditions[i].upper()
216217
else:
218+
# Check if element is a list or not
217219
if type(conditions[i]) == list:
220+
# Check if at the end or not to determine where to add the operand
221+
# Recursive call to call stringify on embedded list
218222
if i + 1 < length:
219223
conditions_str += '(' + self.stringify_conditions(conditions[i], audiences_map) + ') '
220224
else:
221-
conditions_str += condition + \
225+
conditions_str += operand + \
222226
' (' + self.stringify_conditions(conditions[i], audiences_map) + ')'
227+
# Not a list so we handle as and ID to lookup no recursion needed
223228
else:
224229
audience_name = self.lookup_name_from_id(conditions[i], audiences_map)
225230
if audience_name is not None:
231+
# Below handles all cases for one ID or greater
226232
if i + 1 < length - 1:
227-
conditions_str += '"' + audience_name + '" ' + condition + ' '
233+
conditions_str += '"' + audience_name + '" ' + operand + ' '
228234
elif i + 1 == length:
229-
conditions_str += condition + ' "' + audience_name + '"'
235+
conditions_str += operand + ' "' + audience_name + '"'
230236
else:
231237
conditions_str += '"' + audience_name + '" '
232238

tests/test_optimizely_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,8 @@ def test_stringify_audience_conditions_all_cases(self):
13991399
["and", ["or", "1", ["and", "2", "3"]], ["and", "11", ["or", "12", "13"]]],
14001400
["not", ["and", "1", "2"]],
14011401
["or", "1", "100000"],
1402-
["and", "and"]
1402+
["and", "and"],
1403+
["and"]
14031404
]
14041405

14051406
audiences_output = [
@@ -1415,6 +1416,7 @@ def test_stringify_audience_conditions_all_cases(self):
14151416
'("us" OR ("female" AND "adult")) AND ("fr" AND ("male" OR "kid"))',
14161417
'NOT ("us" AND "female")',
14171418
'"us" OR "100000"',
1419+
'',
14181420
''
14191421
]
14201422

0 commit comments

Comments
 (0)