set_length own function
This commit is contained in:
parent
f0b314fd7c
commit
12ed54f925
2 changed files with 17 additions and 11 deletions
|
@ -10,11 +10,12 @@ async def get_loesungswort() -> str:
|
||||||
return fp.read().decode("utf8").strip()
|
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())
|
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))
|
return rnd.sample(seq, len(seq))
|
||||||
|
|
|
@ -10,7 +10,7 @@ from PIL import ImageDraw, ImageFont
|
||||||
|
|
||||||
from ..dav_common import dav_get_file, dav_list_files
|
from ..dav_common import dav_get_file, dav_list_files
|
||||||
from ._image import AdventImage
|
from ._image import AdventImage
|
||||||
from ._misc import shuffle
|
from ._misc import set_length, shuffle
|
||||||
|
|
||||||
router = APIRouter(prefix="/days", tags=["days"])
|
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(
|
async def load_image(
|
||||||
index: int,
|
index: int,
|
||||||
|
images: list[str] = Depends(get_images),
|
||||||
) -> AdventImage:
|
) -> AdventImage:
|
||||||
"""
|
"""
|
||||||
Bild laden und einen quadratischen Ausschnitt
|
Bild laden und einen quadratischen Ausschnitt
|
||||||
|
@ -70,10 +78,7 @@ async def load_image(
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Bild laden
|
# Bild laden
|
||||||
rnd = random.Random(f"{loesungswort}{index}")
|
img_buffer = await dav_get_file(images[index])
|
||||||
dat = rnd.choice(await dav_list_files(_RE_IMAGE_FILE, "/images"))
|
|
||||||
|
|
||||||
img_buffer = await dav_get_file(dat)
|
|
||||||
return await AdventImage.load_standard(img_buffer)
|
return await AdventImage.load_standard(img_buffer)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue