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
------------------------------------------
********************************************
相关文章
- 91个改善Python程序的小技巧(转载)
- Python精确指南——第二章 界面开发
- Linux 下用 crontab 设置定时执行python 程序
- 使用Python的PIL模块来进行图片对比
- 如何用 Python 写一个简易的抽奖程序
- Python - 使用Setuptools进行程序打包
- Python: 爬虫入门-python爬虫入门教程(非常详细)
- 【Python基础】python爬虫之异步网络爬虫ǃ
- Python编程语言学习:python中与数字相关的函数(取整等)、案例应用之详细攻略
- 使用 VS Code 远程调试 Python 程序
- Python:更改默认启动的python程序及其对应的安装包路径(更改pip的默认安装包的路径)图文教程之详细攻略
- Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法、如何查询某个函数&关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python:pmml格式文件的简介、安装、使用方法(利用python将机器学习模型转为Java常用的pmml格式文件)之详细攻略
- python:程序猿的方式在元宵佳节之际,带你设计【东方明珠】动画浪漫烟花秀(python编程利用Tkinter+PIL库)
- Python语言学习之字符串那些事:python和字符串的使用方法之详细攻略
- 已解决2.Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and wi
- 已解决2. Set PROTOCOL_BUPFERS_PYTHON_iMPLEMENTATION=python (but this will use pure-Python parsing and w
- 是什么让我学python?不是因为穷,是因为有意思
- 〖Python自动化办公篇㉑〗- python实现邮件自动化 - 定时发送邮件
- Python编程:json序列化python对象
- Python重写C语言程序100例--Part6
- python web py入门(18)- 用javascript写第一个Hello World程序
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了)转