Looking at a book, "PYTHON3 Object-oriented programming"
Rich in content, as a record.
notebook.py
__author__='chengang882'Importdatetime#Store the next available ID for all new notelast_id =0classNote (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 ()Globallast_id last_id+ = 1self.id=last_iddefMatch (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.""" returnFilterinchSelf.memoorFilterinchSelf.tagsclassNotebook (object):def __init__(self): self.notes= [] defNew_note (self, memo, tags="'): Self.notes.append (Note (memo, tags))def_find_note (Self, note_id): forNoteinchself.notes:ifSTR (note.id) = =Str (note_id):returnNotereturnNonedefModify_memo (self, note_id, memo): Note=self._find_note (note_id)ifNote:note.memo=MemoreturnTruereturnFalsedefmodify_tags (self, note_id, tags): self._find_note (note_id). Tags=TagsdefSearch (self, filter):return[Note forNoteinchSelf.notesifNote.match (filter)]
menu.py
__author__='chengang882'ImportSYS fromNotebookImportNotebook, NoteclassMenu: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}defDisplay_menu (self):Print("""Notebook Menu 1. Show all Notes 2. Search Notes 3. ADD Note 4. Modify Note 5. Quit""") defRun (self): whileTrue:self.display_menu () Choice= Raw_input ("Enter an option:") Action=Self.choices.get (str (choice))ifaction:action ()Else: Print("{0} is note a valid choice". Format (choice))defShow_notes (Self, notes=None):if notnotes:notes=self.notebook.notes forNoteinchnotes:Print("{0}: {1}\n{2}". Format (note.id, Note.tags, Note.memo))defsearch_notes (self): filter= Raw_input ("Search for:") Notes=Self.notebook.search (Filter) self.show_notes (notes)defadd_note (self): Memo= Raw_input ("Enter a memo:") Print(Memo) Self.notebook.new_note (Memo )Print("Your Note has been added.") defmodify_note (self): ID= Raw_input ("Enter a note ID:") Memo= Raw_input ("Enter a memo:") Tags= Raw_input ("Enter Tags:") ifMemo:self.notebook.modify_memo (ID, Memo)iftags:self.notebook.modify_tags (ID, tags)defQuit (self):Print("Thank for using your notebook today.") sys.exit (0)if __name__=="__main__": Menu (). Run ()
Implementation of class-based command-line notebook