diff --git a/common/__init__.py b/common/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/common/conversion/__init__.py b/common/conversion/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/common/conversion/removeEmptyDecimals.py b/common/conversion/removeEmptyDecimals.py new file mode 100644 index 0000000..80e62ec --- /dev/null +++ b/common/conversion/removeEmptyDecimals.py @@ -0,0 +1,15 @@ +def removeEmptyDecimals(n): + """ + Removes .0 from a float if it doesn't add any extra value to the number. + + Parameters: + n (float): the value to parse + + ``` + >>> removeEmptyDecimals(2.0) + 2 + >>> removeEmptyDecimals(2.5) + 2.5 + ``` + """ + return int(n) if int(n) == n else n \ No newline at end of file diff --git a/common/inputChecking/__init__.py b/common/inputChecking/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/common/inputChecking/boolInput.py b/common/inputChecking/boolInput.py new file mode 100644 index 0000000..eddfd61 --- /dev/null +++ b/common/inputChecking/boolInput.py @@ -0,0 +1,18 @@ +def boolInput(question, error='Skriv in J eller N\n', yesNoLetters=('j', 'n')): + """ + Asks the user a yes/no question and returns a bool based on the input. + + Parameters: \\ + prompt (str): The prompt asking the user for input \\ + error? (str): The message to be printed on parsing error \\ + yesNoLetters? ((str,str)): The letters to be used for representing yes and no in low caps + """ + yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()] + noLetters = [yesNoLetters[1], yesNoLetters[1].capitalize()] + while True: + try: + choice = input(question) + assert choice in yesLetters + noLetters + return choice in yesLetters + except AssertionError: + print(error) \ No newline at end of file diff --git a/common/inputChecking/choiceInput.py b/common/inputChecking/choiceInput.py new file mode 100644 index 0000000..8e78d1b --- /dev/null +++ b/common/inputChecking/choiceInput.py @@ -0,0 +1,16 @@ +def safeQuestion(prompt, choices): + """ + Prompts the user to make a choice and asserts that the choice is valid. + + Parameters: \\ + prompt (str): The prompt asking the user for input \\ + choices ([str]): The choices that the user can choose (in low caps) + """ + allChoices = choices + [choice.capitalize() for choice in choices] + while True: + try: + answer = input(prompt) + assert answer in allChoices + return answer.lower() + except AssertionError: + print('Skriv inn enten', ', '.join(choices[:-1]), 'eller', choices[-1]) \ No newline at end of file diff --git a/common/inputChecking/typeCheck.py b/common/inputChecking/typeCheck.py new file mode 100644 index 0000000..534e9dd --- /dev/null +++ b/common/inputChecking/typeCheck.py @@ -0,0 +1,19 @@ +def inputTypeCheck( + prompt, + type, + error="Kunne ikke tolke input. Har du skrevet det inn riktig?" +): + """ + Typechecks an input, and only returns the input when it could be successfully parsed. + + Parameters: \\ + prompt (str): The prompt asking the user for input \\ + type (fun): The function to be used for parsing \\ + error? (str): The message to be printed on parsing error + """ + while True: + inputValue = input(prompt) + try: + return type(inputValue) + except ValueError: + print(error) \ No newline at end of file diff --git a/common/test/__init__.py b/common/test/__init__.py new file mode 100644 index 0000000..e69de29