rework the decision tree for interactive mode

This commit is contained in:
Christopher Teutsch 2019-05-08 11:16:53 +02:00
parent 6202381e52
commit 1eec302ad8
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D

View File

@ -82,20 +82,37 @@ d | date: Print the date which the data is from.
print(res.date) print(res.date)
elif len(argv[0]) == 3 or len(argv[1]) == 3: elif len(argv[0]) == 3 or len(argv[1]) == 3:
# more than 3 letter abbreviations are invalid # more than 3 letter abbreviations are invalid
if is_float(argv[0]): cur_use = str
# amount first -> convert to currency in argv[1] amt_use = float
print(fmt_and_calc( dir_use = int
cur=argv[1].upper(), if len(argv) == 1 and not is_float(argv[0]):
amt=float(argv[0]), # only currency specified
res=res, cur_use = argv[0].upper()
direction=DIRECTION_FROM_EUR)) amt_use = 1
dir_use = DIRECTION_TO_EUR
elif is_float(argv[0]):
# amount first -> convert EUR to currency in argv[1]
cur_use = argv[1].upper()
amt_use = float(argv[0])
dir_use = DIRECTION_FROM_EUR
elif is_float(argv[1]): elif is_float(argv[1]):
# currency first -> convert to EUR # currency first -> convert argv[1] to EUR
cur_use = argv[0].upper()
amt_use = float(argv[1])
dir_use = DIRECTION_TO_EUR
else:
raise ValueError
# check that the currency exists
if cur_use not in res.rates:
raise ValueError
else:
print(fmt_and_calc( print(fmt_and_calc(
cur=argv[0].upper(), cur=cur_use,
amt=float(argv[1]), amt=amt_use,
res=res, res=res,
direction=DIRECTION_TO_EUR)) direction=dir_use
))
else: else:
print("Not implemented: '" + " ".join(argv) + "'") print("Not implemented: '" + " ".join(argv) + "'")
except IndexError: except IndexError:
@ -103,6 +120,8 @@ d | date: Print the date which the data is from.
pass pass
else: else:
print("Too few arguments: '" + " ".join(argv) + "'") print("Too few arguments: '" + " ".join(argv) + "'")
except ValueError:
print("The currency specified does not exist.")
def is_float(string: str) -> bool: def is_float(string: str) -> bool:
try: try: