Well, if you want the hard part...
Coming from a financial background I can assure you, that saving entries into a csv file permanently is a a bad idea. Unless the only purpose for this file is to import it to something more robust.
Deleting transactions looks like a bad idea, too. In bigger systems (more than one account, or more than one user) this is considered hacking and comes into play only, when something got really wrong. Consider storno...
If this is a basic budgeting program, then I personally would use:
1. Datastore layer
2. A data access layer
3. A business logic layer
4. A user interface layer
Datastore layer can be an any sql database, for the begin sqlite(included in python) will suffice. All other layer can be written in the begin by hand, and later you can try something more appropriate, like sqlalchemy for data access, or wxpython for UI.
The layering is very good, because you can separate concerns and can develop them (almost) separately.
But everything depends on, what you trying to accomplish.
So my imaginary budgeting program would have the functions:
- Set initial balance
- Input expenses/incomes
- Report balance/transactions
I will not go into more detail, because my post will end up as a novel. But more questions are to be answered, like users, authorization, authentication, error checking, database design, fine tuning of requirements and so on. And with more answers come more questions
For learning purposes in the beginning every layer(except datastore) is only a class. In time these classes get bigger, and become modules...
I have written a very limited version of that. You can put in expenses, and get the balance. No error checking, no input validation, full of lazy shortcuts.
import sqlite3
class DataAccess():
my_database="budget.db"
def __init__(self):
self.connection=sqlite3.connect(self.my_database)
def create_database(self):
self.connection.execute("create table account(id integer primary key, balance number);")
self.connection.execute("create table transact(id integer primary key, narrativ text, amount number);")
self.connection.execute("insert into account(balance) values(0);")
self.connection.commit()
def input_transaction(self, amount, narrativ):
sqlstr="insert into transact (narrativ, amount) values(\"%s\",%s); " % (narrativ,amount)
self.connection.execute(sqlstr)
self.connection.execute("update account set balance=balance+%s; " % (amount))
self.connection.commit()
def get_balance(self):
cu=self.connection.cursor()
cu.execute("select * from account;")
return cu.fetchall()
class BussinesLogic():
def __init__(self):
self.database=DataAccess()
def report_balance(self):
return self.database.get_balance()[0][1]
def input_expense(self,expense, narrativ):
self.database.input_transaction(-expense,narrativ)
def create_database(self):
self.database.create_database()
import sys
class UserInterface():
def __init__(self, bl):
self.bl=bl
self.menu=dict()
self.set_menu()
def set_menu(self):
self.menu[4]=("Report Balance",self.display_balance)
self.menu[1]=("Input expense",self.get_expense)
self.menu[2]=("Exit program",self.exit)
self.menu[3]=("Create_database (budget.db)",self.bl.create_database)
def display_balance(self):
print ("your balance is: %s"%self.bl.report_balance())
def get_expense(self):
print("please input the data")
amount=int(raw_input("amount:"))
narrativ=raw_input("narrativ:")
self.bl.input_expense(amount,narrativ)
def exit(self):
print ("Goodbye")
sys.exit(0)
def display_menu(self):
print ("Personal Budgeting")
print ("Please choose a function!")
for k,v in self.menu.iteritems():
print k,v[0]
def main(self):
choice=-1
while True:
self.display_menu()
choice=int(raw_input("?:"))
self.menu[choice][1]()
if __name__=="__main__":
BL=BussinesLogic()
UI=UserInterface(BL)
UI.main()