T. Meissner 5 years ago
parent
commit
7f226118c9
4 changed files with 144 additions and 2 deletions
  1. 1
    0
      .gitignore
  2. 5
    2
      c_pointers/chapter_5.c
  3. 69
    0
      python_3_oop/chapter02/menu.py
  4. 69
    0
      python_3_oop/chapter02/notebook.py

+ 1
- 0
.gitignore View File

@@ -2,3 +2,4 @@
2 2
 *.swp
3 3
 *.*#*
4 4
 .idea
5
+__pycache__

+ 5
- 2
c_pointers/chapter_5.c View File

@@ -97,7 +97,7 @@ int main(void) {
97 97
         // better:
98 98
         char *command = malloc(11);
99 99
         if (command != NULL) {
100
-            printf("%s: ", "Enter a command");
100
+            printf("%s: ", "Enter a command (max. 10 chars): ");
101 101
             scanf("%10s", command);
102 102
             printf("You entered: %s with length: %zu\n", command, strlen(command));
103 103
             free(command);
@@ -107,7 +107,10 @@ int main(void) {
107 107
         // executes without warnings or errors on osx :D
108 108
         char *mem = malloc(11);
109 109
         if (mem != NULL) {
110
-            memset(mem, 0x42, 64);
110
+            memset(mem, 0x42, 16);
111
+            for (size_t i = 0; i < 16; i++) {
112
+                printf("mem[%zu] Address: %p : %c\n", i, &mem[i], mem[i]);
113
+            }
111 114
             free(mem);
112 115
         }
113 116
 

+ 69
- 0
python_3_oop/chapter02/menu.py View File

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

+ 69
- 0
python_3_oop/chapter02/notebook.py View File

@@ -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
+