aboutsummaryrefslogtreecommitdiff
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
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--README.md2
-rw-r--r--utils/read_file.py26
2 files changed, 26 insertions, 2 deletions
diff --git a/README.md b/README.md
index 87fc2ba..0ae36a9 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ over the original project.
- Support for commands:
- [ ] Register.
- [ ] Balance.
- - [ ] Print.
+ - [X] Print.
- Support for flags:
- [ ] `--sort`.
- [ ] `--price-db`.
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 = ""