diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..6a7695c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "pip" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 605ff96..adcdf03 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,5 +1,5 @@ name: Build and test -on: [push, pull_request] +on: [pull_request] jobs: build: runs-on: ubuntu-latest @@ -13,10 +13,8 @@ jobs: run: python -m pip install --upgrade build - name: Install dependencies run: python -m pip install '.[dev]' - - name: Lint - run: python -m black --check flask_mab tests - name: Run tests - run: pytest + run: tox - name: Build dist run: python -m build . - name: Build docs diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 110b6b8..33fa32c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -27,10 +27,8 @@ jobs: run: python -m pip install --upgrade build - name: Install dependencies run: python -m pip install '.[dev]' - - name: Lint - run: python -m black --check flask_mab tests - name: Run tests - run: pytest + run: tox - name: Build dist run: python -m build . - name: Build docs diff --git a/flask_mab/__init__.py b/flask_mab/__init__.py index 62931c2..91150e8 100644 --- a/flask_mab/__init__.py +++ b/flask_mab/__init__.py @@ -10,21 +10,15 @@ :license: BSD, see LICENSE for more details. """ -from flask import current_app, g, request +from flask import current_app, request import json import flask_mab.storage from flask_mab.mab import Mab import types -from collections import namedtuple -from flask import _request_ctx_stack from functools import wraps -try: - from flask import _app_ctx_stack as stack -except ImportError: - from flask import _request_ctx_stack as stack -__version__ = "2.0.1" +__version__ = "3.0.0" def choose_arm(bandit): diff --git a/pyproject.toml b/pyproject.toml index babe3d0..39733d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,8 @@ dev = [ "mock==5.0.2", "sphinx-pyproject==0.1.0", "sphinx==7.0.1", - "Pallets-Sphinx-Themes==2.1.1" + "Pallets-Sphinx-Themes==2.1.1", + "tox==4.14.2" ] [tool.sphinx-pyproject] diff --git a/tests/test_bandits.py b/tests/test_bandits.py index b054bd9..16d1fd0 100644 --- a/tests/test_bandits.py +++ b/tests/test_bandits.py @@ -54,11 +54,6 @@ def save_results(self, results, output_stream): ) sys.stdout.flush() - def percentage_picked(self, picks, winner): - should_win = dict(picks)[winner] - total = sum([pt[1] for pt in picks]) - return should_win / total - class EpsilonGreedyTest(MonteCarloTest): bandit_name = "EpsilonGreedyBandit" @@ -67,8 +62,8 @@ class EpsilonGreedyTest(MonteCarloTest): def test_bandit(self): results = self.run_algo(make_bandit(self.bandit_name, epsilon=0.1), 4000, 250) data = Counter(results[2]) - picks = data.most_common(3) - assert self.percentage_picked(picks, "green") > 0.75 + winner, _ = data.most_common(1).pop() + assert winner is "green" class SoftmaxTest(MonteCarloTest): @@ -77,8 +72,8 @@ class SoftmaxTest(MonteCarloTest): def test_bandit(self): results = self.run_algo(make_bandit("SoftmaxBandit", tau=0.1), 3, 10000) data = Counter(results[2]) - picks = data.most_common(3) - assert self.percentage_picked(picks, "blue") > 0.5 + winner, _ = data.most_common(1).pop() + assert winner is "blue" class AnnealingSoftmaxTest(MonteCarloTest): @@ -87,8 +82,8 @@ class AnnealingSoftmaxTest(MonteCarloTest): def test_bandit(self): results = self.run_algo(make_bandit("AnnealingSoftmaxBandit"), 3, 10000) data = Counter(results[2]) - picks = data.most_common(3) - assert self.percentage_picked(picks, "blue") > 0.4 + winner, _ = data.most_common(1).pop() + assert winner is "blue" class ThompsonBanditTest(MonteCarloTest): @@ -97,5 +92,5 @@ class ThompsonBanditTest(MonteCarloTest): def test_bandit(self): results = self.run_algo(make_bandit("ThompsonBandit"), 10, 15000) data = Counter(results[2]) - picks = data.most_common(3) - assert self.percentage_picked(picks, "blue") > 0.7 + winner, _ = data.most_common(1).pop() + assert winner is "blue" diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 7e39478..c6d4999 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -70,7 +70,7 @@ def test_improper_configuration(self): rv = app_client.get("/") with self.assertRaises(MABConfigException): - app_client.set_cookie("localhost", "MAB", '{"some_bandit": "blue"}') + app_client.set_cookie("MAB", '{"some_bandit": "blue"}', domain="localhost") app_client.get("/reward") def get_arm(self, headers): diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..a8af049 --- /dev/null +++ b/tox.ini @@ -0,0 +1,17 @@ +[tox] +min_version = 4.0 +env_list = + py38 + py39 + py310 + lint + +[testenv] +deps = + .[dev] +commands = pytest tests + +[testenv:lint] +deps = + .[dev] +commands = black --check flask_mab tests