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. 【失踪人口回归】第11届东北地区大学生程序设计竞赛——Time to make some change

    对哈尔滨出租车和纸质题目和2148473647的吐槽都被毕克神牛在知乎上(https://www.zhihu.com/question/59782275/answer/169402588)pick/b ...

  2. Codeforces Round #359 (Div. 1) A. Robbers' watch 暴力

    A. Robbers' watch 题目连接: http://www.codeforces.com/contest/685/problem/A Description Robbers, who att ...

  3. ThinkPHP实现登录限制时__construct和_initialize的区别

    ThinkPHP支持两种构造方法:  __construct和_initialize(ThinkPHP内置的构造方法). 测试URL为:  http://oa.com/index.php/Admin/ ...

  4. Duplicate Elimination in Scrapy(转)

    之前介绍 Scrapy 的时候提过 Spider Trap ,实际上,就算是正常的网络拓扑,也是很复杂的相互链接,虽然我当时给的那个例子对于我感兴趣的内容是可以有一个线性顺序依次爬下来的,但是这样的情 ...

  5. OBD-II Protocol -- SAE J1850 VPW PWM

    http://www.auto-diagnostics.info/j1850 j1850 The SAE J1850 bus bus is used for diagnostics and data ...

  6. .NET快速查找某个类所在的命名空间

    有时候我们从网上copy别人的代码下来,对于某些不熟悉的类,需要添加对某个类的引用时,如何快速找出某个类所在的命名空间呢 例如有如下的一段代码: 现在要添加ConfigurationElement类的 ...

  7. python笔记28-lxml.etree爬取html内容

    前言 本篇继续lxml.etree学习,在线访问接口,通过接口返回的html,解析出想要的text文本内容 环境准备: python 3.6 lxml requets 定位目标 爬取我的博客首页htt ...

  8. mysql错误:this authentication plugin is not supported

    this authentication plugin is not supported 应用程序连接mysql docker一直报错:this authentication plugin is not ...

  9. [翻译] 极具动感的 FRDLivelyButton

    FRDLivelyButton https://github.com/sebastienwindal/FRDLivelyButton FRDLivelyButton is a simple UIBut ...

  10. Android 系统服务一览表

    在<Zygote进程[3]--SystemServer的诞生>一文中介绍了SystemServer的诞生,本文来看一下SystemServer中初始化的系统服务. 1.AccountMan ...