Skip to content

Commit

Permalink
Optimize template picking for chance mode in jinja event plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
rnv812 committed Jul 2, 2024
1 parent c100566 commit 3ca3b8f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions eventum_plugins/event/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down

0 comments on commit 3ca3b8f

Please sign in to comment.