Objects in Python(Chapter 2 of Python 3 Object Oriented Programming)
时间:2010-08-31 来源:Ray Z
notebook
1 import datetime
2
3 last_id = 0
4
5 class Note:
6
7 def __init__(self, memo, tags=''):
8 self.memo = memo
9 self.tags = tags
10 self.creation_date = datetime.date.today()
11 global last_id
12 last_id += 1
13 self.id = last_id
14
15 def match(self, filter):
16 return filter in self.memo or filter in self.tags
17
18 class Notebook:
19
20 def __init__(self):
21 self.notes = []
22
23 def new_note(self, memo, tags=''):
24 self.notes.append(Note(memo, tags))
25
26 def _find_note(self, note_id):
27 for note in self.notes:
28 if note.id == str(note_id):
29 return note
30 return None
31
32 def modify_memo(self, note_id, memo):
33 note = self._find_note(note_id)
34 if note:
35 note.memo = memo
36 return True
37 return False
38
39 def modify_tags(self, note_id, tags):
40 note = self._find_note(note_id)
41 if note:
42 note.tags = tags
43 return True
44 return False
45
46 def search(self, filter):
47 return [note for note in self.notes if note.match(filter)]
2 from notebook import Notebook, Note
3
4 class Menu:
5 def __init__(self):
6 self.notebook = Notebook()
7 self.choices = {
8 "1" : self.show_notes,
9 "2" : self.search_notes,
10 "3" : self.add_note,
11 "4" : self.modify_note,
12 "5" : self.quit
13 }
14
15 def display_menu(self):
16 print("""
17 Notebook Menu
18
19 1. Show all Notes
20 2. Search Notes
21 3. Add Note
22 4. Modify Note
23 5. Quit
24 """)
25
26 def run(self):
27 while True:
28 self.display_menu()
29 choice = input("Enter an option: ")
30 action = self.choices.get(choice)
31 if action:
32 action()
33 else:
34 print("{0} is not a valid choice".format(choice))
35
36 def show_notes(self, notes=None):
37 if not notes:
38 notes = self.notebook.notes
39 for note in notes:
40 print("{0}: {1}\n{2}".format(note.id, note.tags, note.memo))
41
42 def search_notes(self):
43 filter = input("Search for: ")
44 notes = self.notebook.search(filter)
45 self.show_notes(notes)
46
47 def add_note(self):
48 memo = input("Enter a memo:")
49 self.notebook.new_note(memo)
50 print("Your note has been added.")
51
52 def modify_note(self):
53 id = input("Enter a note id:")
54 memo = input("Enter a memo:")
55 tags = input("Enter tags:")
56 if memo:
57 self.notebook.modify_memo(id, memo)
58 if tags:
59 self.notebook.modify_tags(id, tags)
60
61 def quit(self):
62 print("Thank you for using your notebook today.")
63 sys.exit(0)
64
65 if __name__ == "__main__":
66 Menu().run()
67
2
3 last_id = 0
4
5 class Note:
6
7 def __init__(self, memo, tags=''):
8 self.memo = memo
9 self.tags = tags
10 self.creation_date = datetime.date.today()
11 global last_id
12 last_id += 1
13 self.id = last_id
14
15 def match(self, filter):
16 return filter in self.memo or filter in self.tags
17
18 class Notebook:
19
20 def __init__(self):
21 self.notes = []
22
23 def new_note(self, memo, tags=''):
24 self.notes.append(Note(memo, tags))
25
26 def _find_note(self, note_id):
27 for note in self.notes:
28 if note.id == str(note_id):
29 return note
30 return None
31
32 def modify_memo(self, note_id, memo):
33 note = self._find_note(note_id)
34 if note:
35 note.memo = memo
36 return True
37 return False
38
39 def modify_tags(self, note_id, tags):
40 note = self._find_note(note_id)
41 if note:
42 note.tags = tags
43 return True
44 return False
45
46 def search(self, filter):
47 return [note for note in self.notes if note.match(filter)]
menu 1 import sys
2 from notebook import Notebook, Note
3
4 class Menu:
5 def __init__(self):
6 self.notebook = Notebook()
7 self.choices = {
8 "1" : self.show_notes,
9 "2" : self.search_notes,
10 "3" : self.add_note,
11 "4" : self.modify_note,
12 "5" : self.quit
13 }
14
15 def display_menu(self):
16 print("""
17 Notebook Menu
18
19 1. Show all Notes
20 2. Search Notes
21 3. Add Note
22 4. Modify Note
23 5. Quit
24 """)
25
26 def run(self):
27 while True:
28 self.display_menu()
29 choice = input("Enter an option: ")
30 action = self.choices.get(choice)
31 if action:
32 action()
33 else:
34 print("{0} is not a valid choice".format(choice))
35
36 def show_notes(self, notes=None):
37 if not notes:
38 notes = self.notebook.notes
39 for note in notes:
40 print("{0}: {1}\n{2}".format(note.id, note.tags, note.memo))
41
42 def search_notes(self):
43 filter = input("Search for: ")
44 notes = self.notebook.search(filter)
45 self.show_notes(notes)
46
47 def add_note(self):
48 memo = input("Enter a memo:")
49 self.notebook.new_note(memo)
50 print("Your note has been added.")
51
52 def modify_note(self):
53 id = input("Enter a note id:")
54 memo = input("Enter a memo:")
55 tags = input("Enter tags:")
56 if memo:
57 self.notebook.modify_memo(id, memo)
58 if tags:
59 self.notebook.modify_tags(id, tags)
60
61 def quit(self):
62 print("Thank you for using your notebook today.")
63 sys.exit(0)
64
65 if __name__ == "__main__":
66 Menu().run()
67
相关阅读 更多 +