Skip to content

Commit

Permalink
implement history manager allow selection_gap = 0 or 2 or any other n…
Browse files Browse the repository at this point in the history
…umber (default selection gap = 0)
  • Loading branch information
mhewedy committed May 21, 2024
1 parent b470f11 commit 4d034ad
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async def clear_command(update: Update, context: ContextTypes.DEFAULT_TYPE):

global orders
orders = {}
if context.args == "+user_selection_history":
if context.args == "+selection_history":
userSelector.clear_history()
await update.message.reply_text("تم مسح جميع الطلبات بنجاح")

Expand Down
46 changes: 39 additions & 7 deletions test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_select_single_user(self):
selected = selector.select(users)
self.assertEqual(selected, "Alice")

def test_select_non_excluded_from_2_users(self):
selector = UserSelector()
def test_select_non_excluded_from_2_users_with_2_selection_gap(self):
selector = UserSelector(selection_gap=2)
users = ["Alice", "Bob", "Bob", "Bob", "Bob"]
selector.history_manager.history = ["Alice", "Bob", "Alice"]

Expand All @@ -41,8 +41,8 @@ def test_select_non_excluded_from_2_users(self):
selected = selector.select(users)
self.assertEqual(selected, expected_selection)

def test_select_non_excluded_3_users(self):
selector = UserSelector()
def test_select_non_excluded_3_users_with_2_selection_gap(self):
selector = UserSelector(selection_gap=2)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]

Expand All @@ -51,8 +51,40 @@ def test_select_non_excluded_3_users(self):
selected = selector.select(users)
self.assertEqual(selected, expected_selection)

def test_select_non_excluded_from_2_users_with_0_selection_gap(self):
selector = UserSelector(selection_gap=0)
users = ["Alice", "Bob", "Bob", "Bob", "Bob"]
selector.history_manager.history = ["Alice", "Bob", "Alice"]

selected = selector.select(users)
self.assertIn(selected, ["Alice", "Bob"])

def test_select_non_excluded_3_users_with_0_selection_gap(self):
count = 0
for _ in range(0, 100):
selector = UserSelector(selection_gap=0)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
selected = selector.select(users)
if selected in ["Bob", "Charlie"]:
count += 1

self.assertGreater(count, 0)

def test_select_non_excluded_3_users_with_0_selection_gap_2(self):
count = 0
for _ in range(0, 100):
selector = UserSelector(selection_gap=2)
users = ["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie"]
selector.history_manager.history = ["Alice", "Bob", "Charlie"]
selected = selector.select(users)
if selected in ["Bob", "Charlie"]:
count += 1

self.assertEqual(count, 0)

def test_select_non_excluded_3_users_with_ids(self):
selector = UserSelector()
selector = UserSelector(selection_gap=2)
users = [(1, "Alice"), (1, "Alice"), (2, "Bob"), (2, "Bob"), (3, "Charlie"), (3, "Charlie"), (3, "Charlie")]
selector.history_manager.history = [(1, "Alice"), (2, "Bob"), (3, "Charlie")]

Expand All @@ -63,15 +95,15 @@ def test_select_non_excluded_3_users_with_ids(self):

def test_select_never_picks_last_n_from_history(self):
for _ in range(100):
selector = UserSelector()
selector = UserSelector(selection_gap=2)
users = \
["Alice", "Alice", "Bob", "Bob", "Charlie", "Charlie", "Charlie", "Diana", "Diana", "David", "David"]
selector.history_manager.history = ["Alice", "Bob", "Charlie", "Diana"]
selected = selector.select(users)
self.assertIn(selected, ["Alice", "Bob", "David"])

def test_history_maintenance(self):
selector = UserSelector()
selector = UserSelector(selection_gap=2)
users = ["Alice", "Bob", "Charlie", "Diana"]
selections = []
for _ in range(100):
Expand Down
25 changes: 13 additions & 12 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import random
import time
from abc import abstractmethod, ABC
from typing import Any
from typing import Callable

Expand Down Expand Up @@ -37,20 +38,21 @@ async def is_admin(update: Update, context: ContextTypes.DEFAULT_TYPE) -> bool:
return False


class HistoryManager:
class HistoryManager(ABC):
def __init__(self):
self.history = []

@abstractmethod
def load_history(self):
pass

def save_history(self):
pass
def save_history(self, h):
self.history = h


class InMemoryHistoryManager(HistoryManager):
def __init__(self):
super().__init__()
def load_history(self):
pass


class FileSystemHistoryManager(HistoryManager):
Expand All @@ -71,16 +73,16 @@ def load_history(self):
self.history = json.load(file)
else:
self.history = []
self.save_history()
self.save_history(self.history)

def save_history(self):
def save_history(self, h):
with open(self.file_path, 'w') as file:
json.dump(self.history, file)


class UserSelector:
def __init__(self, history_manager=InMemoryHistoryManager()):
self.selection_gap = 2
def __init__(self, selection_gap=0, history_manager=InMemoryHistoryManager()):
self.selection_gap = selection_gap
self.history_manager = history_manager

def select(self, users):
Expand All @@ -99,12 +101,11 @@ def select(self, users):
return self.select(users)
else:
logging.info(f'users: {users}, selected user is: {selected}')
self.history_manager.history = (self.history_manager.history + [selected])[-2:]
self.history_manager.save_history()
self.history_manager.save_history((self.history_manager.history + [selected])[-2:])
return selected

def clear_history(self):
self.history_manager.history = []
self.history_manager.save_history([])


def get_congrats_msg():
Expand Down

0 comments on commit 4d034ad

Please sign in to comment.