set_length own function

This commit is contained in:
Jörn-Michael Miehe 2022-10-14 23:03:36 +00:00
parent f0b314fd7c
commit 12ed54f925
2 changed files with 17 additions and 11 deletions

View file

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

View file

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