python记事本编程_Python面向对象编程 - 一个记事本程序范例(一)

本文介绍了一个使用Python实现的记事本程序,涵盖了Note类和Notebook类的设计,包括创建、修改笔记及搜索功能。通过示例展示了如何创建、修改和搜索包含特定关键词的笔记。
摘要由CSDN通过智能技术生成

notebook.py

importdatetime

last_id=0classNote:‘‘‘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()globallast_id

last_id+= 1self.id=last_iddefmatch(self, filter):return filter in self.memo or filter inself.tagsclassNotebook:‘‘‘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 inself.notes:if note.id ==note_id:returnnotereturnNonedefmodify_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=memodefmodify_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=tagsdefsearch(self, filter):‘‘‘Find all notes that match the given filter

string.‘‘‘

return [note for note in self.notes ifnote.match(filter)]

note_book_test.py

from notebook importNote, Notebookdefdisplay_notes(notes):for note innotes: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("********************************************")

运行结果:

[, ]

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

------------------------------------------

********************************************

原文:http://www.cnblogs.com/davidgu/p/4855413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值