Efficiently assign users to buckets.
Project description
choixpeau
Install
pip install choixpeau
Usage
from choixpeau import choixpeau
redis_config = {
"host": "localhost
}
choixpeau = choixpeau.Choixpeau(
redis_config=redis_config,
ab_test_ids=["poudlard"],
buckets=["gryffondor", "poufsouffle", "serdaigle", "serpentard"] # ["A", "B"] by default
)
get
choixpeau.get("harrypotter")
# if the key already exists
> [(None, { "ab_test_group": "gryffondor", "created_at": "2021-01-29" }, "poudlard")]
# if it does not
> [("ab:poudlard:harrypotter", { "ab_test_group": "gryffondor", "created_at": "2021-01-29" }, "poudlard")]
store
choixpeau.store(
"ab:poudlard:harrypotter",
{ "ab_test_group": "gryffondor", "created_at": "2021-01-29" }
)
FastAPI
from fastapi import FastAPI, Request, BackgroundTasks
from choixpeau import choixpeau
from choixpeau.decorators.fastapi import ab
app = fastapi.FastAPI()
redis_config = {
"host": "localhost
}
@app.on_event("startup")
async def startup():
# Initialize choixpeau at the app level
app.state.choixpeau = choixpeau.Choixpeau(
redis_config=redis_config,
ab_test_ids=["poudlard"],
buckets=["gryffondor", "poufsouffle", "serdaigle", "serpentard"]
)
@app.post("/")
@ab # requires the request body to have a user_id field
async def read_house(request: Request, background_tasks: BackgroundTasks):
wizard = request.state.user # the user attribute is automatically added to the request
if wizard["ab_test_group"] == "gryffondor":
return { "message": "Welcome to Gryffondor!" }
else:
return { "message": "Welcome!" }
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
choixpeau-0.0.6.tar.gz
(4.2 kB
view hashes)
Built Distribution
Close
Hashes for choixpeau-0.0.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 239b4e8eb8b9e817777999a4f07fa06e64909063e558d8ca77ec6a2cd01d3a37 |
|
MD5 | f8acb14eea9c0e96b5a635ebbc1bd0d8 |
|
BLAKE2b-256 | 0db159f113af7f76851eed3736ede4e76067e4ea96bd0c16203495180d4ceb5f |