From ed2375386bcaa8612b67f9bb2545ed96af24fa05 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Sat, 10 Aug 2024 10:58:31 +0100 Subject: [PATCH] Pull file operations out to a files.py --- config.py | 4 ++-- files.py | 14 ++++++++++++++ history.py | 6 +++--- matching.py | 19 ++++++------------- matching_test.py | 2 +- matchy.py | 2 +- 6 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 files.py diff --git a/config.py b/config.py index 2689e9e..66fb52e 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,6 @@ """Very simple config loading library""" from schema import Schema, And, Use -import matching +import files FILE = "config.json" @@ -24,7 +24,7 @@ class Config(): def load() -> Config: """Load the config and validate it""" - config = matching.load(FILE) + config = files.load(FILE) Schema( { # Discord bot token diff --git a/files.py b/files.py new file mode 100644 index 0000000..3a76b6b --- /dev/null +++ b/files.py @@ -0,0 +1,14 @@ +"""File operation helpers""" +import json + + +def load(file: str) -> dict: + """Load a json file directly as a dict""" + with open(file) as f: + return json.load(f) + + +def save(file: str, content: dict): + """Save out a content dictionary to a file""" + with open(file, "w") as f: + json.dump(content, f, indent=4) diff --git a/history.py b/history.py index b697aa6..f0dbfbc 100644 --- a/history.py +++ b/history.py @@ -3,7 +3,7 @@ import os import time from schema import Schema, And, Use, Optional from typing import Protocol -import matching +import files FILE = "history.json" @@ -28,7 +28,7 @@ class History(): def save(self) -> None: """Save out the history""" - matching.save(FILE, self.__dict__) + files.save(FILE, self.__dict__) def save_groups_to_history(self, groups: list[list[Member]]) -> None: """Save out the groups to the history file""" @@ -51,7 +51,7 @@ class History(): def load() -> History: """Load the history and validate it""" - history = matching.load(FILE) if os.path.isfile(FILE) else { + history = files.load(FILE) if os.path.isfile(FILE) else { "groups": [], "matchees": {} } diff --git a/matching.py b/matching.py index c00a726..d203b6c 100644 --- a/matching.py +++ b/matching.py @@ -1,23 +1,16 @@ """Utility functions for matchy""" -import json import random from typing import Protocol -def load(file: str) -> dict: - """Load a json file directly as a dict""" - with open(file) as f: - return json.load(f) +class Member(Protocol): + @property + def id(self) -> int: + pass -def save(file: str, content: dict): - """Save out a content dictionary to a file""" - with open(file, "w") as f: - json.dump(content, f, indent=4) - - -def objects_to_groups(matchees: list[object], - per_group: int) -> list[list[object]]: +def members_to_groups(matchees: list[Member], + per_group: int) -> list[list[Member]]: """Generate the groups from the set of matchees""" random.shuffle(matchees) num_groups = max(len(matchees)//per_group, 1) diff --git a/matching_test.py b/matching_test.py index 634db85..c59a7ec 100644 --- a/matching_test.py +++ b/matching_test.py @@ -14,7 +14,7 @@ import matching ]) def test_matchees_to_groups(matchees, per_group): """Test simple group matching works""" - groups = matching.objects_to_groups(matchees, per_group) + groups = matching.members_to_groups(matchees, per_group) for group in groups: # Ensure the group contains the right number of members assert len(group) >= per_group diff --git a/matchy.py b/matchy.py index 1ef7715..12977e3 100755 --- a/matchy.py +++ b/matchy.py @@ -91,7 +91,7 @@ async def match(interaction: discord.Interaction, group_min: int = None, matchee # Create our groups! matchees = list( m for m in interaction.channel.members if matchee in m.roles) - groups = matching.objects_to_groups(matchees, group_min) + groups = matching.members_to_groups(matchees, group_min) # Post about all the groups with a button to send to the channel msg = '\n'.join(matching.group_to_message(g) for g in groups)