基于类的命令行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篇
博客园很早就开通了,当时下决心要把自己的经验心得记录上去,但是却没有做到,因为一直觉得自己搞得东西可能还是比较的初级,感觉拿不出手,所以也就是只是把它记录在在印象笔记上面(三年下来,还是整理和收藏了一 ...
- PyQt 自定义信号带参数
import sys from PyQt5.QtCore import pyqtSignal, QObject from PyQt5.QtWidgets import QMainWindow, QAp ...
- AOJ DSL_2_A Range Minimum Query (RMQ)
Range Minimum Query (RMQ) Write a program which manipulates a sequence A = {a0,a1,...,an−1} with the ...
- 分布式服务框架dubbo原理解析(转)
libaba有好几个分布式框架,主要有:进行远程调用(类似于RMI的这种远程调用)的(dubbo.hsf),jms消息服务(napoli.notify),KV数据库(tair)等.这个框架/工具/产品 ...
- 【Codeforces710F】String Set Queries (强制在线)AC自动机 + 二进制分组
F. String Set Queries time limit per test:3 seconds memory limit per test:768 megabytes input:standa ...
- Linux 查找文件并删除文件内容
find * ./ |while read file; do echo ''>$file; done
- ubuntu 下简单录音
找了半天录音工具,甚至都在尝试用 pyAudio 自己写了,结果发现,原来有现成命令行工具用! 就是 sox 工具包.这个工具包有 4 个工具:sox, play, rec, soxi.rec 和 p ...
- 轻松理解spring IOC
spring IOC(Inversion of control)即控制反转 概念:一,spring框架的核心之一 二,控制权由对象本身转向容器:由容器根据配置文件去创建实例并创建各个实例之间的依赖关系 ...
- [Nhibernate]SchemaExport工具的使用(二)——创建表及其约束、存储过程、视图
目录 写在前面 文档与系列文章 表及其约束 存储过程 视图 总结 写在前面 由于一直在山西出差,有几天没更新博客了.昨晚回到家,将博客园最近三天更新的文章搜集了一下,花费了半天的时间,看了看,有些文章 ...
- mysql安装使用笔记
mysql2008年被sun公司10亿美元收购, 后sun被oracle收购. widenius : 维德纽斯重新写的mysql的分支 mariaDB. 白发程序员, 是由 瑞典mysql AB公司开 ...