基于类的命令行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得益于跨平台.无依赖的特性,用来编写命令行或系统管理这类小程序非常不错. 本文 ...
随机推荐
- c++调用lua环境配置
安装完luaforwindows后进行配置 配置项目属性
- 【BZOJ-3195】奇怪的道路 状压DP (好题!)
3195: [Jxoi2012]奇怪的道路 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: 184[Submit][Statu ...
- noip2016十连测round1
A:String Master 题目:所谓最长公共子串,比如串 A:"abcde",串 B:"jcdkl",则它们的最长公共子串为串 "cd" ...
- Mac下Intellij IDEA Console中文是?
Mac下Intellij IDEA Console中文是?,解决办法是在运行时的VM参数配置也加入: -Dfile.encoding=UTF-8
- 求n!质因数分解之后素数a的个数
n!质因数分解后P的个数=n/p+n/(p*p)+n/(p*p*p)+......直到n<p*p*p*...*p //主要代码,就这么点东西,数学真是厉害啊!幸亏我早早的就退了数学2333 do ...
- json使用
json是存储和交换文本信息的语法,类似 XML,比 XML 更小.更快,更易解析 json 可通过 JavaScript 进行解析 json 数据可使用 AJAX 进行传输 //读取var JSON ...
- jvm垃圾回收机制
http://blog.csdn.net/zsuguangh/article/details/6429592 原文地址
- YII2生成增删改查
下载完成后在basic/db.php配置数据库参数. 1.配置虚拟主机后进入YII入口文件 index.php 进行get传值 ?r=gii ,进入创建界面 2.点击 Model Generator下 ...
- SDL 显示解码后的yuv12数据
在上篇h264解码为yuv12后http://jhlong12345.blog.163.com/blog/static/1230631292015725115058709/ ,需要显示出来,使用sdl ...
- webpack入坑之旅
转自: http://guowenfh.github.io/2016/03/24/vue-webpack-01-base/ http://guowenfh.github.io/2016/03/25/v ...