0

我是编程新手,并且有一项我一直在工作的任务。我了解定义功能和许多基础知识,但此时我有点碰壁了。

我正试图弄清楚这一点,但还不真正理解“类”功能是如何工作的。我将不胜感激这方面的任何帮助;对一些 python 资源的任何帮助也可以降低如何/为什么使用类。


一段时间以来,您一直在工作中从事数据库项目。你的老板鼓励你用 Python 对数据库进行编程。您不同意,认为 Python 不是数据库语言,但您的老板坚持提供以下示例电话数据库的源代码。

他要求你做两件事:

  1. 评估现有的源代码并对其进行扩展,使其对公司的经理有用。(您不需要 GUI 界面,只需处理数据库方面的工作:数据输入和检索 - 当然您必须让程序运行或正常工作

  2. 他希望你批判性地评估 Python 作为数据库工具。


将下面的示例代码导入 Python IDLE 并对其进行增强、运行和调试。通过提供简单的数据输入和检索功能,使其成为更现实的数据库工具。

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() )

我不确定我是否走在正确的轨道上,但这是我目前所拥有的:

def openPB():
    foo = phonedb()
    print 'Please select an option:'
    print '1 - Lookup'
    print '2 - Add'
    print '3 - Delete'
    print '4 - Quit'
    entry=int(raw_input('>> '))
    if entry==1:
            namelookup=raw_input('Please enter a name: ')
            for entry in foo.lookup(namelookup):
                    print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype() )
    elif entry==2:
            name=raw_input('Name: ')
            number=raw_input('Number: ')
            showtype=input('Type (UNKNOWN, HOME, WORK, FAX, CELL): \n>> ')
            for entry in foo.add(name, number, showtype):           #Trying to figure out this part
                    print '%-40s %s (%s)'% (entry.name, entry.number, entry.showtype() )
    elif entry==3:
            delname=raw_input('Please enter a name to delete: ')
            #                                                       #Trying to figure out this part
            print "Contact '%s' has been deleted" (delname)
    elif entry==4:
            print "Phone book is now closed"
            quit
    else:
            print "Your entry was not recognized."
            openPB()

openPB()
4

1 回答 1

0

Learn Python the Hard WayDive into Python和内置的Python 教程对于开始学习 Python 的人来说都是非常好的资源。我开始学习时也使用了Beginning Python 。

于 2012-09-08T17:03:23.737 回答