views:

84

answers:

1

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()
+1  A: 

Your problem is that the DB module 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.

#!/usr/bin/python

import shelve                   #Module:Shelve is imported to achieve persistence


Accounts = 0

Victor = {'Name':'Victor Hughes','Acctype':'Savings'} #???
Xavier = {'Name':'Xavier Bosco','Acctype':'Savings'}
Louis = {'Name':'Louis Philip','Acctype':'Current'}
Beverly = {'Name':'Beverly Dsilva','Acctype':'Current'}


def open_shelf(name='shelfile.shl'):
    global Accounts
    Accounts = shelve.open(name)          #Accounts = {}
    # why are you adding this every time? is this just debugging code?
    Accounts['Louis']= Louis
    Accounts['Beverly']= Beverly
    Accounts['Xavier']= Xavier
    Accounts['Victor']= Victor


def close_shelf():
    Accounts.close()

You will now need to call the DB.open_shelf() function before you write to it and the DB.close_shelf function 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 shelve and adding pretty much zero value. I would scrap the whole DB module and just use shelve directly.

aaronasterling
Yes this just a debugging code.+1 Thankyou it works fine with your answer.But when I try to read from 'shelfile.shl',I do not see the appended values. :( I'm confused
Pavitar