在看一本书《PYTHON3 面向对象编程》

内容丰富,作作记录。

notebook.py

  1. __author__ = 'chengang882'
  2.  
  3. import datetime
  4.  
  5. # Store the next available id for all new note
  6. last_id = 0
  7.  
  8. class Note(object):
  9. """Represent a note in the notebook. Match against a
  10. string in searches and store tags for each note."""
  11.  
  12. def __init__(self, memo, tags=''):
  13. """initialize a note with memo and optional
  14. space-separated tags. Automatically set the note's
  15. creation date and a unique id."""
  16. self.memo = memo
  17. self.tags = tags
  18. self.creation_date = datetime.date.today()
  19. global last_id
  20. last_id += 1
  21. self.id = last_id
  22.  
  23. def match(self, filter):
  24. """Determine if this note matches the filter
  25. text. Return True if it matches, False otherwise.
  26. Search is case sensitive and matches both text and
  27. tags."""
  28. return filter in self.memo or filter in self.tags
  29.  
  30. class Notebook(object):
  31.  
  32. def __init__(self):
  33. self.notes = []
  34.  
  35. def new_note(self, memo, tags=''):
  36. self.notes.append(Note(memo, tags))
  37.  
  38. def _find_note(self, note_id):
  39. for note in self.notes:
  40. if str(note.id) == str(note_id):
  41. return note
  42. return None
  43.  
  44. def modify_memo(self, note_id, memo):
  45. note = self._find_note(note_id)
  46. if note:
  47. note.memo = memo
  48. return True
  49. return False
  50.  
  51. def modify_tags(self, note_id, tags):
  52. self._find_note(note_id).tags = tags
  53.  
  54. def search(self, filter):
  55. return [note for note in self.notes if
  56. note.match(filter)]

menu.py

  1. __author__ = 'chengang882'
  2.  
  3. import sys
  4. from notebook import Notebook, Note
  5.  
  6. class Menu:
  7. def __init__(self):
  8. self.notebook = Notebook()
  9. self.choices = {
  10. ": self.show_notes,
  11. ": self.search_notes,
  12. ": self.add_note,
  13. ": self.modify_note,
  14. ": self.quit
  15. }
  16.  
  17. def display_menu(self):
  18. print("""
  19. Notebook Menu
  20.  
  21. 1. Show all Notes
  22. 2. Search Notes
  23. 3. Add Note
  24. 4. Modify Note
  25. 5. Quit
  26. """)
  27.  
  28. def run(self):
  29. while True:
  30. self.display_menu()
  31. choice = raw_input("Enter an option: ")
  32. action = self.choices.get(str(choice))
  33. if action:
  34. action()
  35. else:
  36. print("{0} is note a valid choice".format(choice))
  37.  
  38. def show_notes(self, notes=None):
  39. if not notes:
  40. notes = self.notebook.notes
  41. for note in notes:
  42. print("{0}: {1}\n{2}".format(
  43. note.id, note.tags, note.memo))
  44.  
  45. def search_notes(self):
  46. filter = raw_input("Search for: ")
  47. notes = self.notebook.search(filter)
  48. self.show_notes(notes)
  49.  
  50. def add_note(self):
  51. memo = raw_input("Enter a memo: ")
  52. print(memo)
  53. self.notebook.new_note(memo)
  54. print("Your note has been added.")
  55.  
  56. def modify_note(self):
  57. id = raw_input("Enter a note id: ")
  58. memo = raw_input("Enter a memo: ")
  59. tags = raw_input("Enter tags: ")
  60. if memo:
  61. self.notebook.modify_memo(id, memo)
  62. if tags:
  63. self.notebook.modify_tags(id, tags)
  64.  
  65. def quit(self):
  66. print("Thank you for using your notebook today.")
  67. sys.exit(0)
  68.  
  69. if __name__ == "__main__":
  70. Menu().run()

