zoukankan      html  css  js  c++  java
  • Python面向对象编程

    notebook.py

    import datetime
    
    last_id = 0
    
    class Note:
        '''Represent a note in the notebook. Match against a
        string in searches and store tags for each note.'''
    
        def __init__(self, memo, tags=''):
            self.memo = memo
            self.tags = tags
            self.creation_date = datetime.date.today()
            global last_id
            last_id += 1
            self.id = last_id
    
        def match(self, filter):
            return filter in self.memo or filter in self.tags
    
    class Notebook:
        '''Represent a collection of notes that can be tagged,
        modified, and searched.'''
    
        def __init__(self):
            '''Initialize a notebook with an empty list.'''
            self.notes = []
    
        def new_note(self, memo, tags=''):
            '''Create a new note and add it to the list.'''
            self.notes.append(Note(memo, tags))
    
        def _find_note(self, note_id):
            '''Locate the note with the given id.'''
            for note in self.notes:
                if note.id == note_id:
                    return note
            return None
    
        def modify_memo(self, note_id, memo):
            '''Find the note with the given id and change its
            memo to the given value.'''
            self._find_note(note_id).memo = memo
    
        def modify_tags(self, note_id, tags):
            '''Find the note with the given id and change its
            tags to the given value.'''
            self._find_note(note_id).tags = tags
    
        def search(self, filter):
            '''Find all notes that match the given filter
            string.'''
            return [note for note in self.notes if
                    note.match(filter)]

    note_book_test.py

    from notebook import Note, Notebook
    
    def display_notes(notes):
        for note in notes:
            print("Id: %d" %(note.id))
            print("Memo: %s" %(note.memo))
            print("------------------------------------------")
    
    n = Notebook()
    n.new_note("hello world")
    n.new_note("hello again")
    print(n.notes)
    display_notes(n.notes)
    
    print("********************************************")
    print("search keyword: hello")
    search_notes = n.search("hello")
    display_notes(search_notes)
    print("********************************************")
    
    print("********************************************")
    print("search keyword: world")
    search_notes = n.search("world")
    display_notes(search_notes)
    print("********************************************")
    
    print("********************************************")
    print("after modify note 1:")
    n.modify_memo(1, "Hi Master HaKu")
    display_notes(n.notes)
    print("********************************************")

    运行结果:

    [<notebook.Note object at 0x02C40E70>, <notebook.Note object at 0x02C40830>]
    Id: 1
    Memo: hello world
    ------------------------------------------
    Id: 2
    Memo: hello again
    ------------------------------------------
    ********************************************
    search keyword: hello
    Id: 1
    Memo: hello world
    ------------------------------------------
    Id: 2
    Memo: hello again
    ------------------------------------------
    ********************************************
    ********************************************
    search keyword: world
    Id: 1
    Memo: hello world
    ------------------------------------------
    ********************************************
    ********************************************
    after modify note 1:
    Id: 1
    Memo: Hi Master HaKu
    ------------------------------------------
    Id: 2
    Memo: hello again
    ------------------------------------------
    ********************************************

  • 相关阅读:
    4.3.4 查询语法基础(2)
    4.3.4 查询语法基础(1)
    一个存储过程调用另一个存储过程 (2009-12-29 13:57:05)
    4.3.3 基于行的操作
    4.3.2 基于集合的操作
    4.3 数据操纵语言(DML)
    4.2 从哪儿开始
    子查询、联合查询和连接查询
    4.1 SQL的本质
    本书说明
  • 原文地址:https://www.cnblogs.com/davidgu/p/4855413.html
Copyright © 2011-2022 走看看