-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
73 lines (56 loc) · 1.58 KB
/
tasks.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
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
import csv
from collections import defaultdict
from decimal import Decimal
from invoke import task
from terminaltables import AsciiTable
@task
def business(ctx):
"""
Print business checking account metrics.
"""
categories = defaultdict(Decimal)
with open('business.csv') as fp:
reader = csv.DictReader(fp)
for row in reader:
category = row['Category']
value = None
try:
value = Decimal(row['Debit'])
except:
value = Decimal(0)
if category == 'Income':
value = Decimal(row['Credit'])
categories[category] += value
data = sorted(categories.items(), key=lambda x: x[0])
table = AsciiTable(data, 'Totals by category')
table.inner_heading_row_border = False
print(table.table)
@task
def oot(ctx):
"""
Print breakdown of out-of-town expenses for each trip
"""
categories = defaultdict(Decimal)
trips = defaultdict(lambda: defaultdict(Decimal))
with open('business.csv') as fp:
reader = csv.DictReader(fp)
for row in reader:
category = row['Category']
if not category.startswith('OOT:'):
continue
trip = trips[row['OOT code']]
value = None
try:
value = Decimal(row['Debit'])
except:
value = Decimal(0)
categories[category] += value
trip[category] += value
for code, trip in trips.items():
data = trip.items()
table = AsciiTable(data, code)
table.inner_heading_row_border = False
print(table.table)
table = AsciiTable(categories.items(), 'Categories')
table.inner_heading_row_border = False
print(table.table)