From 09d0508d7ae0501ea4650caa7a8a1a9298f464ad Mon Sep 17 00:00:00 2001 From: Nikita Reznikov Date: Wed, 26 Jun 2024 22:08:25 +0300 Subject: [PATCH] Add jinja module py.py, add shuffle function in rand module --- .../event/jinja_modules/convert.py | 6 ----- eventum_plugins/event/jinja_modules/py.py | 7 +++++ eventum_plugins/event/jinja_modules/rand.py | 27 ++++++++++++++----- pyproject.toml | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) delete mode 100644 eventum_plugins/event/jinja_modules/convert.py create mode 100644 eventum_plugins/event/jinja_modules/py.py diff --git a/eventum_plugins/event/jinja_modules/convert.py b/eventum_plugins/event/jinja_modules/convert.py deleted file mode 100644 index 6840622..0000000 --- a/eventum_plugins/event/jinja_modules/convert.py +++ /dev/null @@ -1,6 +0,0 @@ -import datetime as dt - - -def to_datetime(timestamp: str) -> dt.datetime: - """Return datetime object parsed from ISO8601 timestamp string.""" - return dt.datetime.fromisoformat(timestamp) diff --git a/eventum_plugins/event/jinja_modules/py.py b/eventum_plugins/event/jinja_modules/py.py new file mode 100644 index 0000000..2c024a5 --- /dev/null +++ b/eventum_plugins/event/jinja_modules/py.py @@ -0,0 +1,7 @@ +from datetime import datetime, time, timedelta + +__all__ = ( + 'datetime', + 'time', + 'timedelta', +) diff --git a/eventum_plugins/event/jinja_modules/rand.py b/eventum_plugins/event/jinja_modules/rand.py index 29c9266..df45a31 100644 --- a/eventum_plugins/event/jinja_modules/rand.py +++ b/eventum_plugins/event/jinja_modules/rand.py @@ -4,20 +4,33 @@ import uuid from string import (ascii_letters, ascii_lowercase, ascii_uppercase, digits, punctuation) -from typing import Any, Sequence +from typing import Sequence, TypeVar +T = TypeVar('T') -def choice(items: Sequence) -> Any: + +def shuffle(items: Sequence[T]) -> list[T] | str: + """Shuffle sequence elements.""" + seq = list(items) + random.shuffle(seq) + + if isinstance(items, str): + return ''.join(seq) # type: ignore[arg-type] + else: + return seq + + +def choice(items: Sequence[T]) -> T: """Return random item from non empty sequence.""" return random.choice(items) -def choices(items: Sequence, n: int) -> list: +def choices(items: Sequence[T], n: int) -> list[T]: """Return `n` random items from non empty sequence.""" return random.choices(items, k=n) -def weighted_choice(items: Sequence, weights: Sequence[float]) -> Any: +def weighted_choice(items: Sequence[T], weights: Sequence[float]) -> T: """Return random item from non empty sequence with `weights` probability. """ @@ -25,10 +38,10 @@ def weighted_choice(items: Sequence, weights: Sequence[float]) -> Any: def weighted_choices( - items: Sequence, + items: Sequence[T], weights: Sequence[float], n: int -) -> list: +) -> list[T]: """Return `n` random items from non empty sequence with `weights` probability. """ @@ -195,7 +208,7 @@ def sha256() -> str: class datetime: @staticmethod def timestamp(start: str, end: str) -> str: - """Return random timestamp im range [start; end].""" + """Return random timestamp in range [start; end].""" start_date = dt.datetime.fromisoformat(start) end_date = dt.datetime.fromisoformat(end) diff --git a/pyproject.toml b/pyproject.toml index b284441..e236116 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "eventum-plugins" -version = "1.0.9" +version = "1.0.10" description = "Plugins for Eventum" license = "Apache-2.0" authors = ["Nikita Reznikov "]