基于类的命令行notebook的实现的更多相关文章

  1. 基于Python与命令行人脸识别项目(系列一)

    Face Recognition 人脸识别 摘要:本项目face_recognition是一个强大.简单.易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,方便大家使用.对于本项目可以使用 ...

  2. xsd、wsdl生成C#类的命令行工具使用方法

    1.xsd生成C#类命令 示例:xsd <xsd文件路径> /c /o:<生成CS文件目录> <其他参数> 参数说明: /c 生成为cs文件,/d 生成DataSe ...

  3. 基于Python与命令行人脸识别项目(系列二)

    接着系统一,继续开始我们face_recognition. Python 模块:face_recognition 在Python中,你可以导入face_recognition模块,调用丰富的API接口 ...

  4. (转载)基于Bash命令行的百度云上传下载工具

    原文链接:http://hi.baidu.com/meoow/item/aef5814bbd5be3e1bcf451e9 这是我根据百度云PCS的API写的一个基于bash的命令行工具, 使用了cur ...

  5. 命令行中运行Java字节码文件提示找不到或无法加载主类的问题

    测试类在命令行操作,编译通过,运行时,提示 错误: 找不到或无法加载主类 java类 package com.company.schoolExercise; public class test7_3_ ...

  6. 一个简单、易用的Python命令行(terminal)进度条库

    eprogress 是一个简单.易用的基于Python3的命令行(terminal)进度条库,可以自由选择使用单行显示.多行显示进度条或转圈加载方式,也可以混合使用. 示例 单行进度条 多行进度条 圆 ...

  7. Mac命令行

    参考:http://www.cnblogs.com/-ios/p/4949923.html 必读 涵盖范围: 这篇文章对刚接触命令行的新手以及具有命令行使用经验的人都有用处.本文致力于做到覆盖面广(尽 ...

  8. FreeSql.Generator命令行代码生成器是如何实现的

    目录 FreeSql介绍 FreeSql.Generator RazorEngine.NetCore 源码解析 FreeSql.Tools FreeSql FreeSql 是功能强大的对象关系映射技术 ...

  9. Go通过cobra快速构建命令行应用

    来自jetbrains Go 语言现状调查报告 显示:在go开发者中使用go开发实用小程序的比例为31%仅次于web,go得益于跨平台.无依赖的特性,用来编写命令行或系统管理这类小程序非常不错. 本文 ...

随机推荐

  1. java-生成印章swing

    案例1 package com; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import j ...

  2. iOS多线程的详情使用示例--简进祥

    大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操作只能 ...

  3. FastJson的简单实用

    一.FastJson的理解 在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON ...

  4. CFBundleVersion与CFBundleShortVersionString,上架注意事项

    CFBundleVersion,标识(发布或未发布)的内部版本号.这是一个单调增加的字符串,包括一个或多个时期分隔的整数. CFBundleShortVersionString  标识应用程序的发布版 ...

  5. Java学习笔记10

    31.编写当年龄age大于13且小于18时结果为true的布尔表达式age > 13 && age < 18 32.编写当体重weight大于50或身高大于160时结果为t ...

  6. Node.js入门笔记(1):基本概念

    Node.js和JavaScript: 核心都是ECMAScrit,比如数据类型,语法结构,内置对象等等. 但是在js中顶层是window 在node中的不存在这个window(console.log ...

  7. ES5语法

    ES5新语法主要是体现在Object和.Array操作,同时涉及到JSON. Function.Date 和 String类型上. 1.Object ES5最大的特点是对象扩展很多方法. 新建对象:c ...

  8. 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 ...

  9. JSP脚本中的9个内置对象

    JSP脚本中包含9个内置对象,这9个内置对象都是Servlet API接口的实例,只是JSP规范对它们进行了默认初始化.也就是说它们已经是对象,可以直接使用. 我们随意打开一个由JSP页面生成的Ser ...

  10. jquery选择器(总结)

    基本选择器 选择器 描述 示例 #id 根据给定的ID匹配一个元素 $("#id")   .class 根据制定的class匹配一个元素 $(".class") ...