-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexperiment.py
More file actions
53 lines (46 loc) · 2.21 KB
/
experiment.py
File metadata and controls
53 lines (46 loc) · 2.21 KB
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
import time
from splitcli.experiment.batch_client import BatchClient
from splitcli.experiment.event_result import EventResult
class Experiment(object):
def __init__(self, sdk_token, feature, comp_treatment="on", key_pattern="user_{position}", traffic_type="user", attributes={}):
super(Experiment, self).__init__()
self.sdk_token = sdk_token
self.split_client = BatchClient(sdk_token)
self.feature = feature
self.comp_treatment = comp_treatment
self.key_pattern = key_pattern
self.event_results = []
self.traffic_type = traffic_type
self.attributes = attributes
def run(self, sample_size):
(base_keys, comp_keys) = self.get_samples(sample_size)
for event_result in self.event_results:
(base_events, comp_events) = event_result.events(len(base_keys), len(comp_keys))
self.send_events(event_result, base_events, base_keys)
self.send_events(event_result, comp_events, comp_keys)
self.split_client.destroy()
def get_samples(self, sample_size):
base_keys = []
comp_keys = []
for position in range(sample_size):
key = self.key(position)
treatment = self.split_client.get_treatment(key, self.feature, self.attributes)
if treatment == self.comp_treatment:
comp_keys.append(key)
else:
base_keys.append(key)
return (base_keys, comp_keys)
def send_events(self, event_result, events, keys):
for (key, (count, value)) in zip(keys,events):
properties = event_result.properties
if event_result.property_value is not None:
properties[event_result.property_value] = value
for _ in range(count):
time.sleep(.001)
self.split_client.track(key, self.traffic_type, event_result.event_type, value, properties)
def key(self, position):
return self.key_pattern.format(position=position)
def register(self, eventType, properties={}, property_value=None):
event_result = EventResult(eventType, properties, property_value=property_value)
self.event_results.append(event_result)
return event_result