Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Created June 23, 2018 05:01
Show Gist options
  • Save JamesTheAwesomeDude/75a88f4404bb3e43e0b3992b5784fb9a to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/75a88f4404bb3e43e0b3992b5784fb9a to your computer and use it in GitHub Desktop.
import csv
CSVFILE='2018H1.csv'# TL:DR:
#Spending money:
# -,Personal.Recreation.Social,28.37
# -,Groceries,45.32
#Getting money:
# +,Income.Job,420.69
#Being subsidized:
# *,Gas,25.00
#Administrative corrections (e.g., chargebacks, canceled checks, etc):
# %,Personal,17.00
with open(CSVFILE) as f:
#TODO large budget file support
og=[e for e in csv.reader(f)]
budget=dict()
for t,c,v in og:
if t not in budget:
budget[t]=list()
budget[t].append( (c,int(float(v)*100 + 0.4999)) )
cat_sep=['Personal.*', 'Personal.Technology', 'Personal.Recreation.Social', 'Personal.Recreation']
budg2=dict()
def handle_item(t, c, v, budg2=budg2):
if t=='-':
if not c in budg2:
budg2[c]=0
budg2[c]+=v
elif t=='+':
pass
elif t=='*' or t=='%':
if not c in budg2:
budg2[c]=0
budg2[c]-=v
else:
raise NotImplementedError
def check_is_match(cat, cat_sep):
temp_cat=cat.split('.')[::-1]
for element in cat_sep.split('.'):
if len(temp_cat):
# Still items yet to pop
# Compare next item of cat_sep with next item of cat
if element != temp_cat.pop():
# MISMATCH, kill it
return False
else:
#len==0, but for-loop is still running,
#therefore cat was shorter than cat_sep
return False
# The for-loop exited, so cat_sep was shorter or equal
# -AND all items tested matched
return True
for t,l in budget.items():
for c,v in l:
for c_s in cat_sep:
if check_is_match(c, c_s):
handle_item(t,c_s,v,budg2)
break
else:
handle_item(t,c.split('.')[0],v,budg2)
labels=[]
values=[]
for c,v in budg2.items():
labels.append(c)
values.append(v/100)
import plotly.offline
import plotly.graph_objs
trace = plotly.graph_objs.Pie(labels=labels, values=values)
plotly.offline.plot([trace],filename='out.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment