aboutsummaryrefslogtreecommitdiff
path: root/utils/read_file.py
diff options
context:
space:
mode:
authorAdrián Oliva <adrian.oliva@cimat.mx>2023-05-05 08:48:42 -0600
committerAdrián Oliva <adrian.oliva@cimat.mx>2023-05-05 08:48:42 -0600
commit3fad0986c04c6442c2d506387a72e221305902f9 (patch)
tree80bc248588418d1da36562ce7f960d5f3be31c1d /utils/read_file.py
parent83effeb49354ff6d0dc66bee0df3c1e4167810cf (diff)
downloadLedger.py-3fad0986c04c6442c2d506387a72e221305902f9.tar.gz
Ledger.py-3fad0986c04c6442c2d506387a72e221305902f9.zip
Print verb is mostly completed.
May still have bugs, but for now it is good enough.
Diffstat (limited to '')
-rw-r--r--utils/read_file.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/utils/read_file.py b/utils/read_file.py
index ee8f06d..4f5e9ab 100644
--- a/utils/read_file.py
+++ b/utils/read_file.py
@@ -25,14 +25,38 @@ class entry:
return datetime.fromisoformat(f'{int(year)}-{int(month):02}-{int(day):02}')
+ def __str_to_price_format(self, price: str):
+ # Find the first instance of a number. A string of digits that may have
+ # a dot and more digits after.
+ price_nu = re.findall(r'\d+(?:\.\d*)?', price)[0]
+ # For the currency symbol, we get rid of the number.
+ price_sy = price.replace(price_nu, '')
+
+ if '-' in price:
+ # If there was a minus (-), add it to the number and delete it from
+ # the currency symbol.
+ price_nu = f"-{price_nu}"
+ price_sy = price_sy.replace('-', '')
+
+ # Remove the whitespace around the currency symbol.
+ price_sy = price_sy.strip()
+
+ # If the symbol is 1 character long, write it on the left.
+ if len(price_sy) == 1:
+ return f"{price_sy}{float(price_nu):.02f}"
+ # If it is longer than 1 character, write it on the right.
+ else:
+ return f"{float(price_nu):.02f} {price_sy}"
+
+
def __str__(self) -> str:
result = self.date.strftime('%Y/%m/%d')
result += " " + self.comment + "\n"
for trans in self.transactions:
if len(trans) == 2:
- # TODO: `price` must have a specific format!
account, price = trans
+ price = self.__str_to_price_format(price)
else:
account = trans[0]
price = ""