-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_methods.py
61 lines (55 loc) · 1.77 KB
/
data_methods.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
from bs4 import BeautifulSoup
from datetime import date, timedelta
import matplotlib.pyplot as plt
plt.xticks(rotation=60)
def make_parser(html_doc):
"""
Take in an html string and return a BeautifulSoup parser
"""
parser = BeautifulSoup(html_doc, 'html.parser')
return parser
def get_tag_info(tag):
"""
Take in a <tr> tag and return a tuple of the form:
(date, expense, new_balance, location)
"""
tags = tag.find_all('td')
time_tag = tags[0]
tag_text = time_tag.text
month, day, year = tuple(tag_text.split()[0].split('/'))
this_date = date(year=int(year), month=int(month), day=int(day))
expense = float(tags[1].text[1:])
new_balance = float(tags[2].text[1:])
location = tags[3].text
return (this_date, expense, new_balance, location)
def get_all_tags(parser):
"""
Take in a parser with the flex dollar expenses page and return a list of
infos for each tag (see get_tag_info for more reference)
"""
infos = list()
tags = parser.find_all('tr')[2:]
for tag in tags:
infos.append(get_tag_info(tag))
return infos
def plot_expenses(tags_info):
"""
Gets information about flex dollars expenses for each date and plots them
using matplotlib.pyplot
"""
expenses = dict()
for tag_info in tags_info:
date, expense, new_balance, locations = tag_info
if date not in expenses:
expenses[date] = 0
expenses[date] += expense
x, y = [], []
all_dates = list(expenses.keys())
min_date, max_date = min(all_dates), max(all_dates)
curr_date = min_date
while curr_date <= max_date:
x.append(curr_date)
y.append(expenses.get(curr_date, 0))
curr_date = curr_date + timedelta(days=1)
plt.plot(x, y)
plt.show()