Utoljára aktív 1731423730

Lab notes on getting going thru the early parts of Tasty lab.

kristofer's Avatar kristofer gist felülvizsgálása 1731423729. Revízióhoz ugrás

Nincsenek változtatások

kristofer's Avatar kristofer gist felülvizsgálása 1731423658. Revízióhoz ugrás

1 file changed, 1 insertion, 1 deletion

TastyHints.md

@@ -114,7 +114,7 @@ for task_name, task_status in tasks.items():
114 114 We could put that loop into a function:
115 115
116 116 ```python
117 - def print_tasks(tasks):
117 + def display_tasks(tasks):
118 118 for task_name, task_status in tasks.items():
119 119 print("- ", task_name, task_status)
120 120 ```

kristofer's Avatar kristofer gist felülvizsgálása 1731423612. Revízióhoz ugrás

1 file changed, 2 insertions

TastyHints.md

@@ -83,6 +83,8 @@ tasks = {} # creates an empty dictionary
83 83
84 84 tasks['Buy Milk'] = 'not yet'
85 85
86 + tasks['Start Lab'] = 'completed'
87 +
86 88 # to list all the tasks
87 89
88 90 for task_name, task_status in tasks.items():

kristofer's Avatar kristofer gist felülvizsgálása 1731423530. Revízióhoz ugrás

1 file changed, 2 insertions

TastyHints.md

