Pull file operations out to a files.py

This commit is contained in:
Marc Di Luzio 2024-08-10 10:58:31 +01:00
parent 96fb77f71f
commit ed2375386b
6 changed files with 27 additions and 20 deletions

View file

@ -1,6 +1,6 @@
"""Very simple config loading library""" """Very simple config loading library"""
from schema import Schema, And, Use from schema import Schema, And, Use
import matching import files
FILE = "config.json" FILE = "config.json"
@ -24,7 +24,7 @@ class Config():
def load() -> Config: def load() -> Config:
"""Load the config and validate it""" """Load the config and validate it"""
config = matching.load(FILE) config = files.load(FILE)
Schema( Schema(
{ {
# Discord bot token # Discord bot token

14
files.py Normal file
View file

@ -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)

View file

@ -3,7 +3,7 @@ import os
import time import time
from schema import Schema, And, Use, Optional from schema import Schema, And, Use, Optional
from typing import Protocol from typing import Protocol
import matching import files
FILE = "history.json" FILE = "history.json"
@ -28,7 +28,7 @@ class History():
def save(self) -> None: def save(self) -> None:
"""Save out the history""" """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: def save_groups_to_history(self, groups: list[list[Member]]) -> None:
"""Save out the groups to the history file""" """Save out the groups to the history file"""
@ -51,7 +51,7 @@ class History():
def load() -> History: def load() -> History:
"""Load the history and validate it""" """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": [], "groups": [],
"matchees": {} "matchees": {}
} }

View file

@ -1,23 +1,16 @@
"""Utility functions for matchy""" """Utility functions for matchy"""
import json
import random import random
from typing import Protocol from typing import Protocol
def load(file: str) -> dict: class Member(Protocol):
"""Load a json file directly as a dict""" @property
with open(file) as f: def id(self) -> int:
return json.load(f) pass
def save(file: str, content: dict): def members_to_groups(matchees: list[Member],
"""Save out a content dictionary to a file""" per_group: int) -> list[list[Member]]:
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]]:
"""Generate the groups from the set of matchees""" """Generate the groups from the set of matchees"""
random.shuffle(matchees) random.shuffle(matchees)
num_groups = max(len(matchees)//per_group, 1) num_groups = max(len(matchees)//per_group, 1)

View file

@ -14,7 +14,7 @@ import matching
]) ])
def test_matchees_to_groups(matchees, per_group): def test_matchees_to_groups(matchees, per_group):
"""Test simple group matching works""" """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: for group in groups:
# Ensure the group contains the right number of members # Ensure the group contains the right number of members
assert len(group) >= per_group assert len(group) >= per_group

View file

@ -91,7 +91,7 @@ async def match(interaction: discord.Interaction, group_min: int = None, matchee
# Create our groups! # Create our groups!
matchees = list( matchees = list(
m for m in interaction.channel.members if matchee in m.roles) 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 # 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) msg = '\n'.join(matching.group_to_message(g) for g in groups)