In this video tutorial on how to use Python, I will focus on how to use an interesting Python Module called Shelve. Shelve allows you to store data in files similar to how a database works. It is very easy to use and provides a convenient way to take advantage of the power of Python dictionaries while providing you with the ability to change the data.
I’ll continue in the next tutorial to cover another interesting Python Module called SQLite. It is a Module that can be used as if it was a database meaning:
However, it operates without a need to have an actual database server. More on it next time.
Like always, a lot of code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:
Here is All the Code from the Video
#! /usr/bin/python
import shelve
def addCust(database):
customer = {}
print “Add a new customer to the database\n”
custNum = raw_input(‘Enter a customer number: ‘)
customer[‘firstName’] = raw_input(‘Customer First Name: ‘)
customer[‘lastName’] = raw_input(‘Customer Last Name: ‘)
customer[‘streetAdd’] = raw_input(‘Customer Street Address: ‘)
customer[‘city’] = raw_input(‘Customer City: ‘)
customer[‘state’] = raw_input(‘Customer State: ‘)
customer[‘zip’] = raw_input(‘Customer Zip Code: ‘)
database[custNum] = customer
return
def main():
database = shelve.open(‘customers.dat’, writeback=True)
addCust(database)
lookForCust = 1
while (lookForCust != ‘0’):
lookForCust = raw_input(“Enter Customer Number(0 to Exit)”)
if lookForCust ==’0′:
break
else:
try:
for i in database[lookForCust]:
print i, ” “, database[lookForCust][i]
except KeyError:
print “Customer not in database”
break
else:
print “\n”
database.close()
if __name__ == ‘__main__’: main()
Little issue here:
I’m getting an error that reads:
database = shelve.open(‘customers.dat’, writeback=True)
AttributeError: ‘module’ object has no attribute ‘open’
Has “Shelve” been updated? I can’t seem to “open” it.
Make sure everything is indented properly. That is the only way I was able to get that error. Sorry that is the only thing I can come up with with just that little bit of code
It seems to be. Tried a few combinations but it lined up with the database = open… line:
import shelve
def addCust(database):
customer = {}
print “Add a new customer to the database.”
custNum = raw_input(“Enter a customer number.”)
customer[‘firstName’] = raw_input(‘Customer First Name: ‘)
customer[‘lastName’] = raw_input(‘Customer Last Name: ‘)
customer[‘streetAdd’] = raw_input(‘Customer Street Address: ‘)
customer[‘City’] = raw_input(‘Customer City: ‘)
customer[‘State’] = raw_input(‘Customer State: ‘)
customer[‘Zip’] = raw_input(‘Customer Zip Code: ‘)
database[custNum] = customer
return
def main():
database = shelve.open(‘customers.dat’, writeback=True)
addCust(database)
lookForCust = 1
while (lookForCust !=0):
lookForCust = raw_input(“Enter Customer Number(0 to Exit) “)
if lookForCust == ‘0’:
break
else:
try:
for i in database[lookForCust]:
print i, ” “, database[lookForCust], i
except KeyError:
print “Customer not in database.”
break
else:
print “\n”
database.close()
if __name__ == ‘__main__’: main()
Derek:
I think the problem was that Eclipse doesn’t seem to have imported shelve or spllite with the Python package.
I ran it in IDLE and it worked fine.
# ” if __name__ == ‘__main__’: main() ”
Dear instructor my name is matt
and im confused with the line of
code. Do I always use this at the
end of file?
It is basically used so that the python interpreter can tell the differenct between the main program and imported modules. When the python interpreter executes a program if it is the main program, it gives the special variable __name__ the value “__main__”. If you input another module into your code that module won’t have the value “__main__” set for its __name__.