Lost, Python Programming

Import the sample code below into the Python IDLE and enhance it, run it and debug it. Add features to make this a more realistic database tool by providing for easy data entry and retrieval. Export your successful program to a Python file for later upload to coursenet.

Here is the sample source code:

# An example from Sean Reifschneider''s Python Tutorial at Linux Expo 98.
# Copyright (c) 1998 Sean Reifschneider, tummy.com, ltd.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# You can contact Sean Reifschneider at
# P.O. Box 270624,
# Fort Collins, CO USA 80527-0624,
# or at jafo-gpl@tummy.com
# Simple phone-number database module

import shelve
import string

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.lower(str(self))
that = string.lower(that)

if string.find(this, that) >= 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 __init__(self, dbname = ''phonedata''):
self.dbname = dbname;
self.shelve = shelve.open(self.dbname);

def __del__(self):
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:


# 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'', CELL)
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() )

Posted Date: 1/11/2013 9:45:01 AM | Location : Afghanistan

Related Discussions:- Lost, Assignment Help, Ask Question on Lost, Get Answer, Expert's Help, Lost Discussions

Write discussion on Lost
Your posts are moderated
Related Questions
how to calculate standard deviations in programming

Programming models Just  as there  are several methods for organizing entire  software systems, there  are  different strategies for formally expressing computational processe

1 Low Level Exploits 1.1 Savegames Jimmy is becoming increasingly frustrated at the computer game hes playing. He has a save right before the levels boss but he needs either more

Write an algorithm for a program that receives, as input from the user, 2 string variables and 2 integer variables; then joins together and displays the combined strings; and final

draw the variation of pressure with volume of a real gas at temperatures lower than its critical temperature.also draw its surface of discontinuity

List mutation and shared structure Lists are mutable data  structures, which defines  that  we can actually modifies  the values  stored in their components. We do this by usi

We encourage you to adopt an interactive style of debugging and programming. Use the Python shell a lot. Write short pieces of code and check them.  It is much  simpler to test the

There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many

Worked example 1   Let's examine what  happens when  we compute the following Python code:   def p(x, y): z = x*x - y return z + 1/z   >>> p(1.0, 2.0) -2.0