Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions eppo_client/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
from typing import Optional
from eppo_client.configuration_requestor import (
ExperimentConfigurationDto,
Expand Down Expand Up @@ -37,6 +38,9 @@ def assign(self, subject: str, experiment_key: str) -> Optional[str]:
)
):
return None
override = self._get_subject_variation_override(experiment_config, subject)
if override:
return override
shard = get_shard(
"assignment-{}-{}".format(subject, experiment_key),
experiment_config.subject_shards,
Expand All @@ -56,6 +60,14 @@ def _shutdown(self):
"""
self.__poller.stop()

def _get_subject_variation_override(
self, experiment_config: ExperimentConfigurationDto, subject: str
) -> Optional[str]:
subject_hash = hashlib.md5(subject.encode("utf-8")).hexdigest()
if subject_hash in experiment_config.overrides:
return experiment_config.overrides[subject_hash]
return None

def _is_in_experiment_sample(
self,
subject: str,
Expand Down
1 change: 1 addition & 0 deletions eppo_client/configuration_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ExperimentConfigurationDto(SdkBaseModel):
enabled: bool
variations: List[VariationDto]
name: Optional[str]
overrides: Dict[str, str] = {}


RAC_ENDPOINT = "/randomized_assignment/config"
Expand Down
17 changes: 17 additions & 0 deletions test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,28 @@ def test_assign_subject_not_in_sample(mock_config_requestor):
VariationDto(name="control", shardRange=ShardRange(start=0, end=100))
],
name="recommendation_algo",
overrides=dict(),
)
client = EppoClient(config_requestor=mock_config_requestor)
assert client.assign("user-1", "experiment-key-1") is None


@patch("eppo_client.configuration_requestor.ExperimentConfigurationRequestor")
def test_with_subject_in_overrides(mock_config_requestor):
mock_config_requestor.get_configuration.return_value = ExperimentConfigurationDto(
subjectShards=10000,
percentExposure=100,
enabled=True,
variations=[
VariationDto(name="control", shardRange=ShardRange(start=0, end=100))
],
name="recommendation_algo",
overrides={"d6d7705392bc7af633328bea8c4c6904": "override-variation"},
)
client = EppoClient(config_requestor=mock_config_requestor)
assert client.assign("user-1", "experiment-key-1") == "override-variation"


@pytest.mark.parametrize("test_case", test_data)
def test_assign_subject_in_sample(test_case):
print("---- Test case for {} Experiment".format(test_case["experiment"]))
Expand Down