944,044 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1478
  • Python RSS
Jun 12th, 2007
0

Newbie- Need Assistance

Expand Post »
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.

python Syntax (Toggle Plain Text)
  1. import shelve
  2. import string
  3.  
  4. def print_menu():
  5. print 'Welcome to the Phone Record'
  6. print '1. Add Name'
  7. print '2. Delete Name'
  8. print '3. Look Up Name'
  9. print '9. Quit'
  10.  
  11. numbers = ()
  12. menu_choice = 0
  13. print_menu()
  14. while menu_choice != 9:
  15. menu_choice = input("Type in a number (1-9):")
  16. if menu_choice == 1:
  17. print "Add Number:"
  18. Name = raw_input("Name:")
  19. Work = raw_input("Work Number:")
  20. Home = raw_input("Home Number:")
  21. Fax = raw_input("Fax Number:")
  22. Cell = raw_input("Cell NUmber:")
  23.  
  24. elif menu_choice == 2:
  25. print "Delete Name;"
  26. Name = raw_input("Name:")
  27.  
  28. elif menu_choice == 3:
  29. print "Look Up Name;"
  30. Name = raw_input ("Name:")
  31. print "Name:" 'x' "\tNumber:", numbers[x]
  32. print
  33.  
  34. UNKNOWN = 0
  35. HOME = 1
  36. WORK = 2
  37. FAX = 3
  38. CELL = 4
  39.  
  40. class phoneentry:
  41. def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN):
  42. self.name = name
  43. self.number = number
  44. self.type = type
  45.  
  46. # create string representation
  47. def __repr__(self):
  48. return ('%s:%d' % (self.name, self.type))
  49.  
  50. # fuzzy compare or two items
  51. def __cmp__(self, that):
  52. this = string.lowe(str(self))
  53. that = string.lower(that)
  54. if string.find(this, that) >= 0:
  55. return (0)
  56.  
  57. return (cmp(this, that))
  58.  
  59. def showtype(self):
  60. if self.type == UNKNOWN: return ('Unknown')
  61. if self.type == HOME: return ('Home')
  62. if self.type == WORK: return ('Work')
  63. if self.type == FAX: return ('Fax')
  64. if self.type == CELL: return ('Cellular')
  65.  
  66. class phonedb:
  67. def __int__(self, dbname = 'phonedata'):
  68. self.dbname = dbname;
  69. self.shelve = shelve.open(self.dbname);
  70.  
  71. def __del__(self):
  72. self.shelve.close()
  73. self.shelve = None
  74.  
  75. def add(self, name, number, type = HOME):
  76. e = phoneentry(name, number, type)
  77.  
  78. def lookup(self, string):
  79. list = []
  80. for key in self.shelve.keys():
  81. e = self.shelve[key]
  82. if cmp(e, string) == 0:
  83. list.append(e)
  84.  
  85. return (list)
  86.  
  87. # if not being loaded as a module, run a small test
  88. if __name__ == '__main__':
  89. foo = phonedb()
  90. foo.add('Sean Reifschneider', '970-555-1111', HOME)
  91. foo.add('Sean Reifschneider', '970-555-2222', FAX)
  92. foo.add('Evelyn Mitchell', '970-555-1111', HOME)
  93. print 'First lookup:'
  94.  
  95. for entry in foo.lookup('reifsch'):
  96. print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
  97.  
  98. print
  99.  
  100. print 'Second lookup:'
  101.  
  102. for entry in foo.lookup('e'):
  103. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
olufunkky is offline Offline
8 posts
since Apr 2007
Jun 12th, 2007
0

Re: Newbie- Need Assistance

What is the original code and what have you added to it? The way it is now, it does not make much sense.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 12th, 2007
0

Re: Newbie- Need Assistance

Here is the original code