@@ -71,6 +71,8 @@ for task_name, task_status in tasks.items():
71 71
72 72 ## Hint 2
73 73
74 + ### Adding code to the class
75 +
74 76 As we have thought about this a bit, we hit on the idea that a dictionary might work well:
75 77
76 78 ```python

kristofer's Avatar kristofer gist felülvizsgálása 1731423483. Revízióhoz ugrás

1 file changed, 16 insertions, 8 deletions

TastyHints.md

@@ -24,14 +24,22 @@ This is the `tasks` variable.
24 24 The most important thing is the tasks you're tracking.
25 25 It tracks all the tasks, things that get added and removed.
26 26
27 - What are the two things you want to track?
27 + What are the two things you want to track about each task?
28 28
29 29 - the name/title of the Task _Buy Milk_
30 30 - status of Task _complete_ or _not yet_
31 31
32 + That means you track `(task_name, task_status)`.
32 33 so in Python, if you have a pair things that need to be tracked together, a good possibility is a Dictionary.
33 34
34 - Imagine a dict like this one:
35 + ```python
36 + tasks = {
37 + "task1": "not yet",
38 + "task2": "completed"
39 + }
40 + ```
41 +
42 + Imagine some code to get a `tasks` dict with a few tasks in it:
35 43
36 44 ```python
37 45
@@ -56,8 +64,8 @@ else:
56 64
57 65 # to list all the tasks
58 66
59 - for task_name, status in tasks.items():
60 - print("- ", task_name, status)
67 + for task_name, task_status in tasks.items():
68 + print("- ", task_name, task_status)
61 69 ```
62 70
63 71
@@ -75,8 +83,8 @@ tasks['Buy Milk'] = 'not yet'
75 83
76 84 # to list all the tasks
77 85
78 - for task_name, status in tasks.items():
79 - print("- ", task_name, status)
86 + for task_name, task_status in tasks.items():
87 + print("- ", task_name, task_status)
80 88 ```
81 89
82 90 Paste this block of code into the python interpreter and observe what happens.
@@ -95,8 +103,8 @@ tasks['Walk Dog'] = 'not yet'
95 103
96 104 # to list all the tasks
97 105
98 - for task_name, status in tasks.items():
99 - print("- ", task_name, status)
106 + for task_name, task_status in tasks.items():
107 + print("- ", task_name, task_status)
100 108 ```
101 109
102 110 We could put that loop into a function:

kristofer's Avatar kristofer gist felülvizsgálása 1731423235. Revízióhoz ugrás

1 file changed, 4 insertions, 1 deletion

TastyHints.md

@@ -1,8 +1,11 @@
1 1 # Tasty Lab Hints
2 2
3 - ## Hint 1
3 + Lab notes on getting going thru the early parts of **Tasty**.
4 +
5 + ## Hint 0
4 6
5 7 Remember start _very small_ and build outward.
8 + The class/program already works, run it and see.
6 9
7 10 ## Hint 1
8 11

kristofer's Avatar kristofer gist felülvizsgálása 1731423148. Revízióhoz ugrás

1 file changed, 80 insertions, 16 deletions

TastyHints.md

@@ -1,23 +1,45 @@
1 1 # Tasty Lab Hints
2 2
3 + ## Hint 1
3 4
4 - ## Hint 2
5 + Remember start _very small_ and build outward.
6 +
7 + ## Hint 1
8 +
9 + ### Modeling a series of tasks
10 +
11 + If you haven't already decided how to start...
12 +
13 + This is the `tasks` variable.
14 +
15 + ```
16 + Tasty class.
17 + :param tasks: the user tasks
18 + ```
19 + (each of those `:param` things are probably instance variables (eventually))
20 +
21 + The most important thing is the tasks you're tracking.
22 + It tracks all the tasks, things that get added and removed.
23 +
24 + What are the two things you want to track?
25 +
26 + - the name/title of the Task _Buy Milk_
27 + - status of Task _complete_ or _not yet_
5 28
6 - If we have thought about this a bit, we might hit on the idea that a dictionary might work well:
29 + so in Python, if you have a pair things that need to be tracked together, a good possibility is a Dictionary.
30 +
31 + Imagine a dict like this one:
7 32
8 33 ```python
9 34
10 35 tasks = {} # creates an empty dictionary
11 36
12 37 tasks['Buy Milk'] = 'not yet'
13 -
14 38 tasks['Start Lab'] = 'completed'
15 39
16 40 tasks['Finish Lab'] = 'not yet'
17 41
18 - # now there are three tasks in the dictionary
19 -
20 - # how to test if a task is complete?
42 + # to test if something is done?
21 43
22 44 task_name = 'Buy Milk'
23 45
@@ -28,6 +50,26 @@ else:
28 50 # task is 'complete'
29 51 pass
30 52
53 +
54 + # to list all the tasks
55 +
56 + for task_name, status in tasks.items():
57 + print("- ", task_name, status)
58 + ```
59 +
60 +
61 + ## Hint 2
62 +
63 + As we have thought about this a bit, we hit on the idea that a dictionary might work well:
64 +
65 + ```python
66 +
67 + tasks = {} # creates an empty dictionary
68 +
69 + # add a task to the empty tasks variable
70 +
71 + tasks['Buy Milk'] = 'not yet'
72 +
31 73 # to list all the tasks
32 74
33 75 for task_name, status in tasks.items():
@@ -38,7 +80,31 @@ Paste this block of code into the python interpreter and observe what happens.
38 80 Read the code, does it make sense how we are using the task_name as the _key_ and
39 81 the task_status as the _value_?
40 82
41 - We need to think about how to put thse ideas _into the class Tasty_.
83 + Add another task, and print out the tasks dict again.
84 +
85 + ```python
86 +
87 + tasks = {} # creates an empty dictionary
88 +
89 + # add a task to the empty tasks variable
90 +
91 + tasks['Walk Dog'] = 'not yet'
92 +
93 + # to list all the tasks
94 +
95 + for task_name, status in tasks.items():
96 + print("- ", task_name, status)
97 + ```
98 +
99 + We could put that loop into a function:
100 +
101 + ```python
102 + def print_tasks(tasks):
103 + for task_name, task_status in tasks.items():
104 + print("- ", task_name, task_status)
105 + ```
106 +
107 + We need to think about how to put these ideas _into the class Tasty_.
42 108 We might...
43 109
44 110 ```python
@@ -58,8 +124,8 @@ class Tasty:
58 124
59 125
60 126 def display_tasks(self):
61 - for task_name, status in self.tasks.items():
62 - print("- ", task_name, status)
127 + for task_name, task_status in self.tasks.items():
128 + print("- ", task_name, task_status)
63 129
64 130 ```
65 131
@@ -67,16 +133,12 @@ See how those things went from just trial code to _methods_ in the class?
67 133
68 134 Yes, and... the `self` variable is how you know that thing is a _method_ and not just a _function_.
69 135
70 - That's an example of how I might use the rough class here.
71 -
72 - ```python
73 - def display_tasks(self):
74 - for task_name, status in self.tasks.items():
75 - print("- ", task_name, status)
76 - ```
136 + That's an example of how I might use the rough class.
77 137
78 138 ## Hint 3
79 139
140 + ### How to handle more complex input
141 +
80 142 Student Asks: _I'm struggling to see where we take the user input for the task name._
81 143
82 144 If we have `command = input('Tasty> ')`
@@ -146,6 +208,8 @@ def prompt_user(self, prompt):
146 208 # split words into a list and process into two parts.
147 209 ```
148 210
211 + ## Hint 4
212 +
149 213 ### Saving/Loading to a JSON file
150 214
151 215 JSON is just a structured text format for data. Very Handy.

kristofer's Avatar kristofer gist felülvizsgálása 1731422633. Revízióhoz ugrás

1 file changed, 73 insertions

TastyHints.md

@@ -1,6 +1,79 @@
1 1 # Tasty Lab Hints
2 2
3 3
4 + ## Hint 2
5 +
6 + If we have thought about this a bit, we might hit on the idea that a dictionary might work well:
7 +
8 + ```python
9 +
10 + tasks = {} # creates an empty dictionary
11 +
12 + tasks['Buy Milk'] = 'not yet'
13 +
14 + tasks['Start Lab'] = 'completed'
15 +
16 + tasks['Finish Lab'] = 'not yet'
17 +
18 + # now there are three tasks in the dictionary
19 +
20 + # how to test if a task is complete?
21 +
22 + task_name = 'Buy Milk'
23 +
24 + if tasks[task_name] == 'not yet':
25 + # task is unfinished
26 + pass
27 + else:
28 + # task is 'complete'
29 + pass
30 +
31 + # to list all the tasks
32 +
33 + for task_name, status in tasks.items():
34 + print("- ", task_name, status)
35 + ```
36 +
37 + Paste this block of code into the python interpreter and observe what happens.
38 + Read the code, does it make sense how we are using the task_name as the _key_ and
39 + the task_status as the _value_?
40 +
41 + We need to think about how to put thse ideas _into the class Tasty_.
42 + We might...
43 +
44 + ```python
45 + class Tasty:
46 +
47 + def __init__(self):
48 + self.tasks = {}
49 +
50 + def add_task(self, task_name):
51 + """
52 + Add a new task to the user tasks.
53 + """
54 + if task_name not in self.tasks:
55 + self.tasks[task_name] = "not yet"
56 + else:
57 + print("Task already added.")
58 +
59 +
60 + def display_tasks(self):
61 + for task_name, status in self.tasks.items():
62 + print("- ", task_name, status)
63 +
64 + ```
65 +
66 + See how those things went from just trial code to _methods_ in the class?
67 +
68 + Yes, and... the `self` variable is how you know that thing is a _method_ and not just a _function_.
69 +
70 + That's an example of how I might use the rough class here.
71 +
72 + ```python
73 + def display_tasks(self):
74 + for task_name, status in self.tasks.items():
75 + print("- ", task_name, status)
76 + ```
4 77
5 78 ## Hint 3
6 79

kristofer's Avatar kristofer gist felülvizsgálása 1731422341. Revízióhoz ugrás

1 file changed, 115 insertions

TastyHints.md(fájl létrehozva)

@@ -0,0 +1,115 @@
1 + # Tasty Lab Hints
2 +
3 +
4 +
5 + ## Hint 3
6 +
7 + Student Asks: _I'm struggling to see where we take the user input for the task name._
8 +
9 + If we have `command = input('Tasty> ')`
10 +
11 + So the `command` variable will have `exit` or whatever, after the user types something in
12 + and taps <return>, right?
13 +
14 + And the `new <task>` command/line when entered, will actually look like `new Buy Milk` when user types the
15 + command to be processed by the program.
16 +
17 + so take the string inputted and split it into a list of strings, breaking each string on the <space> character.
18 +
19 + ```python
20 + txt = "command typed by user"
21 +
22 + x = txt.split()
23 +
24 + print(x)
25 +
26 + # x will be a list: ['command', 'typed', 'by', 'user']
27 + ```
28 +
29 + If we want to extend this to make two new strings:
30 + - command (the first string, perhaps "new")
31 + - and rest (the rest of the split strings glued back together with spaces in between...)
32 +
33 + ```python
34 + txt = "new Buy Milk"
35 + t = txt.split()
36 + print(t) # t will be: ["new", "Buy", "Milk"]
37 + command = t[0] # command will be "new"
38 + rest = t[1:] # carefully: Slice the array, from t[1] to end of list
39 + # rest will be ["Buy", "Milk"]
40 + rest = " ".join(rest) # now rest will be "Buy Milk"
41 + print(command, ' - ', rest)
42 + ```
43 +
44 + and after hacking a bit in the python interpreter, you may end up with
45 +
46 + ```python
47 + line = input(prompt)
48 + #print(line)
49 + while not line:
50 + line = input(prompt) # this causes input to wait until
51 + # line is not an empty string
52 + words = line.split()
53 + #print(words)
54 + command = words[0]
55 + #print(command)
56 + rest = words[1:]
57 + rest = " ".join(rest)
58 + #print(rest)
59 + ```
60 +
61 + and maybe the right thing to do is to change the `command = input("Tasty> ")` to something like
62 + `command, rest = tasty.prompt_user("Tasty> ")`
63 +
64 + then all the "words" after the "new" will be what you pull together to name the task?
65 +
66 + so `prompt-user(self, prompt)` becomes a method which asks for the user's input
67 + (using input(prompt))
68 +
69 + ```python
70 + def prompt_user(self, prompt):
71 + inp = input(prompt)
72 + # insert the stuff up there about how to
73 + # split words into a list and process into two parts.
74 + ```
75 +
76 + ### Saving/Loading to a JSON file
77 +
78 + JSON is just a structured text format for data. Very Handy.
79 +
80 + ## saving? a Hint
81 +
82 + ```python
83 + with open("saved_data.json", "w") as fp:
84 + json.dump(self.tasks,fp)
85 + ```
86 +
87 + ## loading? a Hint
88 +
89 + ```python
90 + with open(filename) as json_file:
91 + self.tasks = json.load(json_file)
92 + ```
93 +
94 + And you should embed these two ideas into a method.
95 +
96 + When you get to the point where you are puzzling over how to save/load multiple dicts
97 + (say, _tasks_, _trash_, and _important_),
98 + you might consider putting all this in another dictionary and just saving that.
99 +
100 + You still have to _load_ the one dict and then split it into three for use by the methods.
101 + This should be done in _load tasks_ method.
102 +
103 + The data structure might be created by
104 +
105 + ```python
106 + dict_to_save = { "tasks": self.tasks, "trash": self.trash, "impt": self.important }
107 + ```
108 +
109 + You can now _json.dump_ the `dict_to_save` to a file.
110 +
111 + Loading it will be the opposite, you end up with one `loaded_dicts` which
112 + have to be split like: `self.tasks = loaded_dicts['tasks']`
113 +
114 +
115 + Easy Peasy
Újabb Régebbi