From 62bb8076e99792021e6cb990cb2d8cf00028bad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Mon, 16 Sep 2024 23:01:37 +0200 Subject: [PATCH] word_db: add random-pick methods --- pwgen_fr/word_db.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pwgen_fr/word_db.py b/pwgen_fr/word_db.py index 353cbb5..fd4ecac 100644 --- a/pwgen_fr/word_db.py +++ b/pwgen_fr/word_db.py @@ -2,6 +2,7 @@ import gzip import json +import secrets import typing as t from enum import Enum from pathlib import Path @@ -12,17 +13,32 @@ class Genre(Enum): FEM = "féminin" INV = "invariable" # pour les noms uniquement + @classmethod + def pick(cls) -> "Genre": + """random-pick (avoids inv)""" + return secrets.choice([cls.masc, cls.fem]) + class Nombre(Enum): SING = "singulier" PLUR = "pluriel" + @classmethod + def pick(cls) -> "Nombre": + """random-pick""" + return secrets.choice(list(cls)) + class Temps(Enum): PRESENT = "present" FUTUR = "futur" IMPARFAIT = "imparfait" + @classmethod + def pick(cls) -> "Temps": + """random-pick""" + return secrets.choice(list(cls)) + class Nom(t.NamedTuple): """Nom commun""" @@ -38,6 +54,13 @@ class Nom(t.NamedTuple): """Accorde en nombre""" return getattr(self, nombre.name.lower()) + @property + def genre_or_pick(self) -> Genre: + """Genre of the noun, or random-pick if invariable""" + if self.genre == Genre.INV: + return Genre.pick() + return self.genre + @property def serialized(self): return {"genre": self.genre.name, "sing": self.sing, "plur": self.plur}