基于类的命令行notebook的实现

简介: 在看一本书《PYTHON3 面向对象编程》 内容丰富,作作记录。 notebook.py __author__ = 'chengang882' import datetime # Store the next available id for all new note las...

在看一本书《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 = {
            "1": self.show_notes,
            "2": self.search_notes,
            "3": self.add_note,
            "4": self.modify_note,
            "5": 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()

目录
相关文章
|
前端开发 JavaScript
Jupyter Notebook自动补全代码配置
Jupyter Notebook自动补全代码配置
1112 0
Jupyter Notebook自动补全代码配置
|
9月前
|
Shell Linux 文件存储
Jupyter notebook安装运行(详解)
Jupyter notebook安装运行(详解)
|
10月前
|
Python
Jupyter-notebook 常用魔法命令
本文分享了一些常用到的 Jupyter-notebook 软件的快捷键以及 魔法命令,以供学习
125 0
|
11月前
|
Linux iOS开发 MacOS
一日一技:你不知道的PyCharm命令行用法
一日一技:你不知道的PyCharm命令行用法
184 0
|
Python
Pycharm配置运行参数
Pycharm配置运行参数
861 0
Pycharm配置运行参数
|
Python
Python编程:jupyter notebook安装启动
Python编程:jupyter notebook安装启动
Python编程:jupyter notebook安装启动
|
Windows
【最佳实践】修改Anaconda中的Jupyter Notebook默认工作路径
打开Anaconda安装目录下的etc文件如:C:\Anaconda3\etc\jupyter我的安装目录在C:\Anaconda3
202 1
|
Shell Linux 开发工具
一日一技:如何从多个Jupyter Notebook中找到需要代码段
一日一技:如何从多个Jupyter Notebook中找到需要代码段
493 0
一日一技:如何从多个Jupyter Notebook中找到需要代码段
|
Shell Linux 开发工具
notebook安装、运行及使用(上)
notebook安装、运行及使用如果你的主页面里边的文件夹跟我的不同,或者你在疑惑为什么首次启动里边就已经有这么多文件夹,不要担心,这里边的文件夹全都是你的家目录里的目录文件。你可以在终端中执行以下2步来查看:
764 0
|
Linux iOS开发 MacOS
notebook安装、运行及使用(下)
notebook安装、运行及使用如果你的主页面里边的文件夹跟我的不同,或者你在疑惑为什么首次启动里边就已经有这么多文件夹
424 0