From 12ed54f925525849a8958d5e3da04d81c75a0b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 14 Oct 2022 23:03:36 +0000 Subject: [PATCH] set_length own function --- api/advent22_api/routers/_misc.py | 13 +++++++------ api/advent22_api/routers/days.py | 15 ++++++++++----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/api/advent22_api/routers/_misc.py b/api/advent22_api/routers/_misc.py index 3700248..440ec28 100644 --- a/api/advent22_api/routers/_misc.py +++ b/api/advent22_api/routers/_misc.py @@ -10,11 +10,12 @@ async def get_loesungswort() -> str: return fp.read().decode("utf8").strip() -async def shuffle(seq: Sequence, length: int | None = None) -> list: + +async def set_length(seq: Sequence, length: int) -> list: + infinite = itertools.cycle(seq) + return list(itertools.islice(infinite, length)) + + +async def shuffle(seq: Sequence) -> list: rnd = random.Random(await get_loesungswort()) - - if length is not None: - infinity = itertools.cycle(seq) - seq = list(itertools.islice(infinity, length)) - return rnd.sample(seq, len(seq)) diff --git a/api/advent22_api/routers/days.py b/api/advent22_api/routers/days.py index 5e85d96..b48c9c8 100644 --- a/api/advent22_api/routers/days.py +++ b/api/advent22_api/routers/days.py @@ -10,7 +10,7 @@ from PIL import ImageDraw, ImageFont from ..dav_common import dav_get_file, dav_list_files from ._image import AdventImage -from ._misc import shuffle +from ._misc import set_length, shuffle router = APIRouter(prefix="/days", tags=["days"]) @@ -61,8 +61,16 @@ _RE_IMAGE_FILE = re.compile( ) +async def get_images() -> list[str]: + ls = await dav_list_files(_RE_IMAGE_FILE, "/images") + ls = await set_length(ls, 24) + + return await shuffle(ls) + + async def load_image( index: int, + images: list[str] = Depends(get_images), ) -> AdventImage: """ Bild laden und einen quadratischen Ausschnitt @@ -70,10 +78,7 @@ async def load_image( """ # Bild laden - rnd = random.Random(f"{loesungswort}{index}") - dat = rnd.choice(await dav_list_files(_RE_IMAGE_FILE, "/images")) - - img_buffer = await dav_get_file(dat) + img_buffer = await dav_get_file(images[index]) return await AdventImage.load_standard(img_buffer)