diff options
author | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-07 17:48:41 -0600 |
---|---|---|
committer | Adrián Oliva <adrian.oliva@cimat.mx> | 2023-05-07 17:48:41 -0600 |
commit | 2dcabff113476a18dadf1e281c013115902c2059 (patch) | |
tree | 476cbfd083a81dee5c27b0b4a2bf13e8553fcc3a /utils | |
parent | 674c382afa00d65c550e141a2a758b9b1e348178 (diff) | |
download | Ledger.py-2dcabff113476a18dadf1e281c013115902c2059.tar.gz Ledger.py-2dcabff113476a18dadf1e281c013115902c2059.zip |
`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 (limited to '')
-rw-r--r-- | utils/register.py | 17 |
1 files changed, 14 insertions, 3 deletions
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 |