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
------------------------------------------
********************************************

Python面向对象编程 - 一个记事本程序范例(一)的更多相关文章

  1. Python面向对象编程 - 一个记事本程序范例(二)

    给程序加上控制台菜单 menu.py import sys from notebook import Notebook, Note class Menu: '''Display a menu and ...

  2. Python面向对象编程扑克牌发牌程序,另含大量Python代码!

    1. 题目 编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不 ...

  3. python面向对象编程进阶

    python面向对象编程进阶 一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 ...

  4. Python面向对象编程(下)

    本文主要通过几个实例介绍Python面向对象编程中的封装.继承.多态三大特性. 封装性 我们还是继续来看下上文中的例子,使用Student类创建一个对象,并修改对象的属性.代码如下: #-*- cod ...

  5. Python 面向对象编程基础

    Python 面向对象编程基础 虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到. 一类: 语法: class 类名: 类属性,方法 ...

  6. python面向对象编程学习

    python面向对象编程 基本概念理解 面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作 ...

  7. Python面向对象编程——继承与派生

    Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ...

  8. 图解python | 面向对象编程

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/56 本文地址:http://www.showmeai.tech/article-det ...

  9. python 面向对象编程学习

    1. 问题:将所有代码放入一个py文件:无法维护 方案:如果将代码才分放到多个py文件,好处: 1. 同一个名字的变量互相不影响 2.易于维护 3.引用模块: import module 2.包:解决 ...

随机推荐

  1. php的curl也没这么复杂

    许多同学在第一次使用curl的时候感觉一个头两个大(包括我在内),看着这一条条的curl_setopt函数完全摸不着头脑,不过在你花10分钟看了我的介绍后相信你以后也能轻松戏耍php的curl了 首先 ...

  2. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  3. ROS知识(9)----NodeHandle命令空间问题

    一直被NodleHandle的构造函数的命名空间搞混淆了.例如: ros::NodeHandle node_private("~/"); ros::NodeHandle node_ ...

  4. onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发

    onInterceptTouchEvent 与 onTouchEvent 分析与MotionEvent在ViewGroup与View中的分发        Notice:本文将紧接着 Android ...

  5. SQLite使用EF6的连接配置

    在配置文件中配置连接字符串 1. 使用nuget安装SQLite Install-Package System.Data.SQLite 安装好后的依赖项有: System.Data.SQLite.dl ...

  6. debian添加中文支持

    转载:http://www.shunix.com/debian-chinese-support-472/   debian与ubuntu有很大的相似性,但是debian相对更原始,比如在语言支持这一块 ...

  7. C#位运算符的基本用法

    位运算符包括:| 按位或 OR,& 按位与 AND,^ 按位异或 XOR,~ 取反 NOT,<< 左移 Left Shift,>> 右移 Right Shift,等等. ...

  8. datagrid在MVC中的运用05-加入时间搜索条件,枚举填充下拉框

    本文主要来体验在搜索区域增加更多的搜索条件,主要包括: ※ 使用jQuery ui的datepicker显示时间,设置显示格式.样式. ※ 设置jQuery ui的onClose事件,使开始和结束时间 ...

  9. 事物的隔离级别与并发完美体现了cap理论(确保数据完整、安全、一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)

    事物的隔离级别与并发完美体现了cap理论(确保数据完整.安全.一致性,在此基础上实现高性能访问(鱼和熊掌不可兼得)

  10. 使用Redis实现抢购的一种思路(list队列实现)

    原文:https://my.oschina.net/chinaxy/blog/1829233 抢购是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如 ...