From 2dcabff113476a18dadf1e281c013115902c2059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Sun, 7 May 2023 17:48:41 -0600 Subject: `currencies` class can now convert to string. Before we had to iterate and immediatly stop. Now if there's only one type of currency, we can return the string of it. --- utils/register.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'utils') diff --git a/utils/register.py b/utils/register.py index 0a103fb..b067e56 100644 --- a/utils/register.py +++ b/utils/register.py @@ -85,11 +85,11 @@ class currencies: """ if not self.money: yield '0' - for currency, number in self.money.items(): + for currency, amount in self.money.items(): if len(currency) == 1: - yield f'{currency}{number:.02f}' + yield f'{currency}{amount:.02f}' else: - yield f'{number:.02f} {currency}' + yield f'{amount:.02f} {currency}' # What to do if we do "-currencies"? @@ -113,6 +113,17 @@ class currencies: return amount_left < amount_right + def __str__(self) -> str: + if len(self.money.items()) != 1: + raise Exception('Cannot convert to string more than one currency!') + + for currency, amount in self.money.items(): + if len(currency) == 1: + return f'{currency}{amount:.02f}' + else: + return f'{amount:.02f} {currency}' + + def complete_prices(my_entry: entry): """ Transform all string of prices to the class `currencies`. If there is an -- cgit v1.2.3