blob: ec24f6e46ef2914c0450548dc5c459d25539c223 (
plain) (
blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
from utils.read_file import entry
from typing import List
from utils.register import complete_prices
def separate_currencies(my_entries: List[entry]):
"""
Given a list of entries with just one transaction, group entries with
respect to their currencies symbols.
"""
seen_curr = set()
list_curr = []
# First search for every currency we can find in `my_entries`.
for ent in my_entries:
for trans in ent.transactions:
wallet = trans[1]
if len(wallet.money.keys()) != 1:
raise Exception('Cannot compare more than one currency at a time.')
for curr in wallet.money.keys():
break
if curr not in seen_curr:
seen_curr.add(curr)
list_curr.append(curr)
# Now group each currency. Select a currency and append entries that have
# that specific currency. Then select the next seen currency and so on.
result = []
for my_currency in list_curr:
for ent in my_entries:
wallet = ent.transactions[0][1]
for curr in wallet.money.keys():
break
if my_currency == curr:
result.append(ent)
return result
def join_concurrent_entries(my_entries: List[entry]):
result = []
for ent in my_entries:
if len(result) == 0:
result.append(ent)
elif ent.comment == result[-1].comment and ent.date == result[-1].date:
result[-1].transactions.append(
ent.transactions[0]
)
else:
result.append(ent)
return result
def sort_by_date(my_entries: List[entry]):
my_entries.sort(key=lambda x: x.date)
def sort_by_amount(my_entries: List[entry]):
"""
We have to break entries by their transactions. Then we can sort the
entries by their amount in their unique transaction.
"""
# Break entries for them to have only one transaction.
result = []
for ent in my_entries:
complete_prices(ent)
for trans in ent.transactions:
result.append(
entry(
date=ent.date.strftime('%Y/%m/%d'),
comment=ent.comment,
transactions=[trans]
)
)
# Sort each transaction given the amount value.
result.sort(key=lambda x: x.transactions[0][1])
# Separate different currencies from each other.
result = separate_currencies(result)
# Join concurrent entries if they have the same comment and date.
result = join_concurrent_entries(result)
# Hacky way of modifying the original `my_entries` list.
while my_entries:
my_entries.pop()
for item in result:
my_entries.append(item)
# Each transaction now has the `currencies` class format. Convert all of
# them into strings.
for ent in my_entries:
for trans in ent.transactions:
for price in trans[1]:
break
trans[1] = price
def sort_entries(my_entries: List[entry], rule: str) -> None:
"""
Sort the list of entries in-place. This function returns None, the list
given as a parameter will be modified.
"""
# Sort by date.
if rule.lower() in ['d', 'date']:
sort_by_date(my_entries)
# Sort by amount.
if rule.lower() in ['a', 'amount']:
sort_by_amount(my_entries)
|