Select Git revision
template_list.js
common_functions.py 1.68 KiB
import datetime
import re
from typing import List, Optional, TypeVar
ChoiceType = TypeVar("ChoiceType")
def select_from_list(choices: List[ChoiceType], choice_name: str, none_is_option: bool = False) -> ChoiceType:
query_user = True
return_value: ChoiceType = None
while query_user:
print(f'Choose the {choice_name} from this list:')
number_of_choices = len(choices)
for i in range(number_of_choices):
print(f'{i + 1})\t{choices[i]}'.expandtabs(4))
if none_is_option:
print('0)\tNone of the above'.expandtabs(4))
selection = input('Enter selection: ')
try:
selection_value = int(selection)
if none_is_option and selection_value == 0:
return_value = None
query_user = False
elif selection_value < 1 or selection_value > number_of_choices:
raise ValueError('Selection out of range.')
else:
return_value = choices[selection_value - 1]
query_user = False
except ValueError:
print(f'\tSelection must be an integer between {0 if none_is_option else 1} '
f'and {number_of_choices}, inclusive!')
return return_value
def strip_html(text: Optional[str]) -> Optional[str]:
if text is not None:
return re.sub(re.compile('<.*?>'), '', text)
else:
return None
def semester_stamp() -> str:
today = datetime.date.today()
year: int = today.year
month: int = today.month
# Normalize month to semester start
if month < 5:
month = 1
elif month < 8:
month = 5
else:
month = 8
return f'{year}-0{month}'