scorebook.utils.jinja_helpers
Jinja2 template helper functions for Scorebook.
number_to_letter
def number_to_letter(index: int, uppercase: bool = True) -> str
Convert a number to a letter (0->A, 1->B, etc.).
Arguments:
index
- The number to convert to a letter (0-based index, must be 0-25)uppercase
- If True, returns uppercase letter; if False, returns lowercase
Returns:
str
- A letter from A-Z (or a-z if uppercase is False)
Raises:
ValueError
- If index is less than 0 or greater than 25
letter_to_number
def letter_to_number(letter: str) -> int
Convert a letter to a number (A->0, B->1, etc.).
Arguments:
letter
- A single letter character (A-Z or a-z)
Returns:
int
- The zero-based position of the letter in the alphabet
Raises:
ValueError
- If the input is not a single letter character
format_list
def format_list(items: List[Any],
separator: str = ", ",
last_separator: str = " and ") -> str
Format a list with proper separators and conjunction.
Examples:
format_list(["a", "b", "c"]) -> "a, b and c" format_list(["a", "b"]) -> "a and b" format_list(["a"]) -> "a"
truncate_text
def truncate_text(text: str, max_length: int, suffix: str = "...") -> str
Truncate text to a maximum length with optional suffix.
format_number
def format_number(number: float, precision: int = 2) -> str
Format a number with specified decimal places.
extract_initials
def extract_initials(text: str) -> str
Extract initials from a text string.
Examples:
extract_initials("John Doe") -> "JD" extract_initials("Machine Learning Model") -> "MLM"
json_pretty
def json_pretty(obj: Any, indent: int = 2) -> str
Pretty-print an object as JSON.
percentage
def percentage(value: float, total: float, precision: int = 1) -> str
Calculate and format a percentage.
Examples:
percentage(25, 100) -> "25.0%" percentage(1, 3, 2) -> "33.33%"
ordinal
def ordinal(n: int) -> str
Convert number to ordinal format like 1st, 2nd, 3rd, etc.
default_jinja_globals
def default_jinja_globals() -> Dict[str, Any]
Get default global functions for Jinja templates.
default_jinja_filters
def default_jinja_filters() -> Dict[str, Any]
Get default filters for Jinja templates.