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"""
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

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
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": {}
}

View file

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

View file

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

View file

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