kristofer revisou este gist . Ir para a revisão
1 file changed, 4 insertions, 5 deletions
TastyHint3.md
@@ -89,11 +89,10 @@ The data structure might be created by | |||
89 | 89 | dict_to_save = { "tasks": self.tasks, "trash": self.trash, "impt": self.important } | |
90 | 90 | ``` | |
91 | 91 | ||
92 | + | You can now _json.dump_ the `dict_to_save` to a file. | |
92 | 93 | ||
94 | + | Loading it will be the opposite, you end up with one `loaded_dicts` which | |
95 | + | have to be split like: `self.tasks = loaded_dicts['tasks']` | |
93 | 96 | ||
94 | 97 | ||
95 | - | ||
96 | - | ||
97 | - | ||
98 | - | ||
99 | - | ||
98 | + | Easy Peasy |
kristofer revisou este gist . Ir para a revisão
1 file changed, 26 insertions
TastyHint3.md
@@ -67,6 +67,32 @@ with open("saved_data.json", "w") as fp: | |||
67 | 67 | json.dump(self.tasks,fp) | |
68 | 68 | ``` | |
69 | 69 | ||
70 | + | ## loading? a Hint | |
71 | + | ||
72 | + | ```python | |
73 | + | with open(filename) as json_file: | |
74 | + | self.tasks = json.load(json_file) | |
75 | + | ``` | |
76 | + | ||
77 | + | And you should embed this stuff into a method. | |
78 | + | ||
79 | + | When you get to the point where you are puzzling over how to save/load multiple dicts | |
80 | + | (say, _tasks_, _trash_, and _important_), | |
81 | + | you might consider putting all this in another dictionary and just saving that. | |
82 | + | ||
83 | + | You still have to _load_ the one dict and then split it into three for use by the methods. | |
84 | + | This should be done in _load tasks_ method. | |
85 | + | ||
86 | + | The data structure might be created by | |
87 | + | ||
88 | + | ```python | |
89 | + | dict_to_save = { "tasks": self.tasks, "trash": self.trash, "impt": self.important } | |
90 | + | ``` | |
91 | + | ||
92 | + | ||
93 | + | ||
94 | + | ||
95 | + | ||
70 | 96 | ||
71 | 97 | ||
72 | 98 |
kristofer revisou este gist . Ir para a revisão
1 file changed, 17 insertions
TastyHint3.md
@@ -29,6 +29,23 @@ rest = " ".join(rest) | |||
29 | 29 | print(command, ' - ', rest) | |
30 | 30 | ``` | |
31 | 31 | ||
32 | + | and after hacking a bit in the python interpreter, you may end up with | |
33 | + | ||
34 | + | ```python | |
35 | + | line = input(prompt) | |
36 | + | #print(line) | |
37 | + | while not line: | |
38 | + | line = input(prompt) | |
39 | + | words = line.split() | |
40 | + | #print(words) | |
41 | + | command = words[0] | |
42 | + | #print(command) | |
43 | + | rest = words[1:] | |
44 | + | rest = " ".join(rest) | |
45 | + | #print(rest) | |
46 | + | ``` | |
47 | + | ||
48 | + | ||
32 | 49 | and maybe the right thing to do is to change the `command = input("Tasty> ")` to something like | |
33 | 50 | `command, rest = tasty.prompt_user("Tasty> ")` | |
34 | 51 |
kristofer revisou este gist . Ir para a revisão
1 file changed, 32 insertions, 2 deletions
TastyHint3.md
@@ -7,8 +7,30 @@ and the `new <task>` will actually look like `new Buy Milk` | |||
7 | 7 | ||
8 | 8 | so take the string and split it into a list of strings, breaking each string on the <space> character. | |
9 | 9 | ||
10 | - | and maybe the right thing to do is to change the `command = input("Tasty> ")` to something like `words = prompt_user("Tasty> ")` | |
11 | - | and then immediately `command = words[0]` | |
10 | + | ```python | |
11 | + | txt = "welcome to the jungle" | |
12 | + | ||
13 | + | x = txt.split() | |
14 | + | ||
15 | + | print(x) | |
16 | + | ||
17 | + | # ['welcome', 'to', 'the', 'jungle'] | |
18 | + | ``` | |
19 | + | ||
20 | + | BUT Ulas says | |
21 | + | ||
22 | + | ```python | |
23 | + | txt = "new Buy Milk" | |
24 | + | t = txt.split() | |
25 | + | print(t) | |
26 | + | command = t[0] | |
27 | + | rest = t[1:] | |
28 | + | rest = " ".join(rest) | |
29 | + | print(command, ' - ', rest) | |
30 | + | ``` | |
31 | + | ||
32 | + | and maybe the right thing to do is to change the `command = input("Tasty> ")` to something like | |
33 | + | `command, rest = tasty.prompt_user("Tasty> ")` | |
12 | 34 | ||
13 | 35 | then all the "words" after the "new" will be what you pull together to name the task? | |
14 | 36 | ||
@@ -21,6 +43,14 @@ def prompt_user(self, prompt): | |||
21 | 43 | # lookup how to split words into words[] | |
22 | 44 | ``` | |
23 | 45 | ||
46 | + | ## saving? a Hint | |
47 | + | ||
48 | + | ```python | |
49 | + | with open("saved_data.json", "w") as fp: | |
50 | + | json.dump(self.tasks,fp) | |
51 | + | ``` | |
52 | + | ||
53 | + | ||
24 | 54 | ||
25 | 55 | ||
26 | 56 |
kristofer revisou este gist . Ir para a revisão
1 file changed, 11 insertions, 1 deletion
TastyHint3.md
@@ -13,4 +13,14 @@ and then immediately `command = words[0]` | |||
13 | 13 | then all the "words" after the "new" will be what you pull together to name the task? | |
14 | 14 | ||
15 | 15 | so `prompt-user(self, prompt)` becomes a method which asks for the user's input | |
16 | - | (using input(prompt)) | |
16 | + | (using input(prompt)) | |
17 | + | ||
18 | + | ```python | |
19 | + | def prompt_user(self, prompt): | |
20 | + | inp = input(prompt) | |
21 | + | # lookup how to split words into words[] | |
22 | + | ``` | |
23 | + | ||
24 | + | ||
25 | + | ||
26 | + |
kristofer revisou este gist . Ir para a revisão
1 file changed, 16 insertions
TastyHint3.md(arquivo criado)
@@ -0,0 +1,16 @@ | |||
1 | + | ||
2 | + | _I'm struggling to see where we take the user input for the task name._ | |
3 | + | ||
4 | + | the `command` variable will have `exit` or whatever, right? | |
5 | + | ||
6 | + | and the `new <task>` will actually look like `new Buy Milk` | |
7 | + | ||
8 | + | so take the string and split it into a list of strings, breaking each string on the <space> character. | |
9 | + | ||
10 | + | and maybe the right thing to do is to change the `command = input("Tasty> ")` to something like `words = prompt_user("Tasty> ")` | |
11 | + | and then immediately `command = words[0]` | |
12 | + | ||
13 | + | then all the "words" after the "new" will be what you pull together to name the task? | |
14 | + | ||
15 | + | so `prompt-user(self, prompt)` becomes a method which asks for the user's input | |
16 | + | (using input(prompt)) |