From 3ca3b8fd8cf30c3978e27a1e775df6b54327395e Mon Sep 17 00:00:00 2001 From: Nikita Reznikov Date: Tue, 2 Jul 2024 22:30:41 +0300 Subject: [PATCH] Optimize template picking for chance mode in jinja event plugin --- eventum_plugins/event/jinja.py | 13 +++++++++---- pyproject.toml | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/eventum_plugins/event/jinja.py b/eventum_plugins/event/jinja.py index 249791e..d4b7e6e 100644 --- a/eventum_plugins/event/jinja.py +++ b/eventum_plugins/event/jinja.py @@ -169,6 +169,7 @@ def __init__( for template in self._templates } self._spinning_template_index = self._get_spinning_template_index() + self._template_chance_weights = self._get_template_chance_weights() def _load_samples( self @@ -258,6 +259,13 @@ def _get_spinning_template_index(self) -> Iterator[int]: for i, _ in enumerate((self._templates)): yield i + def _get_template_chance_weights(self) -> list[float]: + """Get chance weights of templates""" + return [ + list(template_item.values())[0].chance + for template_item in self._config.templates + ] + def _pick_template(self) -> Template | list[Template]: """Pick template(s) depending on picking mode.""" @@ -269,10 +277,7 @@ def _pick_template(self) -> Template | list[Template]: case TemplatePickingMode.CHANCE: return random.choices( population=self._templates, - weights=[ - list(template_item.values())[0].chance - for template_item in self._config.templates - ], + weights=self._template_chance_weights, k=1 )[0] case TemplatePickingMode.SPIN: diff --git a/pyproject.toml b/pyproject.toml index 88953cc..4f0a181 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "eventum-plugins" -version = "1.0.12" +version = "1.0.13" description = "Plugins for Eventum" license = "Apache-2.0" authors = ["Nikita Reznikov "]