WEBVTT

0
00:00.810 --> 00:02.160
So in the last lesson,

1
00:02.190 --> 00:07.190
we looked at how we can use this JSON format to save our data and to load

2
00:09.120 --> 00:11.070
our data into our program.

3
00:11.730 --> 00:16.730
But one of the problems that we have is if we run our program from scratch

4
00:17.280 --> 00:21.600
where we don't have that data.json file created,

5
00:21.930 --> 00:26.930
then the first thing that it's going to encounter once it lands inside here is

6
00:27.120 --> 00:30.570
it's going to try and open a file that doesn't exist.

7
00:31.110 --> 00:34.050
And what happens then? We get a file

8
00:34.050 --> 00:38.490
not found error because there is no such file data.json.

9
00:39.030 --> 00:41.400
So what can we do? Well,

10
00:41.640 --> 00:46.640
we already learned about dealing with exceptions and handling them so that we

11
00:47.160 --> 00:49.380
don't end up in these situations.

12
00:49.980 --> 00:54.980
So now it's time to put your knowledge into practice inside a real project.

13
00:55.650 --> 01:00.650
And I want you to think about how you can catch that particular exception,

14
01:01.380 --> 01:06.060
how you can get it to work so that if this fails,

15
01:06.330 --> 01:09.780
then we simply create a brand new data file.

16
01:10.200 --> 01:13.530
But if it does work, then we can simply load up the data,

17
01:13.860 --> 01:16.050
update the data and write the data.

18
01:16.680 --> 01:19.830
So the actual implementation I'll leave up to you.

19
01:20.220 --> 01:25.220
But what you want to be able to do is to not have a data.json file and run

20
01:26.190 --> 01:28.380
your code, and it would not crash

21
01:28.500 --> 01:33.500
and instead it would create a brand new data.json file with the new data.

22
01:35.640 --> 01:40.640
But if you had already got some old data, then it should simply just update and

23
01:40.770 --> 01:45.600
write it to file. Pause the video and see if you can complete this

24
01:45.720 --> 01:46.590
as a challenge.

25
01:50.970 --> 01:55.200
This always starts with analyzing which part of our code is the part that's

26
01:55.200 --> 01:59.400
going to fail or likely to fail at some point. And in our case,

27
01:59.460 --> 02:00.960
it's this line right here,

28
02:01.140 --> 02:06.140
opening this line in read mode is dangerous because if this doesn't exist,

29
02:07.260 --> 02:09.720
then we get that file not found error.

30
02:10.260 --> 02:15.090
So what we can do is we can put this inside a try statement.

31
02:15.660 --> 02:20.010
Try opening this file and try reading the data that's inside.

32
02:20.790 --> 02:24.630
Now, if this fails, however, then we can catch it

33
02:25.110 --> 02:28.230
and we're going to catch a very specific type of exception,

34
02:28.260 --> 02:32.250
which is the file not found error exception. Now,

35
02:32.250 --> 02:34.050
when this exception happens,

36
02:34.230 --> 02:38.490
what we want to do is we want to create a new file

37
02:38.820 --> 02:43.740
which is going to be our data.json. So at this point,

38
02:43.740 --> 02:48.090
remember it doesn't yet exist. And we're going to open it in write mode.

39
02:48.750 --> 02:53.490
And this data file is going to be used so that we can write to it

40
02:53.520 --> 02:57.810
and we can dump our newly created data up here.

41
02:58.350 --> 03:01.480
Remember, when the user hits the add button,

42
03:01.780 --> 03:06.190
then everything they've typed in these entries gets aggregated into this

43
03:06.190 --> 03:07.023
dictionary

44
03:07.210 --> 03:11.710
and that dictionary is what we want to dump into this new data file.

45
03:12.340 --> 03:16.900
So we'll get hold of that new data and we'll dump it into the data file.

46
03:17.830 --> 03:18.550
And again,

47
03:18.550 --> 03:23.550
we'll keep the indent to four spaces just so that we can read it more easily.

48
03:25.030 --> 03:28.750
Now this is what should happen if the file's not found.

49
03:29.380 --> 03:33.250
But if the file was found and it already exists,

50
03:33.340 --> 03:37.240
then we're going to tap into the else statement. And in the else statement

51
03:37.270 --> 03:42.070
what we're going to do is we're going to get hold of the data which we managed

52
03:42.070 --> 03:46.270
to get because this block of code succeeded, and in the else block

53
03:46.270 --> 03:50.950
what we're going to do is we're going to update the data with the new data. Now,

54
03:51.100 --> 03:53.500
this else block is only going to be triggered

55
03:53.530 --> 03:56.650
if everything inside the try block was successful.

56
03:57.070 --> 03:59.260
So that means by the end of this block,

57
03:59.290 --> 04:04.290
we have this data which holds onto all of the existing data from that data file

58
04:05.020 --> 04:09.670
and we update that data with the new data which is this dictionary here.

59
04:10.360 --> 04:13.210
Now, after we've updated that data,

60
04:13.270 --> 04:17.560
we want to be able to open up data.jason in the write mode

61
04:17.950 --> 04:22.950
and then dump this updated data from here into that data file.

62
04:25.480 --> 04:27.940
And then once all of this is done,

63
04:28.570 --> 04:32.980
no matter if there was an exception or if there was no issues,

64
04:33.280 --> 04:37.840
we're going to still need to delete everything that's inside the website and

65
04:37.840 --> 04:38.800
password entry.

66
04:39.220 --> 04:43.240
So we can even add a finally keyword right here.

67
04:44.560 --> 04:49.360
So this is the final code that deals with our exception.

68
04:49.690 --> 04:54.460
We have our try block which can fail, we have our except block

69
04:54.490 --> 04:58.720
which deals with any failures that occur, we have our else block

70
04:58.780 --> 05:03.010
which has the code that needs to be run if there were no issues,

71
05:03.370 --> 05:07.810
and finally, at the very end no matter if there was an issue or no issue,

72
05:08.140 --> 05:12.640
we have a block of code which clears our website and password entries.

73
05:13.240 --> 05:16.660
Now you might've noticed there's a little bit of repetition here with the

74
05:16.690 --> 05:19.090
writing to file. So if you want to,

75
05:19.120 --> 05:22.660
you could in fact create a separate method somewhere up here.

76
05:23.110 --> 05:26.980
But I'm actually going to keep this as it is just because I think when you look

77
05:26.980 --> 05:30.550
at this code, if you had any issues and you wanted to review it,

78
05:31.060 --> 05:35.920
this is much easier to understand and reason about compared to a separate

79
05:35.920 --> 05:36.753
function.

80
05:37.330 --> 05:40.930
Feel free to refactor this code and make it as short as you want.

81
05:41.380 --> 05:43.210
But this is the version I'm going to keep

82
05:43.450 --> 05:46.690
just so that if you had any issues and you want to take a look at it,

83
05:46.960 --> 05:47.860
it's there for you

84
05:47.950 --> 05:52.950
and it's easy to see the try, except, else, and finally blocks with all of the

85
05:54.070 --> 05:55.660
code together in one place.

86
05:58.130 --> 05:59.330
Now in the next lesson,

87
05:59.390 --> 06:02.750
we're going to be completing the functionality of our password manager.

88
06:03.110 --> 06:06.380
We're gonna add that all important search functionality

89
06:06.740 --> 06:11.510
which is going to look through our JSON, fetch the results for a particular

90
06:11.510 --> 06:14.630
website, and then show it to us in a popup.

91
06:15.050 --> 06:18.380
So for all of that and more, I'll see you on the next lesson.