forked from splitio/python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplitters.py
More file actions
69 lines (58 loc) · 2.14 KB
/
Copy pathsplitters.py
File metadata and controls
69 lines (58 loc) · 2.14 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""A module for implementation of the Splitter engine"""
from __future__ import absolute_import, division, print_function, unicode_literals
from splitio.treatments import CONTROL
from splitio.hashfns import get_hash_fn
class Splitter(object):
"""
The class responsible for selecting a treatment given a key, a feature seed and condition
partitions.
"""
def get_treatment(self, key, seed, partitions, algo):
"""
Returs a treatment for a key, a feature seed and condition partitions. It returns CONTROL
if partitions is None or empty.
:param key: The key for which to determine the treatment
:type key: str
:param seed: The feature seed
:type seed: int
:param partitions: The condition partitions
:type partitions: list
:return: The treatment
:rtype: str
"""
if not partitions:
return CONTROL
if len(partitions) == 1 and partitions[0].size == 100:
return partitions[0].treatment
return self.get_treatment_for_bucket(
self.get_bucket(key, seed, algo),
partitions
)
def get_bucket(self, key, seed, algo):
"""
Get the bucket for a key hash
:param key_hash: The hash for a key
:type key_hash: int
:return: The bucked for a hash
:rtype: int
"""
hashfn = get_hash_fn(algo)
key_hash = hashfn(key, seed)
return abs(key_hash) % 100 + 1
def get_treatment_for_bucket(self, bucket, partitions):
"""
Gets the treatment for a given bucket and partitions. It'll return treatment for the first
partition that contains the bucket.
:param bucket: The bucket number generated by get_bucket
:type bucket: int
:param partitions: The condition partitions
:type partitions: list
:return: The treatment
:rtype: str
"""
covered_buckets = 0
for partition in partitions:
covered_buckets += partition.size
if covered_buckets >= bucket:
return partition.treatment
return CONTROL