Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new Jinja helper functions flip_coin and pick_multiple #134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Jaid
Copy link

@Jaid Jaid commented Aug 11, 2024

I added two new helper functions for Jinja2 templates.

pick_multiple

Returns multiple values from an input list (a resolved wildcard for example). The number of items can be specified, otherwise it’s rolled between 1 and the maximum. If the specified amount is too high, it’s autocorrected instead of throwing an error.

Example

Between 1 and maximum

{% set separator = ' and ' %}
{% set characters = ['ash', 'misty', 'brock', 'professor_oak', 'gary'] %}
a hike with {{ pick_multiple(characters)|join(separator) }} in the mountains

Between 0 and maximum

{% set separator = ' and ' %}
a hike
{% set characters = ['ash', 'misty', 'brock', 'professor_oak', 'gary'] %}
{% set charactersAmount = randint(0, characters|length) %}
{% if charactersAmount > 0 %}
  with {{ pick_multiple(characters, charactersAmount)|join(separator) }}
{% endif %}
in the mountains

flip_coin

If no arguments are given, returns a random boolean. Otherwise randomly picks an argument.

Example

For branches

{% if flip_coin() %}
  1girl,
{% endif %}
laying on grass

For quick inline decisions

a {{ flip_coin('caught', 'fleeing') }} Pikachu

Copy link
Collaborator

@akx akx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add docs and/or tests for these?

Comment on lines +29 to +36
def pick_multiple(items: Iterable[Any], returnLength: int | True = True) -> list[Any]:
if returnLength is True:
returnLength = random.randint(1, len(items))
if returnLength == 0:
return []
if returnLength > len(items):
return list(items)
return random.sample(list(items), returnLength)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def pick_multiple(items: Iterable[Any], returnLength: int | True = True) -> list[Any]:
if returnLength is True:
returnLength = random.randint(1, len(items))
if returnLength == 0:
return []
if returnLength > len(items):
return list(items)
return random.sample(list(items), returnLength)
def pick_multiple(items: Iterable[Any], return_length: int | True = True) -> list[Any]:
if return_length is True:
return_length = random.randint(1, len(items))
if return_length == 0:
return []
if return_length > len(items):
return list(items)
return random.sample(list(items), return_length)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants