Reference library

A/B testing & experimentation

User bucketing, experiment metrics, statistical comparison, and rollout guardrails.

7 matches
A/B testing & experimentation easy

How to Create a Sticky Consistent Mock with unittest.mock in Python

Shows how to use unittest.mock.patch.object to mock a method consistently across multiple calls, returning a sticky value every time.

unittest mock testing
Python
from unittest.mock import patch

class Database:
    def fetch(self, key):
        return f"real value for {key}"

def get_value(db, key):
    return db.fetch(key)

if __name__ == "__main__":
    db = Database()
    with patch.object(db, "fetch", return_value="sticky value") as mock_fetch:
        result1 = get_value(…
15 0 Open
A/B testing & experimentation medium

How to Create an Interrupted Time Series Mock in Python

Generate simulated interrupted time series data with a pre/post-intervention trend, level shift, and noise to test segmented regression models.

interrupted-time-series simulation numpy
Python
import numpy as np

# Mock interrupted time series data
np.random.seed(42)
n_pre = 50
n_post = 50
time = np.arange(0, n_pre + n_post)

# Pre-intervention: linear trend + noise
pre_trend = 0.05 * time[:n_pre] + np.random.normal(0, 0.5, n_pre)

# Post-intervention: new slope + level shift + noise
post_trend = 0.05 * tim…
15 0 Open
A/B testing & experimentation medium

How to Mock Mutual Exclusion for A/B Experiment Groups in Python

Simulate mutual exclusion for experiment groups using a thread-safe lock, ensuring only one member updates the shared counter at a time.

threading mutual-exclusion ab-testing
Python
import threading
import time
import random


class CountingGate:
    """A mock mutual exclusion gate using a lock."""
    def __init__(self):
        self.counter = 0
        self.lock = threading.Lock()

    def enter(self, group_id, member_id):
        with self.lock:
            current = self.counter
            t…
13 0 Open
A/B testing & experimentation medium

How to Mock Time for Cache TTL Testing in Python

This code demonstrates how to test a cache's TTL expiration logic by mocking time.time with unittest.mock to control the passage of time.

caching ttl unit-testing
Python
import time
from unittest.mock import patch

class ConfigCache:
    def __init__(self, ttl=60):
        self.ttl = ttl
        self._store = {}
        self._timestamps = {}

    def get(self, key):
        if key not in self._store:
            return None
        if time.time() - self._timestamps[key] > self.ttl:
  …
17 0 Open
A/B testing & experimentation easy

How to Mock a Remote Config Fetch in Python

Simulate a remote config API response with metadata, timestamps, and mock data for testing or local development.

mock config testing
Python
import json
from datetime import datetime
from typing import Any, Dict

def fetch_remote_config(mock_data: Dict[str, Any]) -> Dict[str, Any]:
    """Simulate fetching a remote config with metadata and timestamps."""
    return {
        "status": "success",
        "source": "mock",
        "fetched_at": datetime.utcn…
14 0 Open
A/B testing & experimentation easy

How to Mock an Exposure Event Log Record in Python

Generate a realistic exposure event record with UUID, UTC timestamp, and risk level for testing or experimentation.

mocking events testing
Python
import uuid
from datetime import datetime, timezone


def mock_exposure_event(person_id: str, location: str, duration_minutes: int) -> dict:
    return {
        "event_id": str(uuid.uuid4()),
        "person_id": person_id,
        "location": location,
        "duration_minutes": duration_minutes,
        "timestamp…
16 0 Open
A/B testing & experimentation medium

Thompson Sampling Mock Bandit in Python

Implement a Thompson sampling multi-armed bandit to explore and exploit reward probabilities across multiple options, updating Beta distributions over time.

thompson-sampling bandit-algorithms exploration-exploitation
Python
import random

class ThompsonSamplingBandit:
    def __init__(self, num_arms, alpha=1.0, beta=1.0):
        self.num_arms = num_arms
        self.alpha = [alpha] * num_arms
        self.beta = [beta] * num_arms

    def select_arm(self):
        samples = [random.betavariate(a, b) for a, b in zip(self.alpha, self.beta…
12 0 Open

Browse by section

Each section groups closely related Python snippets.

A/B testing & experimentation — Python code examples

What you will find here

This page collects a/b testing & experimentation snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.