python Syntax (Toggle Plain Text)
  1. import shelve
  2. import string
  3.  
  4. UNKNOWN = 0
  5. HOME = 1
  6. WORK = 2
  7. FAX = 3
  8. CELL = 4
  9.  
  10. class phoneentry:
  11. def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN):
  12. self.name = name
  13. self.number = number
  14. self.type = type
  15.  
  16. # create string representation
  17. def __repr__(self):
  18. return ('%s:%d' % (self.name, self.type))
  19.  
  20. # fuzzy compare or two items
  21. def __cmp__(self, that):
  22. this = string.lowe(str(self))
  23. that = string.lower(that)
  24. if string.find(this, that) >= 0:
  25. return (0)
  26. return (cmp(this, that))
  27.  
  28. def showtype(self):
  29. if self.type == UNKNOWN: return ('Unknown')
  30. if self.type == HOME: return ('Home')
  31. if self.type == WORK: return ('Work')
  32. if self.type == FAX: return ('Fax')
  33. if self.type == CELL: return ('Cellular')
  34.  
  35. class phonedb:
  36. def __int__(self, dbname = 'phonedata'):
  37. self.dbname = dbname;
  38. self.shelve = shelve.open(self.dbname);
  39.  
  40. def __del__(self):
  41. self.shelve.close()
  42. self.shelve = None
  43.  
  44. def add(self, name, number, type = HOME):
  45. e = phoneentry(name, number, type)
  46. self.shelve[str(e)] = e
  47.  
  48. def lookup(self, string):
  49. list = []
  50. for key in self.shelve.keys():
  51. e = self.shelve[key]
  52. if cmp(e, string) == 0:
  53. list.append(e)
  54. return (list)
  55.  
  56. # if not being loaded as a module, run a small test
  57. if __name__ == '__main__':
  58. foo = phonedb()
  59. foo.add('Sean Reifschneider', '970-555-1111', HOME)
  60. foo.add('Sean Reifschneider', '970-555-2222', FAX)
  61. foo.add('Evelyn Mitchell', '970-555-1111', HOME)
  62.  
  63. print 'First lookup:'
  64.  
  65. for entry in foo.lookup('reifsch'):
  66. print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
  67.  
  68. print
  69.  
  70. print 'Second lookup:'
  71.  
  72. for entry in foo.lookup('e'):
  73. 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)
  1. import shelve, _bsddb
  2. filename = 'C:/Pyhton25/phonerecord.py'
  3.  
  4. def dele(name, ptype):
  5. key = name + ':' + ptype
  6. db = shelve.open(filename)
  7. try:
  8. try:
  9. del db[key]
  10. print 'Entry deleted.'
  11. except _bsddb.DBNotFoundError:
  12. print 'No Entries with', key, 'exist!'
  13. finally:
  14. db.close()
  15.  
  16. b = 1
  17. foo = phonedb()
  18. while b != 4:
  19. print
  20. print "Welcome to the Phone Database"
  21. print "Please choose from the following"
  22. print
  23. print "If you would like to display Phone Number(s) select 1"
  24. print
  25. print "If you would like to add an entry select 2"
  26. print
  27. print "If you would like to delete for an entry select 3"
  28. print
  29. print "To quit select 4 "
  30.  
  31. b = input(':')
  32. if b == 1:
  33. print 'Enter the lookup up key to find (For example for all names with ''Rob'' in them type Rob)'
  34. print
  35. a = raw_input(':')
  36.  
  37. for entry in foo.lookup(a):
  38. print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() )
  39. print
  40.  
  41. if b == 2:
  42. print "Please enter the full name: (Example: John Smith)"
  43. n = raw_input(':')
  44. print "Please enter the phone number: (Example: 970-432-5432)"
  45. p = raw_input(':')
  46. print "Please enter the phone type: (0 = Unkown, 1 = Home, 2 = Work, 3 = Fax, 4 = Cell)"
  47.  
  48. t = raw_input(':')
  49. if t == '0':
  50. foo.add(n, p, UNKNOWN)
  51. if t == '1':
  52. foo.add(n, p, HOME)
  53. if t == '2':
  54. foo.add(n, p, WORK)
  55. if t == '3':
  56. foo.add(n, p, FAX)
  57. if t == '4':
  58. foo.add(n, p, CELL)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
olufunkky is offline Offline
8 posts
since Apr 2007
Jun 16th, 2007
0

Re: Newbie- Need Assistance

You have mistyped __init__

It's __int__ in your phonedb class, look carefully...
Last edited by ffao; Jun 16th, 2007 at 4:27 pm. Reason: Fiddling with the smilies
Reputation Points: 20
Solved Threads: 5
Newbie Poster
ffao is offline Offline
15 posts
since May 2007
Jun 17th, 2007
0

Re: Newbie- Need Assistance

Click to Expand / Collapse  Quote originally posted by ffao ...
You have mistyped __init__

It's __int__ in your phonedb class, look carefully...
Thank you so much for pointing out this mistake. I have checked through several times and I didn't for once noticed it. I was so confused when I couldn't get the expected result, and didn't know what else to do, not knowing it was my mistake. Thank you once again. I almost gave up on the program. For those who might want to run something similar, below is the correct version of the program, I had tested it and it's running smoothly.

python Syntax (Toggle Plain Text)
  1. class phonedb:
  2. def __init__(self, dbname = 'phonedata'):
  3. self.dbname = dbname;
  4. self.shelve = shelve.open(self.dbname);
  5.  
  6. def __del__(self):
  7. self.shelve.close()
  8. self.shelve = None
  9.  
  10. def add(self, name, number, type = HOME):
  11. e = phoneentry(name, number, type)
  12. self.shelve[str(e)] = e
  13.  
  14. def lookup(self, string):
  15. list = []
  16. for key in self.shelve.keys():
  17. e = self.shelve[key]
  18. if cmp(e, string) == 0:
  19. list.append(e)
  20. return (list)
  21.  
  22. # if not being loaded as a module, run a small test
  23. if __name__ == '__main__':
  24. foo = phonedb()
  25. foo.add('Sean Reifschneider', '970-555-1111', HOME)
  26. foo.add('Sean Reifschneider', '970-555-2222', FAX)
  27. foo.add('Evelyn Mitchell', '970-555-1111', HOME)
  28.  
  29. print 'First lookup:'
  30.  
  31. for entry in foo.lookup('reifsch'):
  32. print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
  33.  
  34. print
  35.  
  36. print 'Second lookup:'
  37.  
  38. for entry in foo.lookup('e'):
  39. print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())
Reputation Points: 10
Solved Threads: 0
Newbie Poster
olufunkky is offline Offline
8 posts
since Apr 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: "Hosts script: Python"
Next Thread in Python Forum Timeline: Read Client Folder and more





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC