commit 2dcabff113476a18dadf1e281c013115902c2059
parent 674c382afa00d65c550e141a2a758b9b1e348178
Author: Adrián Oliva <[email protected]>
Date: Sun, 7 May 2023 17:48:41 -0600
`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.
Diffstat:
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git 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