BanditMiddleware

flask_mab

This module implements all the extension logic for Multi-armed bandit experiments on Flask apps.

copyright:
  1. 2013 by Mark Grey.
license:

BSD, see LICENSE for more details.

class flask_mab.__init__.BanditMiddleware(app=None)[source]

Bases: object

The main flask extension. Sets up all the necessary tracking for the bandit experiments

init_app(app)[source]

Attach Multi Armed Bandit to application and configure

Parameters:app – A flask application instance
teardown(*args, **kwargs)[source]

Stub for old flask versions

exception flask_mab.__init__.MABConfigException[source]

Bases: exceptions.Exception

Raised when internal state in MAB setup is invalid

flask_mab.__init__.add_bandit(app, name, bandit=None)[source]

Attach a bandit for an experiment

Parameters:
  • name – The name of the experiment, will be used for lookups
  • bandit (Bandit) – The bandit to use for this experiment
flask_mab.__init__.choose_arm(bandit)[source]

Route decorator for registering an impression conveinently

Parameters:bandit (string) – The bandit/experiment to register for
flask_mab.__init__.reward_endpt(bandit, reward_val=1)[source]

Route decorator for rewards.

Parameters:
  • bandit (string) – The bandit/experiment to register rewards for using arm found in cookie.
  • reward (float) – The amount of reward this endpoint should give its winning arm
flask_mab.__init__.suggest_arm_for(key)[source]

Get an experimental outcome by id. The primary way the implementor interfaces with their experiments.

Suggests arms if not in cookie, using cookie val if present

Parameters:
  • key (string) – The bandit/experiment to get a suggested arm for
  • also_pull – Should we register a pull/impression at the same time as suggesting
Raises KeyError:
 

in case requested experiment does not exist

Fork me on GitHub