I have a function named OpenAccount() which takes in the details from the User and appends it to my database dictionary.
I have a database file(module) which is imported in my function file.
I have a function called AppendRecord(key,**dictvalues) which appends values to my database file.
However I am not able to call the function and take in values at run-time to append.
It works fine with Hard-coded values.I’m posting the code.Please help me out.
def AppendRecord(key, **dictvalues):
salesDept[key] = dict(dictvalues)
and the call for hard coded values is …
AppendRecord('Jill',Name='Jill', Acctype='Savings')
Now when I’ trying to take in all the values along with the key from the user,I am not able to call the function.I’m just a beginner at Python so please pardon me for any errors:
Edited Code:
#!/usr/bin/python
import os #This module is imported so as to use clear function in the while-loop
import DB #Imports the data from database DB.py
def AppendRecord(key, **dictvalues):
DB.Accounts[key] = dict(dictvalues)
def OpenAccount(): #Opens a new a/c and appends to the database data-base.
while True:
os.system("clear") #Clears the screen once the loop is re-invoked
#Parameters taken from the user at Run-time so as to make entries in the database and append them
print '\n','Choose an Account Type'
print '\n','\n1)Savings Account','\n2)Current Account'
choice = input('Enter an optin: ')
if choice == 1:
name = raw_input('\nEnter a name: ')
depo = input('\nEnter amount(Rs.): ')
key = raw_input('\nEnter an alphanumeric-id: ')
acc= raw_input('\nEnter the Account-Type: ')
AppendRecord(key,Name=name,Initial_deposit=depo,Acctype=acc)
EDIT 1:
The Errors I get are.
File "/usr/lib/python2.5/shelve.py", line 124, in __setitem__
self.dict[key] = f.getvalue()
TypeError: 'int' object does not support item assignment
EDIT 2:
Following is the DB.py database file source code.
#!/usr/bin/python
import shelve #Module:Shelve is imported to achieve persistence
Victor = {'Name':'Victor Hughes','Acctype':'Savings'}
Xavier = {'Name':'Xavier Bosco','Acctype':'Savings'}
Louis = {'Name':'Louis Philip','Acctype':'Current'}
Beverly = {'Name':'Beverly Dsilva','Acctype':'Current'}
Accounts = shelve.open('shelfile.shl') #Accounts = {}
Accounts['Louis']= Louis
Accounts['Beverly']= Beverly
Accounts['Xavier']= Xavier
Accounts['Victor']= Victor
Accounts.close()
Your problem is that the
DBmodule is closing the shelf before you write to it. Get rid of the last line and it will work fine.You will also need to provide a function to close it or do so manually.
You will now need to call the
DB.open_shelf()function before you write to it and theDB.close_shelffunction when you are done. I would recommend calling them at the beginning and end of your program respectively.You’ll note that this is really just wrapping
shelveand adding pretty much zero value. I would scrap the wholeDBmodule and just useshelvedirectly.