Skip to content

Commit

Permalink
Add jinja module py.py, add shuffle function in rand module
Browse files Browse the repository at this point in the history
  • Loading branch information
rnv812 committed Jun 26, 2024
1 parent 90614be commit 09d0508
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
6 changes: 0 additions & 6 deletions eventum_plugins/event/jinja_modules/convert.py

This file was deleted.

7 changes: 7 additions & 0 deletions eventum_plugins/event/jinja_modules/py.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from datetime import datetime, time, timedelta

__all__ = (
'datetime',
'time',
'timedelta',
)
27 changes: 20 additions & 7 deletions eventum_plugins/event/jinja_modules/rand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,44 @@
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.
"""
return random.choices(items, weights=weights, k=1).pop()


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.
"""
Expand Down Expand Up @@ -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)

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.9"
version = "1.0.10"
description = "Plugins for Eventum"
license = "Apache-2.0"
authors = ["Nikita Reznikov <[email protected]>"]
Expand Down

0 comments on commit 09d0508

Please sign in to comment.