基于类的命令行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得益于跨平台.无依赖的特性,用来编写命令行或系统管理这类小程序非常不错. 本文 ...
随机推荐
- java-生成印章swing
案例1 package com; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import j ...
- iOS多线程的详情使用示例--简进祥
大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操作只能 ...
- FastJson的简单实用
一.FastJson的理解 在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON ...
- CFBundleVersion与CFBundleShortVersionString,上架注意事项
CFBundleVersion,标识(发布或未发布)的内部版本号.这是一个单调增加的字符串,包括一个或多个时期分隔的整数. CFBundleShortVersionString 标识应用程序的发布版 ...
- Java学习笔记10
31.编写当年龄age大于13且小于18时结果为true的布尔表达式age > 13 && age < 18 32.编写当体重weight大于50或身高大于160时结果为t ...
- Node.js入门笔记(1):基本概念
Node.js和JavaScript: 核心都是ECMAScrit,比如数据类型,语法结构,内置对象等等. 但是在js中顶层是window 在node中的不存在这个window(console.log ...
- ES5语法
ES5新语法主要是体现在Object和.Array操作,同时涉及到JSON. Function.Date 和 String类型上. 1.Object ES5最大的特点是对象扩展很多方法. 新建对象:c ...
- 2012-2013 ACM-ICPC Northeastern European Regional Contest (NEERC 12)
Problems # Name A Addictive Bubbles1 addictive.in / addictive.out 2 s, 256 MB x438 B Blin ...
- JSP脚本中的9个内置对象
JSP脚本中包含9个内置对象,这9个内置对象都是Servlet API接口的实例,只是JSP规范对它们进行了默认初始化.也就是说它们已经是对象,可以直接使用. 我们随意打开一个由JSP页面生成的Ser ...
- jquery选择器(总结)
基本选择器 选择器 描述 示例 #id 根据给定的ID匹配一个元素 $("#id") .class 根据制定的class匹配一个元素 $(".class") ...