| | |
Newbie- Need Assistance
Thread Solved
![]() |
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
Please I need help with this program. I tried different approach, but doesn't seem to be getting no where.
QUESTION:
You've been going to work on a database project at work for sometime now. Your boss encourages you to program the database in Python. You disagree, arguing that Python is not a database language but your boss persists by providing the source code below for a sample telephone database. He asks you to do two things: 1. Evaluate the existing source code and extend it to make it useful for managers in the firm. (You do not need a GUI interface, just work on the database aspects: data entry and retrieval - of course you must get the program to run or properly work, and 2. He wants you to critically evaluate Python as a database tool.
I got stuck while running the program with the following error:
QUESTION:
You've been going to work on a database project at work for sometime now. Your boss encourages you to program the database in Python. You disagree, arguing that Python is not a database language but your boss persists by providing the source code below for a sample telephone database. He asks you to do two things: 1. Evaluate the existing source code and extend it to make it useful for managers in the firm. (You do not need a GUI interface, just work on the database aspects: data entry and retrieval - of course you must get the program to run or properly work, and 2. He wants you to critically evaluate Python as a database tool.
python Syntax (Toggle Plain Text)
import shelve import string def print_menu(): print 'Welcome to the Phone Record' print '1. Add Name' print '2. Delete Name' print '3. Look Up Name' print '9. Quit' numbers = () menu_choice = 0 print_menu() while menu_choice != 9: menu_choice = input("Type in a number (1-9):") if menu_choice == 1: print "Add Number:" Name = raw_input("Name:") Work = raw_input("Work Number:") Home = raw_input("Home Number:") Fax = raw_input("Fax Number:") Cell = raw_input("Cell NUmber:") elif menu_choice == 2: print "Delete Name;" Name = raw_input("Name:") elif menu_choice == 3: print "Look Up Name;" Name = raw_input ("Name:") print "Name:" 'x' "\tNumber:", numbers[x] UNKNOWN = 0 HOME = 1 WORK = 2 FAX = 3 CELL = 4 class phoneentry: def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN): self.name = name self.number = number self.type = type # create string representation def __repr__(self): return ('%s:%d' % (self.name, self.type)) # fuzzy compare or two items def __cmp__(self, that): this = string.lowe(str(self)) that = string.lower(that) if string.find(this, that) >= 0: return (0) return (cmp(this, that)) def showtype(self): if self.type == UNKNOWN: return ('Unknown') if self.type == HOME: return ('Home') if self.type == WORK: return ('Work') if self.type == FAX: return ('Fax') if self.type == CELL: return ('Cellular') class phonedb: def __int__(self, dbname = 'phonedata'): self.dbname = dbname; self.shelve = shelve.open(self.dbname); def __del__(self): self.shelve.close() self.shelve = None def add(self, name, number, type = HOME): e = phoneentry(name, number, type) def lookup(self, string): list = [] for key in self.shelve.keys(): e = self.shelve[key] if cmp(e, string) == 0: list.append(e) return (list) # if not being loaded as a module, run a small test if __name__ == '__main__': foo = phonedb() foo.add('Sean Reifschneider', '970-555-1111', HOME) foo.add('Sean Reifschneider', '970-555-2222', FAX) foo.add('Evelyn Mitchell', '970-555-1111', HOME) print 'First lookup:' for entry in foo.lookup('reifsch'): print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype()) print 'Second lookup:' for entry in foo.lookup('e'): print '%-40 %s (%s)' % (entry.name, entry.number, entry.showtype())
I got stuck while running the program with the following error:
Welcome to the Phone Record 1. Add Name 2. Delete Name 3. Look Up Name 9. Quit Type in a number (1-9):1 Add Number: Name:Elizabeth Work Number:718-555-1111 Home Number:718-555-2222 Fax Number:718-555-2223 Cell NUmber:718-555-1122 Type in a number (1-9):9 First lookup: Traceback (most recent call last): File "C:\Python25\phoneroster.py", line 95, in <module> for entry in foo.lookup('reifsch'): File "C:\Python25\phoneroster.py", line 80, in lookup for key in self.shelve.keys(): AttributeError: phonedb instance has no attribute 'shelve' >>>
Last edited by vegaseat; Jun 12th, 2007 at 12:20 pm. Reason: [code=python] tag so linenumbers show
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
Here is the original code
And I was trying to create phone record through data input. So, this is what I added:
python Syntax (Toggle Plain Text)
import shelve import string UNKNOWN = 0 HOME = 1 WORK = 2 FAX = 3 CELL = 4 class phoneentry: def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN): self.name = name self.number = number self.type = type # create string representation def __repr__(self): return ('%s:%d' % (self.name, self.type)) # fuzzy compare or two items def __cmp__(self, that): this = string.lowe(str(self)) that = string.lower(that) if string.find(this, that) >= 0: return (0) return (cmp(this, that)) def showtype(self): if self.type == UNKNOWN: return ('Unknown') if self.type == HOME: return ('Home') if self.type == WORK: return ('Work') if self.type == FAX: return ('Fax') if self.type == CELL: return ('Cellular') class phonedb: def __int__(self, dbname = 'phonedata'): self.dbname = dbname; self.shelve = shelve.open(self.dbname); def __del__(self): self.shelve.close() self.shelve = None def add(self, name, number, type = HOME): e = phoneentry(name, number, type) self.shelve[str(e)] = e def lookup(self, string): list = [] for key in self.shelve.keys(): e = self.shelve[key] if cmp(e, string) == 0: list.append(e) return (list) # if not being loaded as a module, run a small test if __name__ == '__main__': foo = phonedb() foo.add('Sean Reifschneider', '970-555-1111', HOME) foo.add('Sean Reifschneider', '970-555-2222', FAX) foo.add('Evelyn Mitchell', '970-555-1111', HOME) print 'First lookup:' for entry in foo.lookup('reifsch'): print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype()) print 'Second lookup:' for entry in foo.lookup('e'): print '%-40 %s (%s)' % (entry.name, entry.number, entry.showtype())
And I was trying to create phone record through data input. So, this is what I added:
python Syntax (Toggle Plain Text)
import shelve, _bsddb filename = 'C:/Pyhton25/phonerecord.py' def dele(name, ptype): key = name + ':' + ptype db = shelve.open(filename) try: try: del db[key] print 'Entry deleted.' except _bsddb.DBNotFoundError: print 'No Entries with', key, 'exist!' finally: db.close() b = 1 foo = phonedb() while b != 4: print "Welcome to the Phone Database" print "Please choose from the following" print "If you would like to display Phone Number(s) select 1" print "If you would like to add an entry select 2" print "If you would like to delete for an entry select 3" print "To quit select 4 " b = input(':') if b == 1: print 'Enter the lookup up key to find (For example for all names with ''Rob'' in them type Rob)' a = raw_input(':') for entry in foo.lookup(a): print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() ) if b == 2: print "Please enter the full name: (Example: John Smith)" n = raw_input(':') print "Please enter the phone number: (Example: 970-432-5432)" p = raw_input(':') print "Please enter the phone type: (0 = Unkown, 1 = Home, 2 = Work, 3 = Fax, 4 = Cell)" t = raw_input(':') if t == '0': foo.add(n, p, UNKNOWN) if t == '1': foo.add(n, p, HOME) if t == '2': foo.add(n, p, WORK) if t == '3': foo.add(n, p, FAX) if t == '4': foo.add(n, p, CELL)
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
You have mistyped __init__
It's __int__ in your phonedb class, look carefully...
python Syntax (Toggle Plain Text)
class phonedb: def __init__(self, dbname = 'phonedata'): self.dbname = dbname; self.shelve = shelve.open(self.dbname); def __del__(self): self.shelve.close() self.shelve = None def add(self, name, number, type = HOME): e = phoneentry(name, number, type) self.shelve[str(e)] = e def lookup(self, string): list = [] for key in self.shelve.keys(): e = self.shelve[key] if cmp(e, string) == 0: list.append(e) return (list) # if not being loaded as a module, run a small test if __name__ == '__main__': foo = phonedb() foo.add('Sean Reifschneider', '970-555-1111', HOME) foo.add('Sean Reifschneider', '970-555-2222', FAX) foo.add('Evelyn Mitchell', '970-555-1111', HOME) print 'First lookup:' for entry in foo.lookup('reifsch'): print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype()) print 'Second lookup:' for entry in foo.lookup('e'): print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- UNIX Newbie / Boot assistance on Sun Ultra5 (*nix Software)
- Mac Newbie - IE and WMP on Mac? (OS X)
- Obtain Remote Assistance by Sending an E-mail Message in Windows XP (Windows tips 'n' tweaks)
- vBulletin technical assistance (Existing Scripts)
Other Threads in the Python Forum
- Previous Thread: "Hosts script: Python"
- Next Thread: Read Client Folder and more
| Thread Tools | Search this Thread |
abrupt accessdenied ansi anti apache application approximation argv array assignment backend beginner binary bluetooth builtin calculator change character converter countpasswordentry curved customdialog dan08 dictionary edit exe file float format function gnu heads homework ideas inches input java keyboard lapse leftmouse library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook pointer prime programming py2exe pygame pyopengl python random recursion redirect remote reverse scrolledtext session software sprite sqlite statictext statistics string strings syntax terminal text thread threading time tlapse tuple twoup ubuntu unicode unit urllib urllib2 variable wordgame write wxpython xlib







