Skip to content

Instantly share code, notes, and snippets.

@manicmonkey
Created June 10, 2014 20:38
Show Gist options
  • Select an option

  • Save manicmonkey/8e42d908829cf5956585 to your computer and use it in GitHub Desktop.

Select an option

Save manicmonkey/8e42d908829cf5956585 to your computer and use it in GitHub Desktop.
Short python script to pickle petrol receipts for interesting analytics
from datetime import datetime
import os
import pickle
__author__ = 'manicmonkey'
MILEAGE_DATA_DIR = '/home/manicmonkey/Documents/Mileage/'
class DataParseException(Exception):
def __init__(self, msg, ex = None):
self.msg = msg
self.ex = ex
def read_receipt():
#read in data
mileage = raw_input('Mileage: ')
fuel = raw_input('Fuel: ')
cost = raw_input('Cost: ')
fill_date = raw_input('Date: ')
#try to parse data
try:
int(mileage)
if float(fuel) > 25:
raise DataParseException("Fuel out of range: {0}".format(fuel))
if 5 < float(cost) > 25:
raise DataParseException("Cost out of range: {0}".format(cost))
try:
datetime.strptime(fill_date, '%d%m%Y')
except:
pass
except Exception as e:
raise DataParseException("Problem with data. mileage: '{0}' fuel: '{1}' cost: '{2}' fill_date: '{3}'".format(mileage, fuel, cost, fill_date), e)
return mileage, fuel, cost, fill_date
def read_receipts():
while True:
try:
receipt = read_receipt()
save_receipt(receipt)
#another receipt?
more = raw_input('Another? (Y/n): ')
if more == 'n':
break
except DataParseException as e:
print "Breaking due to: " + e.msg
break
def save_receipt(receipt):
f = open(MILEAGE_DATA_DIR + str(receipt[0]), 'w')
pickle.dump(receipt, f)
print "Pickled: " + str(receipt)
read_receipts()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment