Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

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:

#!/usr/bin/python
#
# 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
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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

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.lower(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 __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'', 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

print ''Second lookup:''
for entry in foo.lookup(''e''):
print ''%-40s %s (%s)'' % ( entry.name, entry.number, entry.showtype() )

Python, Programming

  • Category:- Python
  • Reference No.:- M9517565

Have any Question?


Related Questions in Python

In this programming assignment you will write a client

In this programming assignment, you will write a client pingprogram in Python. Your client will send a simple ping message to a server, receive a correspondingpong message back from the server, and determine the delay be ...

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

A software company sells a package that retails for 99

A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program using python t ...

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

Environment setupthe first mini project will be based on

Environment Setup The first mini project will be based on Ladder Logic programming. We will be using Schneider Electric's IDE called SoMachine Basic to do the programming. The latest ver- sion of SoMachine Basic for Wind ...

Question write a python program with a graphical user

Question: Write a python program with a graphical user interface that will allow a user to create a custom pizza which they wish to order. At minimum, the user should be able to choose the size of the pizza, the type of ...

Sieve of eratosthenes in pythonthe goal is to find all the

Sieve of Eratosthenes (in Python) The goal is to find all the prime numbers less than or equal to some natural number maxn. We have a list that tells us if any of the numbers 0..maxn are "marked". It can be an array of b ...

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As