You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
11 KiB
274 lines
11 KiB
#!/bin/python3 |
|
# coding: utf-8 |
|
|
|
import argparse |
|
import sys |
|
|
|
class UnsupportedPythonVersion(Exception): |
|
pass |
|
|
|
if sys.version_info[0] < 3: |
|
raise UnsupportedPythonVersion('Python 3 or newer is required') |
|
|
|
# list of temp unit notations to check user input against |
|
celsius_notations = ['celsius','grad celsius','c','°c','° c','celcius'] |
|
kelvin_notations = ['kelvin','grad kelvin','k','°k','° k'] |
|
fahrenheit_notations = ['fahrenheit','grad fahrenheit','f','°f','° f'] |
|
rankine_notations = ['rankine','grad rankine','r','ra','°r','°ra','° r','° ra'] |
|
|
|
# id for temp units |
|
id_temp_celsius = 'c' |
|
id_temp_kelvin = 'k' |
|
id_temp_fahrenheit = 'f' |
|
id_temp_rankine = 'r' |
|
|
|
# absolute zero values |
|
temp_celsius_absolute_zero = -273.15 |
|
temp_kelvin_absolute_zero = 0 |
|
temp_fahrenheit_absolute_zero = -459.670 |
|
temp_rankine_absolute_zero = 0 |
|
|
|
# strings for input requests |
|
user_prompt_temp_unit_input = 'Welche Temperatur-Einheit soll umgerechnet werden?: ' |
|
user_prompt_temp_unit_output = 'In welche Temperatur-Einheit soll umgerechnet werden?: ' |
|
user_prompt_temp_input_first = "Gebe die Temperatur in " |
|
user_prompt_temp_input_last = " ein: " |
|
|
|
# strings for program output |
|
user_output_celsius = ' Grad Celsius' |
|
user_output_kelvin = ' Grad Kelvin' |
|
user_output_fahrenheit = ' Grad Fahrenheit' |
|
user_output_rankine = ' Grad Rankine' |
|
temp_output_unit_input = None |
|
temp_output_unit_output = None |
|
user_output_temp_output = "Das sind " |
|
user_output_dumb_convert = 'Das kannst du selber ;-)' |
|
user_output_invalid_temp_value = 'Die eingebene Temperatur ist kälter als der absolute Nullpunkt. Das ist unmöglich. Gib eine zulässige Temperatur ein.' |
|
welcome_message = \ |
|
''' |
|
--- python temperature converter --- |
|
Konvertiert jede Temperatur von einer Einheit in eine beliebige andere Einheit. |
|
Verfügbare Einheiten: |
|
- Celsius |
|
- Kelvin |
|
- Fahrenheit |
|
- Rankine |
|
''' |
|
|
|
# strings for argparser |
|
parser_description = 'Python Temperatur-Koverter. Konvertiert zwischen Temperaturen in Celsius, Kelvin, Fahrenheit und Rankine.' |
|
parser_epilog = '* - Pflicht-Argumente im nicht-interaktiven Modus' |
|
arg_mode_interactive = ['interaktiv'] |
|
arg_mode_non_interactive = ['nicht-interaktiv'] |
|
arg_mode_help = 'Modus: interaktiv oder nicht-interaktiv' |
|
arg_input_unit_help = 'Die umzuwandelnde Einheit *' |
|
arg_output_unit_help = 'Die gewünschte Einheit *' |
|
arg_temp_value_help = 'Der Temperatur-Wert zum Umwandeln *' |
|
|
|
# prompt user for tempeature unit |
|
# result id_temp_* for appropriate unit |
|
# usage: |
|
# possible paramter strings: 'input' or 'output' |
|
# 'input' for the unit from which the user wants to convert |
|
# 'output' for the unit to which the user wants to convert |
|
def get_temp_unit(for_temp): |
|
while True: |
|
if for_temp == 'input': |
|
if args.mode in arg_mode_interactive: |
|
temp_unit = input(user_prompt_temp_unit_input) |
|
elif args.mode in arg_mode_non_interactive: |
|
temp_unit = args.input_unit |
|
elif for_temp == 'output': |
|
if args.mode in arg_mode_interactive: |
|
temp_unit = input(user_prompt_temp_unit_output) |
|
elif args.mode in arg_mode_non_interactive: |
|
temp_unit = args.output_unit |
|
else: |
|
raise TypeError('get_temp_unit needs either \'input\' or \'output\' as parameter') |
|
|
|
temp_unit = temp_unit.lower() |
|
|
|
for notation in celsius_notations: |
|
if(temp_unit == notation): |
|
return id_temp_celsius |
|
for notation in kelvin_notations: |
|
if(temp_unit == notation): |
|
return id_temp_kelvin |
|
for notation in fahrenheit_notations: |
|
if(temp_unit == notation): |
|
return id_temp_fahrenheit |
|
for notation in rankine_notations: |
|
if(temp_unit == notation): |
|
return id_temp_rankine |
|
if args.mode in arg_mode_interactive: |
|
print("Fehler: Keine gültige Einheit") |
|
elif args.mode in arg_mode_non_interactive: |
|
raise ValueError('Keine gültige Einheit') |
|
|
|
# prompt user for temperature |
|
# result approprate temperature |
|
# usage: |
|
# possible parameter strings: all of id_temp_* |
|
# may be used with get_temp_unit() |
|
def get_temp(temp_unit): |
|
if temp_unit == id_temp_celsius: |
|
temp_output_unit_input = user_output_celsius |
|
elif temp_unit == id_temp_kelvin: |
|
temp_output_unit_input = user_output_kelvin |
|
elif temp_unit == id_temp_fahrenheit: |
|
temp_output_unit_input = user_output_fahrenheit |
|
elif temp_unit == id_temp_rankine: |
|
temp_output_unit_input = user_output_rankine |
|
while True: |
|
temp = input(user_prompt_temp_input_first + temp_output_unit_input + user_prompt_temp_input_last) |
|
try: |
|
temp = float(temp) |
|
return temp |
|
except ValueError: |
|
print("Fehler: Du musst eine Temperatur angeben!") |
|
|
|
def is_valid_temp(temp_value,temp_input_unit): |
|
if temp_input_unit == id_temp_celsius and temp_value > temp_celsius_absolute_zero: |
|
return True |
|
elif temp_input_unit == id_temp_kelvin and temp_value > temp_kelvin_absolute_zero: |
|
return True |
|
elif temp_input_unit == id_temp_fahrenheit and temp_value > temp_fahrenheit_absolute_zero: |
|
return True |
|
elif temp_input_unit == id_temp_rankine and temp_value > temp_fahrenheit_absolute_zero: |
|
return True |
|
return False |
|
|
|
# convert temperatures from one unit to another |
|
# check wikipedia for the math |
|
def temp_celsius_to_kelvin(temp_celsius): |
|
temp_kelvin = temp_celsius + 273.15 |
|
return temp_kelvin |
|
|
|
def temp_celsius_to_fahrenheit(temp_celsius): |
|
temp_fahrenheit = temp_celsius * 9/5 + 32 |
|
return temp_fahrenheit |
|
|
|
def temp_celsius_to_rankine(temp_celsius): |
|
temp_rankine = temp_celsius * 1.8 + 491.67 |
|
return temp_rankine |
|
|
|
def temp_kelvin_to_celsius(temp_kelvin): |
|
temp_celsius = temp_kelvin- 273.15 |
|
return temp_celsius |
|
|
|
def temp_kelvin_to_fahrenheit(temp_kelvin): |
|
temp_fahrenheit = temp_kelvin * 9/5 - 459.67 |
|
return temp_fahrenheit |
|
|
|
def temp_kelvin_to_rankine(temp_kelvin): |
|
temp_rankine = temp_kelvin * 9/5 |
|
return temp_rankine |
|
|
|
def temp_fahrenheit_to_celsius(temp_fahrenheit): |
|
temp_celsius = (temp_fahrenheit - 32) * 5/9 |
|
return temp_celsius |
|
|
|
def temp_fahrenheit_to_kelvin(temp_fahrenheit): |
|
temp_kelvin = (temp_fahrenheit + 459.67) * 5/9 |
|
return temp_kelvin |
|
|
|
def temp_fahrenheit_to_rankine(temp_fahrenheit): |
|
temp_rankine = temp_fahrenheit + 459.67 |
|
return temp_rankine |
|
|
|
def temp_rankine_to_celsius(temp_rankine): |
|
temp_celsius = temp_rankine * 5/9 - 273.15 |
|
return temp_celsius |
|
|
|
def temp_rankine_to_kelvin(temp_rankine): |
|
temp_kelvin = temp_rankine * 5/9 |
|
return temp_kelvin |
|
|
|
def temp_rankine_to_fahrenheit(temp_rankine): |
|
temp_fahrenheit = temp_rankine - 459.67 |
|
return temp_fahrenheit |
|
|
|
def convert_temp(temp_input_unit,temp_output_unit,temp_value): |
|
# call appropriate function to convert temperature and print the result |
|
if temp_input_unit == id_temp_celsius: |
|
if temp_output_unit == id_temp_kelvin: |
|
temp_kelvin = temp_celsius_to_kelvin(temp_value) |
|
print(user_output_temp_output + str(temp_kelvin) + user_output_kelvin) |
|
elif temp_output_unit == id_temp_fahrenheit: |
|
temp_fahrenheit = temp_celsius_to_fahrenheit(temp_value) |
|
print(user_output_temp_output + str(temp_fahrenheit) + user_output_fahrenheit) |
|
elif temp_output_unit == id_temp_rankine: |
|
temp_rankine = temp_celsius_to_rankine(temp_value) |
|
print(user_output_temp_output + str(temp_rankine) + user_output_rankine) |
|
else: |
|
print(user_output_dumb_convert) |
|
elif temp_input_unit == id_temp_kelvin: |
|
if temp_output_unit == id_temp_celsius: |
|
temp_celsius = temp_kelvin_to_celsius(temp_value) |
|
print(user_output_temp_output + str(temp_celsius) + user_output_celsius) |
|
elif temp_output_unit == id_temp_fahrenheit: |
|
temp_fahrenheit = temp_kelvin_to_fahrenheit(temp_value) |
|
print(user_output_temp_output + str(temp_fahrenheit) + user_output_fahrenheit) |
|
elif temp_output_unit == id_temp_rankine: |
|
temp_rankine = temp_kelvin_to_rankine(temp_value) |
|
print(user_output_temp_output + str(temp_rankine) + user_output_rankine) |
|
else: |
|
print(user_output_dumb_convert) |
|
elif temp_input_unit == id_temp_fahrenheit: |
|
if temp_output_unit == id_temp_celsius: |
|
temp_celsius = temp_fahrenheit_to_celsius(temp_value) |
|
print(user_output_temp_output + str(temp_celsius) + user_output_celsius) |
|
elif temp_output_unit == id_temp_kelvin: |
|
temp_kelvin = temp_fahrenheit_to_kelvin(temp_value) |
|
print(user_output_temp_output + str(temp_kelvin) + user_output_kelvin) |
|
elif temp_output_unit == id_temp_rankine: |
|
temp_rankine = temp_fahrenheit_to_rankine(temp_value) |
|
print(user_output_temp_output + str(temp_rankine) + user_output_rankine) |
|
else: |
|
print(user_output_dumb_convert) |
|
elif temp_input_unit == id_temp_rankine: |
|
if temp_output_unit == id_temp_celsius: |
|
temp_celsius = temp_rankine_to_celsius(temp_value) |
|
print(user_output_temp_output + str(temp_celsius) + user_output_celsius) |
|
elif temp_output_unit == id_temp_kelvin: |
|
temp_kelvin = temp_rankine_to_kelvin(temp_value) |
|
print(user_output_temp_output + str(temp_kelvin) + user_output_kelvin) |
|
elif temp_output_unit == id_temp_fahrenheit: |
|
temp_fahrenheit = temp_rankine_to_fahrenheit(temp_value) |
|
print(user_output_temp_output + str(temp_fahrenheit) + user_output_fahrenheit) |
|
else: |
|
print(user_output_dumb_convert) |
|
|
|
# the magic begins |
|
if __name__ == "__main__": |
|
parser = argparse.ArgumentParser(description=parser_description,epilog=parser_epilog) |
|
parser.add_argument('mode',type=str,help=arg_mode_help) |
|
parser.add_argument('-i','--input_unit',type=str,help=arg_input_unit_help) |
|
parser.add_argument('-o','--output_unit',type=str,help=arg_output_unit_help) |
|
parser.add_argument('-t','--temp_value',type=float,help=arg_temp_value_help) |
|
args = parser.parse_args() |
|
|
|
if args.mode in arg_mode_interactive: |
|
# introduce user to script usage |
|
print(welcome_message) |
|
# request user to provide input |
|
temp_input_unit = get_temp_unit('input') |
|
temp_output_unit = get_temp_unit('output') |
|
temp_value = get_temp(temp_input_unit) |
|
if is_valid_temp(temp_value,temp_input_unit): |
|
convert_temp(temp_input_unit,temp_output_unit,temp_value) |
|
else: |
|
print(user_output_invalid_temp_value) |
|
elif args.mode in arg_mode_non_interactive: |
|
if args.input_unit != None and args.output_unit != None and args.temp_value != None: |
|
temp_input_unit = get_temp_unit('input') |
|
temp_output_unit = get_temp_unit('output') |
|
temp_value = args.temp_value |
|
if is_valid_temp(temp_value,temp_input_unit): |
|
convert_temp(temp_input_unit,temp_output_unit,temp_value) |
|
else: |
|
print(user_output_invalid_temp_value) |
|
else: |
|
print('Please supply all needed arguments') |
|
else: |
|
print('Invalid value')
|
|
|