-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt.py
37 lines (26 loc) · 1.17 KB
/
fmt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from data_model.transaction import Transaction
def format_amount(amount: float, precision: int = 2) -> str:
str_fmt = '{:.' + str(precision) + 'f}'
return str_fmt.format(amount)
def number_length_before_dot(f: float) -> int:
s = format_amount(f)
n = s.find('.')
if n != -1:
return n
else:
return len(s)
def format_line(account, amount, currency, absolute_neg=False, dot_pos=64) -> str:
content = ' {0}'.format(account)
if absolute_neg:
amount = -1 * abs(amount)
padding = dot_pos - len(content) - number_length_before_dot(amount)
content += ' ' * padding
content += format_amount(amount) + ' ' + currency
return content
def format_transaction(transaction: Transaction) -> str:
header = '{0} * "{1}"'.format(str(transaction.transaction_date), transaction.payee)
if transaction.description.strip() != '':
header += ' "{0}"'.format(transaction.description)
to_line = format_line(transaction.to_account, transaction.amount, transaction.currency)
from_line = format_line(transaction.from_account, transaction.amount, transaction.currency, True)
return F'{header}\n{to_line}\n{from_line}\n'