|
@@ -0,0 +1,69 @@
|
|
1
|
+import datetime
|
|
2
|
+
|
|
3
|
+# Store the next available id for all new notes
|
|
4
|
+last_id = 0
|
|
5
|
+
|
|
6
|
+class Note:
|
|
7
|
+ '''Represent a note in the notebook. Match against a
|
|
8
|
+ string in searches and store tags for each note.'''
|
|
9
|
+
|
|
10
|
+ def __init__(self, memo, tags=''):
|
|
11
|
+ '''initialize a note with memo and optional
|
|
12
|
+ space-separated tags. Automatically set the note's
|
|
13
|
+ creation date and a unique id.'''
|
|
14
|
+ self.memo = memo
|
|
15
|
+ self.tags = tags
|
|
16
|
+ self.creation_date = datetime.date.today()
|
|
17
|
+ global last_id
|
|
18
|
+ last_id += 1
|
|
19
|
+ self.id = last_id
|
|
20
|
+
|
|
21
|
+ def match(self, filter):
|
|
22
|
+ '''Determine if this note matches the filter
|
|
23
|
+ text. Return True if it matches, False otherwise.
|
|
24
|
+ Search is case sensitive and matches both text and
|
|
25
|
+ tags.'''
|
|
26
|
+ return filter in self.memo or filter in self.tags
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+class Notebook:
|
|
30
|
+ '''Represent a collection of notes that can be tagged,
|
|
31
|
+ modified, and searched.'''
|
|
32
|
+
|
|
33
|
+ def __init__(self):
|
|
34
|
+ '''Initialize a notebook with an empty list.'''
|
|
35
|
+ self.notes = []
|
|
36
|
+
|
|
37
|
+ def new_note(self, memo, tags=''):
|
|
38
|
+ '''Create a new note and add it to the list.'''
|
|
39
|
+ self.notes.append(Note(memo, tags))
|
|
40
|
+
|
|
41
|
+ def modify_memo(self, note_id, memo):
|
|
42
|
+ '''Find the note with the given id and change its
|
|
43
|
+ memo to the given value.'''
|
|
44
|
+ note = self._find_note(note_id)
|
|
45
|
+ if note:
|
|
46
|
+ note.memo = memo
|
|
47
|
+ return True
|
|
48
|
+ return False
|
|
49
|
+
|
|
50
|
+ def modify_tags(self, note_id, tags):
|
|
51
|
+ '''Find the note with the given id and change its tags
|
|
52
|
+ to the given value.'''
|
|
53
|
+ note = self._find_note(note_id)
|
|
54
|
+ if note:
|
|
55
|
+ note.tags = tags
|
|
56
|
+ return True
|
|
57
|
+ return False
|
|
58
|
+
|
|
59
|
+ def search(self, filter):
|
|
60
|
+ '''Find all notes that match the given filter string.'''
|
|
61
|
+ return [note for note in self.notes if note.match(filter)]
|
|
62
|
+
|
|
63
|
+ def _find_note(self, note_id):
|
|
64
|
+ '''Locate the note with the given id.'''
|
|
65
|
+ for note in self.notes:
|
|
66
|
+ if str(note.id) == str(note_id):
|
|
67
|
+ return note
|
|
68
|
+ return None
|
|
69
|
+
|