基于类的命令行notebook的实现
在看一本书《PYTHON3 面向对象编程》
内容丰富,作作记录。
notebook.py
__author__ = 'chengang882'
import datetime
# Store the next available id for all new note
last_id = 0
class Note(object):
"""Represent a note in the notebook. Match against a
string in searches and store tags for each note."""
def __init__(self, memo, tags=''):
"""initialize a note with memo and optional
space-separated tags. Automatically set the note's
creation date and a unique id."""
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):
"""Determine if this note matches the filter
text. Return True if it matches, False otherwise.
Search is case sensitive and matches both text and
tags."""
return filter in self.memo or filter in self.tags
class Notebook(object):
def __init__(self):
self.notes = []
def new_note(self, memo, tags=''):
self.notes.append(Note(memo, tags))
def _find_note(self, note_id):
for note in self.notes:
if str(note.id) == str(note_id):
return note
return None
def modify_memo(self, note_id, memo):
note = self._find_note(note_id)
if note:
note.memo = memo
return True
return False
def modify_tags(self, note_id, tags):
self._find_note(note_id).tags = tags
def search(self, filter):
return [note for note in self.notes if
note.match(filter)]
menu.py
__author__ = 'chengang882'
import sys
from notebook import Notebook, Note
class Menu:
def __init__(self):
self.notebook = Notebook()
self.choices = {
": self.show_notes,
": self.search_notes,
": self.add_note,
": self.modify_note,
": self.quit
}
def display_menu(self):
print("""
Notebook Menu
1. Show all Notes
2. Search Notes
3. Add Note
4. Modify Note
5. Quit
""")
def run(self):
while True:
self.display_menu()
choice = raw_input("Enter an option: ")
action = self.choices.get(str(choice))
if action:
action()
else:
print("{0} is note a valid choice".format(choice))
def show_notes(self, notes=None):
if not notes:
notes = self.notebook.notes
for note in notes:
print("{0}: {1}\n{2}".format(
note.id, note.tags, note.memo))
def search_notes(self):
filter = raw_input("Search for: ")
notes = self.notebook.search(filter)
self.show_notes(notes)
def add_note(self):
memo = raw_input("Enter a memo: ")
print(memo)
self.notebook.new_note(memo)
print("Your note has been added.")
def modify_note(self):
id = raw_input("Enter a note id: ")
memo = raw_input("Enter a memo: ")
tags = raw_input("Enter tags: ")
if memo:
self.notebook.modify_memo(id, memo)
if tags:
self.notebook.modify_tags(id, tags)
def quit(self):
print("Thank you for using your notebook today.")
sys.exit(0)
if __name__ == "__main__":
Menu().run()

基于类的命令行notebook的实现的更多相关文章
- 基于Python与命令行人脸识别项目(系列一)
Face Recognition 人脸识别 摘要:本项目face_recognition是一个强大.简单.易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,方便大家使用.对于本项目可以使用 ...
- xsd、wsdl生成C#类的命令行工具使用方法
1.xsd生成C#类命令 示例:xsd <xsd文件路径> /c /o:<生成CS文件目录> <其他参数> 参数说明: /c 生成为cs文件,/d 生成DataSe ...
- 基于Python与命令行人脸识别项目(系列二)
接着系统一,继续开始我们face_recognition. Python 模块:face_recognition 在Python中,你可以导入face_recognition模块,调用丰富的API接口 ...
- (转载)基于Bash命令行的百度云上传下载工具
原文链接:http://hi.baidu.com/meoow/item/aef5814bbd5be3e1bcf451e9 这是我根据百度云PCS的API写的一个基于bash的命令行工具, 使用了cur ...
- 命令行中运行Java字节码文件提示找不到或无法加载主类的问题
测试类在命令行操作,编译通过,运行时,提示 错误: 找不到或无法加载主类 java类 package com.company.schoolExercise; public class test7_3_ ...
- 一个简单、易用的Python命令行(terminal)进度条库
eprogress 是一个简单.易用的基于Python3的命令行(terminal)进度条库,可以自由选择使用单行显示.多行显示进度条或转圈加载方式,也可以混合使用. 示例 单行进度条 多行进度条 圆 ...
- Mac命令行
参考:http://www.cnblogs.com/-ios/p/4949923.html 必读 涵盖范围: 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处.本文致力于做到覆盖面广(尽 ...
- FreeSql.Generator命令行代码生成器是如何实现的
目录 FreeSql介绍 FreeSql.Generator RazorEngine.NetCore 源码解析 FreeSql.Tools FreeSql FreeSql 是功能强大的对象关系映射技术 ...
- Go通过cobra快速构建命令行应用
来自jetbrains Go 语言现状调查报告 显示:在go开发者中使用go开发实用小程序的比例为31%仅次于web,go得益于跨平台.无依赖的特性,用来编写命令行或系统管理这类小程序非常不错. 本文 ...
随机推荐
- JS中的进制转换以及作用
js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: //10进制转为16进制 ().toString() // =>&q ...
- UIDynamic-附着行为:UIAttachmentBehavior
直接上代码: // // YFAttachmentBehaviorViewController.m // BigShow1949 // // Created by apple on 16/8/25. ...
- C#中的try catch 和finally
错误的出现并不总是编写应用程序的人的原因,有时应用程序会因为终端用户的操作而发生错误.无论如何,我们都应预测应用程序和代码中出现的错误. 这三个关键字try是必定要用的,要不然就失去了意义.然后cat ...
- eclipse中设置文件默认打开方式
- Bzoj1208 [HNOI2004]宠物收养所
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 7457 Solved: 2960 Description 最近,阿Q开了一间宠物收养所.收养所提供两 ...
- edit界面初始化加默认值
功能名称:modelCreateAction 切入类型:after 事件名称:com.kingdee.bos.eventbus.event.model.ModelCreateEvent package ...
- WampServer 的phpmyadmin数据
WampServer首次安装的时候phpmyadmin的密码是为空 设置密码 1.安装成功后,通过 phpmyadmin 进入mysql,点击上面的 [用户] 菜单,在用户[root]主机[local ...
- 开放数据库互联ODBC配置(odbcconf)
开放数据库互连(ODBC)是微软引进的一种早期数据库接口技术,通过ODBC驱动程序可访问数据库数据:使用ODBC管理器可以完成对数据库的链接操作.笔者利用ODBC接口,将WINDOWS计数器信息写入到 ...
- 项目里面的某个资源文件(比如plist、音频等)无法使用
检查:Build Phases -> Copy Bundle Resources
- 前端开发者进阶之函数柯里化Currying
穆乙:http://www.cnblogs.com/pigtail/p/3447660.html 在计算机科学中,柯里化(英语:Currying),又译为卡瑞化或加里化,是把接受多个参数的函数变换成接 ...