Last active
November 3, 2017 01:21
-
-
Save migueldferreira/9485468 to your computer and use it in GitHub Desktop.
Simple python3 script that defines a set of functions to create, populate and query a balance database to keep track of your transactions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python5 | |
import rlcompleter, readline | |
readline.parse_and_bind('tab:complete') | |
import atexit | |
from calendar import month_abbr | |
import sys | |
import math | |
import sqlite3 | |
import matplotlib.pyplot as pyplot | |
dbname = sys.argv[1] if len(sys.argv) > 2 else 'balance.db' | |
conn = sqlite3.connect(dbname) | |
c = conn.cursor() | |
def create_db(): | |
c.execute("""CREATE TABLE transactions (id integer primary key autoincrement, | |
day int not null, month int not null, year int not null, | |
credit_units int not null default 0, credit_decimals int not null default 0, | |
debit_units int not null default 0, debit_decimals int not null default 0, | |
description nvarchar(255) not null, type int not null default 0)""") | |
conn.commit() | |
""" | |
Array to be filled in with values | |
Example: | |
transactions = [ | |
(1, 1, 2014, 50, 0, 0, 0, "Friend's loan payback") | |
(31, 12, 2014, 0, 0, 100, 50, "New Year's Eve party!") | |
] | |
""" | |
transactions = [] | |
def insert_values(transactions): | |
query = """INSERT INTO transactions (day, month, year, | |
credit_units, credit_decimals, debit_units, debit_decimals, description) | |
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""" | |
c.executemany(query, transactions) | |
def total_balance(): | |
c.execute("""SELECT sum(credit_units), sum(credit_decimals), | |
sum(debit_units), sum(debit_decimals) FROM transactions""") | |
ans = c.fetchall() | |
ans = ans[0] | |
print("Credit: " + str((ans[0]*100 + ans[1])/100)) | |
print("Debit: " + str((ans[2]*100 + ans[3])/100)) | |
return (ans[0]*100 + ans[1] - ans[2]*100 - ans[3])/100 | |
def print_transactions(filename="balance.txt"): | |
f = open(filename, 'wt') | |
c.execute("select * from transactions") | |
ans = c.fetchall() | |
for row in ans: | |
f.write("%4d:\t%02d/%02d/%4d\t%5d.%02d €\t%5d.%02d €\t%s\n" % row[0:9]) | |
f.close() | |
def export_db(filename="export.txt"): | |
c.execute("pragma table_info(transactions)") | |
ans = c.fetchall() | |
header = '' | |
for row in ans: | |
header += row[1] + '|' | |
header = header[0:90] | |
f = open(filename, 'wt') | |
f.write(header + '\n') | |
c.execute("select * from transactions") | |
ans = c.fetchall() | |
for row in ans: | |
line = '' | |
for i in range(10): | |
line += str(row[i]) + '|' | |
f.write(line[0:len(line)-1] + '\n') | |
f.close() | |
def import_db(filename): | |
create_db() | |
f = open(filename, 'r') | |
header = f.readline() | |
header = header[0:len(header)-1].replace('|', ', ') | |
ans = f.readlines() | |
transactions = '' | |
for row in ans: | |
transactions += '(' + row[0:len(row)-1].replace('|', ', ') + '), ' | |
transactions = transactions[0:len(transactions)-1] | |
c.execute("INSERT INTO transactions (%s) VALUES %s" % (header, transactions)) | |
conn.commit() | |
def monthly_graph(): | |
c.execute("""SELECT month, year, sum(credit_units), sum(credit_decimals), | |
sum(debit_units), sum(debit_decimals) FROM transactions GROUP BY year, month""") | |
ans = c.fetchall() | |
xmax = len(ans) | |
xcred = [x - 0.4 for x in range(1,xmax+1)] | |
ycred = [row[2]+row[3]/100 for row in ans] | |
xdebt = [x + 0.4 for x in xcred] | |
ydebt = [row[4]+row[5]/100 for row in ans] | |
#ymax = math.ceil(max(ycred + ydebt)) | |
ymax = 2500; | |
pyplot.figure(num=None, figsize=(30, 14.0625)) | |
pyplot.axis([0, xmax+1, 0, ymax]) | |
pyplot.yticks([i*100 for i in range(0, math.ceil(ymax/100)+1)]) | |
pyplot.xticks(range(1, xmax+1), [month_abbr[row[0]] + " " + str(row[1]) for row in ans],\ | |
rotation=25, ha='right') | |
pyplot.grid(b=True, axis='y', zorder=1) | |
pyplot.bar(xcred, ycred, width=0.4, color='green', label='Credit', zorder=5) | |
pyplot.bar(xdebt, ydebt, width=0.4, color='red', label='Debit', zorder=5) | |
pyplot.legend() | |
pyplot.title('Balance') | |
pyplot.savefig('graph.png') | |
@atexit.register | |
def close(): | |
print("Closing database...") | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment