Providers

Context

Bases: UserDict

Context contains contextual data for use by experiments in determining matching variants.

identity(keypath, seed)

Uses context data to calculate an identity for the given keypath and seed.

Parameters:
  • keypath (str) –

    A path to specifying the key of the value in the context that is to be used as the identity. Paths are in object notation (period-delimited).

  • seed (str) –

    A seed value that is combined with the key to produce an identity.

keypath has a special value, "random", that will return a random value for use as the identity. Seed has no effect if "random" is used as the keypath.

Identity values are cached so, the same keypath/seed pair will not be re-calculated if identity() is called again.

Raises:
  • KeyError

    If the keypath does not exist in the context or its corresponding value is None.

ContextProvider

Bases: ABC

Base class for context providers.

Context providers populate contexts.

context(**kwargs) abstractmethod

Constructs a context.

DjangoRequestContextProvider

Bases: ContextProvider

A context provider for Django environments.

Pre-populates the context with the Django user (user) and tracking key for identifying anonymous users (anonymous).

context(**kwargs)

Constructs a DjangoRequestContextProvider.

Parameters:
  • **kwargs (dict[str, Any], default: {} ) –

    Arbitrary keyword arguments. - request (django.http.HttpRequest): Django request. Must be passed as a keyword argument.

Source

Bases: ABC

Protocol for experiment sources.

Classes implementing this protocol must provide a load method that returns an iterable of Experiment instances. This allows flexible sourcing of experiments from different backends (e.g., settings, database, API).

load() abstractmethod

Loads all experiments that the project should be aware of.

For projects that use cravensworth_middleware, load() will be called for every request.