Reference library

A/B testing & experimentation

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

5 matches
A/B testing & experimentation easy

How to Do Random Assignment in Python for A/B Tests

Assign each item to a binary group (0 or 1) with uniform probability using a small reusable function, optionally weighted, for A/B testing mocks.

random ab-testing assignment
Python
import random

def random_assignment_uniform_mock(items, weights=None):
    """Assign each item to a group (0 or 1) with uniform probability."""
    if weights is None:
        # Default: each item independently gets 0 or 1 with 50% probability
        return [random.randint(0, 1) for _ in items]
    # Optional weight…
13 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 Sequential Calls in Python with unittest.mock

Use Mock.side_effect to return a different result for each sequential call and verify the call order with assert_has_calls.

mock unittest testing
Python
import unittest
from unittest.mock import Mock

class Service:
    def fetch(self, item_id):
        raise NotImplementedError

def process_items(service, ids):
    results = []
    for item_id in ids:
        result = service.fetch(item_id)
        results.append(result)
    return results

if __name__ == "__main__":…
14 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 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

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.