1
00:00:05,320 --> 00:00:09,360
So lets continue on about our
discussions of binary files in Python.

2
00:00:09,360 --> 00:00:12,860
And what we're going to do is talk
about something called pickle.

3
00:00:12,860 --> 00:00:15,590
Now if you've used Java in the past
you might be familiar with

4
00:00:15,590 --> 00:00:17,430
the concept of serialization.

5
00:00:17,430 --> 00:00:20,780
That's a process that allows
objects to be saved to a file.

6
00:00:20,780 --> 00:00:24,430
So that they can be stored or
restored I should say from the file later.

7
00:00:24,430 --> 00:00:28,530
Now python provides a mechanism for
serializing objects called pickling.

8
00:00:28,530 --> 00:00:29,610
Hence the word pickle.

9
00:00:29,610 --> 00:00:33,030
So when and object is pickled and
it's written to a file and

10
00:00:33,030 --> 00:00:37,540
format that contains the objects data
together with sufficient information to

11
00:00:37,540 --> 00:00:40,940
allow that object to be recreated
when it's loaded back in.

12
00:00:40,940 --> 00:00:44,090
Now as we've seen,
converting an object to a byte array.

13
00:00:44,090 --> 00:00:48,060
in order to save it into a binary
file isn't particularly easy, and

14
00:00:48,060 --> 00:00:50,120
we saw that in the previous video.

15
00:00:50,120 --> 00:00:53,350
And no information about
the object itself is stored,

16
00:00:53,350 --> 00:00:55,580
unless we write code to somehow do that.

17
00:00:55,580 --> 00:00:58,830
And if you thought just writing
a simple number was complicated,

18
00:00:58,830 --> 00:01:02,600
imagine how tricky it would be to store
something like our locations dictionary

19
00:01:02,600 --> 00:01:05,129
From the adventure game challenge
that we've done previously.

20
00:01:05,129 --> 00:01:07,290
Clearly there needs to be
a better way of doing it.

21
00:01:07,290 --> 00:01:10,800
Now using pickle,
saving objects is very easy and

22
00:01:10,800 --> 00:01:12,210
I'm gonna show you that in an example.

23
00:01:12,210 --> 00:01:12,790
Now once again,

24
00:01:12,790 --> 00:01:16,660
what we're gonna do is we're going to
use the details, detail as I should say.

25
00:01:16,660 --> 00:01:19,380
If you remember the that
we saw in an earlier video.

26
00:01:19,380 --> 00:01:22,490
So let's make a start,
first we're gonna type import pickle,

27
00:01:22,490 --> 00:01:27,780
which is a library that we're
going to be using for this,

28
00:01:27,780 --> 00:01:33,740
we're gonna type imelda = (

29
00:01:33,740 --> 00:01:41,020
'More Mayhem', Imelda May.

30
00:01:45,040 --> 00:01:47,040
2011.

31
00:01:51,060 --> 00:01:56,220
And it's gonna be 1, the actual song's

32
00:01:56,220 --> 00:02:02,900
Pulling the Rug [SOUND]
Two psycho.

33
00:02:06,940 --> 00:02:11,480
Three, let's put a bracket there.

34
00:02:11,480 --> 00:02:13,030
Three.

35
00:02:13,030 --> 00:02:18,940
Mayhem and lastly four

36
00:02:18,940 --> 00:02:23,600
Kentish Town Wall.

37
00:02:23,600 --> 00:02:26,500
And three brackets.

38
00:02:26,500 --> 00:02:28,350
So that's that line.

39
00:02:28,350 --> 00:02:31,310
Now the code To pick all
these saved objects.

40
00:02:31,310 --> 00:02:35,990
So what we're trying to do is pickle,
or save this imelda object, effectively.

41
00:02:35,990 --> 00:02:44,980
So we're going to type
with open("imelda.pickle",

42
00:02:44,980 --> 00:02:46,830
"Wb").

43
00:02:46,830 --> 00:02:51,980
Or you can also do double quote set, Wb.

44
00:02:51,980 --> 00:02:54,040
s pickle_file.

45
00:02:56,020 --> 00:03:00,370
And we'll talk pickle.dump

46
00:03:00,370 --> 00:03:04,980
imelda pickle file.

47
00:03:04,980 --> 00:03:06,000
So looking at the code,

48
00:03:06,000 --> 00:03:09,410
we first have to import the pickle
module to be able to use it.

49
00:03:09,410 --> 00:03:12,310
And that's the line one that does that.

50
00:03:12,310 --> 00:03:15,100
So it can be saved with
a single call as you saw

51
00:03:15,100 --> 00:03:17,870
on line 12 using the pickle.dump method.

52
00:03:18,900 --> 00:03:23,100
Is quite complex cuz it's obviously
got a tuple containing another tuple,

53
00:03:23,100 --> 00:03:25,130
of tuples for the track listings.

54
00:03:25,130 --> 00:03:28,160
Even so, it can be stored with a single
dump call, which is pretty cool.

55
00:03:28,160 --> 00:03:31,000
So, now, the format of the data
is specific to Python.

56
00:03:31,000 --> 00:03:32,050
That's important.

57
00:03:32,050 --> 00:03:36,020
And if we open the [INAUDIBLE] file
in Editor, for example, IntelliJ.

58
00:03:36,020 --> 00:03:39,400
So, if we come back here or
need to run it first so it gets created,

59
00:03:39,400 --> 00:03:42,610
then we should find the [INAUDIBLE] in
our project then we'll try and open it.

60
00:03:42,610 --> 00:03:44,360
You saw it appear over there other side.

61
00:03:44,360 --> 00:03:46,930
So if you try to open it,

62
00:03:46,930 --> 00:03:51,280
it wants you to associate a file type
we'll click on text to see what happens.

63
00:03:51,280 --> 00:03:55,600
You can see the format that is
on screen now for this object.

64
00:03:55,600 --> 00:03:58,480
You could also open this in if
you prefer to do that as well.

65
00:03:58,480 --> 00:04:01,030
So you can see the data is clearly
present there you can see bits and

66
00:04:01,030 --> 00:04:02,250
pieces of the data.

67
00:04:02,250 --> 00:04:06,510
But there's a load of binary data sort of
in and around it, and you can see those

68
00:04:06,510 --> 00:04:09,300
sort of strange characters that we
talked about in the previous video.

69
00:04:09,300 --> 00:04:11,160
The good thing about this though,
is that fortunately,

70
00:04:11,160 --> 00:04:15,220
the pickle module itself takes care
of all the structural detail for us.

71
00:04:15,220 --> 00:04:17,690
So in other words,
we can just load our object back

72
00:04:17,690 --> 00:04:21,240
easily by using the pickle.load method.

73
00:04:21,240 --> 00:04:22,660
So close that down.

74
00:04:22,660 --> 00:04:27,010
So obviously pickle.dump, and
to retrieve it We do pickle.load.

75
00:04:27,010 --> 00:04:31,300
So what I'm going to do is
I'm just going to that code.

76
00:04:31,300 --> 00:04:32,180
So we still got there.

77
00:04:32,180 --> 00:04:34,390
And I'll just grab this bit of code.

78
00:04:34,390 --> 00:04:35,730
We'll just make a change to it.

79
00:04:35,730 --> 00:04:38,710
So comment that again and

80
00:04:39,720 --> 00:04:42,840
this time instead of writing we
are going to read from it again.

81
00:04:42,840 --> 00:04:45,370
So we are going to read.

82
00:04:45,370 --> 00:04:46,490
So with open.

83
00:04:46,490 --> 00:04:50,840
And the name of the file that we use to
save it we're gonna set that to read.

84
00:04:50,840 --> 00:04:55,290
Let's just call that [INAUDIBLE] _pickled.

85
00:04:55,290 --> 00:05:00,070
And we need to change this format.

86
00:05:00,070 --> 00:05:04,080
It's not a dump anymore, so
it's going to be [INAUDIBLE]

87
00:05:04,080 --> 00:05:09,120
the variable that we want to be
used is gonna contain the data.

88
00:05:09,120 --> 00:05:16,970
Equals then we type pickle.load and it's
only at this point now imelda_pickled.

89
00:05:16,970 --> 00:05:20,210
So imelda_pickled,

90
00:05:20,210 --> 00:05:26,110
like so.So that's now reading the data
out of the imelda.pickle file.

91
00:05:26,110 --> 00:05:27,920
And out in order two object.

92
00:05:27,920 --> 00:05:32,680
Now just to confirm that we can go print

93
00:05:36,070 --> 00:05:41,280
two and of course we go album, artist,

94
00:05:41,280 --> 00:05:47,450
year, track, list equals m l two.

95
00:05:49,200 --> 00:05:54,240
Make it print album, print artist,

96
00:05:54,240 --> 00:06:02,190
print year, and then we can even go for
track in track and let's call list.

97
00:06:02,190 --> 00:06:04,290
Okay, and let's extract
the data from the top one now.

98
00:06:04,290 --> 00:06:11,000
So, its track track_number,
track_title = track.

99
00:06:11,000 --> 00:06:17,980
And of course at that point,
we can then print(track_number,

100
00:06:17,980 --> 00:06:21,120
track_title) like so.

101
00:06:21,120 --> 00:06:29,810
If we run that, You can see we've got all
that data, loaded from that binary file.

102
00:06:29,810 --> 00:06:33,440
Pulled back into Python and then stored in
the Imelda 2 variable, and we're able to

103
00:06:33,440 --> 00:06:36,900
go right through it all, through the
topless role grabbing the necessary data.

104
00:06:36,900 --> 00:06:41,330
So it's all been stored and
nicely retrieved, by that simple command,

105
00:06:41,330 --> 00:06:45,700
pickle.load Now the good thing is,
once you open the file for

106
00:06:45,700 --> 00:06:50,470
writing, we can literally pick as many
objects as we want to in that one file.

107
00:06:50,470 --> 00:06:55,320
So to show that, I'm just gonna
uncomment some of this card.

108
00:06:55,320 --> 00:06:57,950
And this card again, cause we wanna
write that again to this file.

109
00:07:01,440 --> 00:07:02,720
Now I'll comment this bit here.

110
00:07:04,290 --> 00:07:07,730
So I'm going to start off
with our melded data again,

111
00:07:07,730 --> 00:07:12,660
and then up here, we're going to
create a few extra, two extra lists.

112
00:07:12,660 --> 00:07:17,970
So even equals list list range, 0, 10,

113
00:07:17,970 --> 00:07:22,930
2 And odd equals list(range(1,10, 20).

114
00:07:22,930 --> 00:07:29,540
So we can do with open imelda.pickle and
we can dump the imelda

115
00:07:29,540 --> 00:07:33,900
list first, which we're doing online
41 but then we can also add to that.

116
00:07:33,900 --> 00:07:39,640
So we can put pickle.dump[even],

117
00:07:39,640 --> 00:07:47,860
pickle_file, pickle.dump[odd],

118
00:07:47,860 --> 00:07:51,040
pickle_file.

119
00:07:52,350 --> 00:07:53,480
We can even do pickle.

120
00:07:56,820 --> 00:08:02,610
Type in a number like 299302,
pickle underscore file as well.

121
00:08:02,610 --> 00:08:06,850
That stored all four,

122
00:08:06,850 --> 00:08:11,020
all three variables, in fact this
other number bar that we typed in.

123
00:08:11,020 --> 00:08:14,750
And we can grab this code back We can
grab all this code there that we used to

124
00:08:14,750 --> 00:08:19,220
read it back it object first
brough that back in and

125
00:08:19,220 --> 00:08:24,470
we'll uncomment that so
that we'll be able to see our data again.

126
00:08:24,470 --> 00:08:26,830
Print that all out including the tuples.

127
00:08:26,830 --> 00:08:28,550
Then we'll make a gap there so
it will print.

128
00:08:34,590 --> 00:08:37,820
I'm gonna put for i in even_list, so

129
00:08:37,820 --> 00:08:41,549
we're gonna extract all the items
out of the list, the even list.

130
00:08:41,549 --> 00:08:44,560
Print[i].
And what we need to do here, is before

131
00:08:44,560 --> 00:08:48,980
that'll work, we need to change this so
we also grab the other data out as well.

132
00:08:48,980 --> 00:08:55,070
So we're going to put
even_list = pickle.load.

133
00:08:55,070 --> 00:09:00,990
And that's going to be _pickled.

134
00:09:00,990 --> 00:09:07,660
Then I'll put odd_list =
pickle.load( _pickled).

135
00:09:07,660 --> 00:09:14,840
And let's just say x =
pickle.load( .pickled).

136
00:09:14,840 --> 00:09:19,160
So we've now grabbed the full data Items
relating to the items that we saved in

137
00:09:19,160 --> 00:09:24,000
lines 41 to 44 and we into these
variables in lines 47 through 50.

138
00:09:24,000 --> 00:09:28,820
So now we can print them out so
we are printing out obviously.

139
00:09:28,820 --> 00:09:31,910
But also we've now gone
through the even list.

140
00:09:31,910 --> 00:09:33,510
And we can do the same for
the odd list as well.

141
00:09:33,510 --> 00:09:38,450
Let's just put another space in there and
copy all that and

142
00:09:38,450 --> 00:09:41,310
we'll make that odd list.

143
00:09:41,310 --> 00:09:43,000
Of this.

144
00:09:43,000 --> 00:09:47,500
>> [NOISE]
>> And then we'll put another one there.

145
00:09:47,500 --> 00:09:49,620
Another long spacing effectively.

146
00:09:49,620 --> 00:09:50,330
And predicts.

147
00:09:51,450 --> 00:09:55,170
So what should have happened now is we
should be saving the four variables you

148
00:09:55,170 --> 00:09:58,600
can see there we're now retrieving them
back then I'm just going through them,

149
00:09:58,600 --> 00:10:00,460
printing out the values
of the various ones and

150
00:10:00,460 --> 00:10:03,450
they should equate to the values
that we used when we saved.

151
00:10:05,080 --> 00:10:07,860
And then you can see that all the items
in there including x, which of

152
00:10:07,860 --> 00:10:12,190
course which was the number from line 44
that we typed in without a variable name.

153
00:10:12,190 --> 00:10:15,270
So the only real thing to remember here
when we're doing this is that the objects

154
00:10:15,270 --> 00:10:19,090
themselves must be read back in
the same order that they're written.

155
00:10:19,090 --> 00:10:23,260
So we pick followed by two lists and
an integer So we have to read back a.

156
00:10:23,260 --> 00:10:25,350
Very important to do it in the same order.

157
00:10:25,350 --> 00:10:30,180
Much of the code in this example is
concerned with initializing the variables

158
00:10:30,180 --> 00:10:31,300
and printing them out.

159
00:10:31,300 --> 00:10:35,040
The actually reading and
writing is really only four lines.

160
00:10:35,040 --> 00:10:40,660
It's lines 41 to 44 to write, and
47 to 50 to load them back in.

161
00:10:40,660 --> 00:10:43,330
And I think you'll agree it's a lot easier

162
00:10:43,330 --> 00:10:46,260
Than the earlier approach of
converting everything to binaries.

163
00:10:46,260 --> 00:10:47,700
And the good thing also is,

164
00:10:47,700 --> 00:10:52,060
there's very few objects in Python that
can not be saved by pickling them.

165
00:10:52,060 --> 00:10:55,400
Now when you're pickling
objects to save to a file,

166
00:10:55,400 --> 00:11:00,210
you can choose from one From one which at
the time of the recording the video five

167
00:11:00,210 --> 00:11:04,280
different protocols that python is
going to use when serialising the data.

168
00:11:04,280 --> 00:11:10,290
Now the latest version is version four and
that was introduced with python 3.4.

169
00:11:10,290 --> 00:11:12,930
That was an improvement
on the earlier protocols.

170
00:11:12,930 --> 00:11:15,550
In other words, it was better support for
very large objects and

171
00:11:15,550 --> 00:11:18,220
also the ability to pickle
more kinds of objects.

172
00:11:18,220 --> 00:11:21,710
The protocols, this is the disadvantages,
aren't backwards compatible.

173
00:11:21,710 --> 00:11:25,790
Sometimes if you use a version 4 protocol
from earlier Python versions And

174
00:11:25,790 --> 00:11:29,870
you won't be able to un tuple your data so
just to confirm the protocols

175
00:11:29,870 --> 00:11:33,430
aren't backwards compatible so
if you used the version four protocol from

176
00:11:33,430 --> 00:11:37,020
earlier python versions you
won't be able to add data.

177
00:11:37,020 --> 00:11:40,110
So let's go through and
look at some of the different types.

178
00:11:40,110 --> 00:11:43,210
So the original protocol was and

179
00:11:43,210 --> 00:11:48,030
what we can do is we can change the way
we the data by the protocol And

180
00:11:48,030 --> 00:11:50,750
then go back and actually have a look
at the file itself that is produced.

181
00:11:50,750 --> 00:11:55,150
So I'm going to change these lines to do
with writing the file lines 41 though 44,

182
00:11:55,150 --> 00:11:59,070
and what we want to do is put
the third parameter there.

183
00:11:59,070 --> 00:12:04,770
We're going to put call
comma protocol equals zero.

184
00:12:04,770 --> 00:12:07,180
I'm going to do that for all three.

185
00:12:10,840 --> 00:12:14,280
Like so, and now if we run that and

186
00:12:14,280 --> 00:12:17,140
then close this window off, and
if we have a look at that file,

187
00:12:18,220 --> 00:12:21,550
you can see there that we've got something
down Now that's much easier to read.

188
00:12:21,550 --> 00:12:23,950
Now it still doesn't make perfect sense.

189
00:12:23,950 --> 00:12:26,390
You can see there's a bracket
there in t7 and tp8.

190
00:12:26,390 --> 00:12:30,160
And so
it still doesn't make complete sense but

191
00:12:30,160 --> 00:12:33,350
we can set the numbers in our data
are delivered with a capital L.

192
00:12:33,350 --> 00:12:37,470
That's really the point we wanna make
there, that the limiter is an there.

193
00:12:37,470 --> 00:12:41,610
And you can see the 2998302 there with
an L on either side of it as well.

194
00:12:41,610 --> 00:12:44,890
So you can at least make out that
the numerical values are And

195
00:12:44,890 --> 00:12:48,800
you can see most of the other data, so
that's protocol zero, but there's always

196
00:12:48,800 --> 00:12:53,100
the protocol version one, and that's the
first primary protocol in old versions of

197
00:12:53,100 --> 00:12:57,840
python should be able to unpick
the data created with that protocol.

198
00:12:57,840 --> 00:13:00,840
Now python 2.3 introduced
The protocol version two,

199
00:13:00,840 --> 00:13:04,080
which could pick up classes or
efficiently.

200
00:13:04,080 --> 00:13:08,950
Now, this version, protocol version two,
also has a never security checks removed.

201
00:13:08,950 --> 00:13:13,510
It was declared insecure, and for that
reason, really I don't suggest you use it.

202
00:13:13,510 --> 00:13:17,570
And, we'll discuss more about that
security implication shortly.

203
00:13:17,570 --> 00:13:21,420
Now the first Python 3 protocol
was Version 3, unsurprisingly.

204
00:13:21,420 --> 00:13:24,160
And this is also the default protocol,

205
00:13:24,160 --> 00:13:27,310
which is used if you don't
specify one obviously early on.

206
00:13:27,310 --> 00:13:31,680
In our code, before we added the protocol
equal, we didn't specify one,

207
00:13:31,680 --> 00:13:35,710
so we were using protocol
version three by default.

208
00:13:35,710 --> 00:13:39,390
Now all versions of Python 3 can
understand that protocol that's version 3,

209
00:13:39,390 --> 00:13:41,870
but data created, this is important,

210
00:13:41,870 --> 00:13:46,710
won't be readable using Python 2.x
any version of version 2 of Python.

211
00:13:46,710 --> 00:13:51,090
It's very important if you are sort of
creating code that's gonna be using Python

212
00:13:51,090 --> 00:13:51,750
2 and Python 3.

213
00:13:51,750 --> 00:13:57,490
The protocol used to pickle objects can
be determined by Python automatically.

214
00:13:57,490 --> 00:14:01,630
So it's not necessary in other words to
specify a protocol when using the method.

215
00:14:01,630 --> 00:14:05,540
In fact you could use different
protocols in the same file, even.

216
00:14:05,540 --> 00:14:07,010
So you could do something like this.

217
00:14:07,010 --> 00:14:08,840
Instead of putting a number there,
so let's go back and

218
00:14:08,840 --> 00:14:14,320
we could put Protocol equals
pickle.HIGHEST_PROTOCOL.

219
00:14:14,320 --> 00:14:15,930
You can leave the second one at zero,

220
00:14:15,930 --> 00:14:21,790
the third one is we could say something
at pickle.DEFAULT_PROTCOL and

221
00:14:21,790 --> 00:14:27,140
we can even do the same for that one
as well so pickle.DEFAULT_PROTOCOL.

222
00:14:27,140 --> 00:14:29,340
That's still going to
work if you run this.

223
00:14:30,510 --> 00:14:33,100
You see we've still got our data back and
everything is working fine.

224
00:14:33,100 --> 00:14:37,490
Because Python's got those checks in built
to know which protocol is being used for

225
00:14:37,490 --> 00:14:39,850
a particular dump, which is pretty neat.

226
00:14:39,850 --> 00:14:42,060
And we're gonna even open the file
up again and have a look at it.

227
00:14:43,280 --> 00:14:45,720
We notice that we've got a couple
of different formats in there.

228
00:14:45,720 --> 00:14:49,230
You can see the even numbers there,
two, four, six, eight, delimited by Ls.

229
00:14:49,230 --> 00:14:50,700
But the other ones don't
really make a lot of sense,

230
00:14:50,700 --> 00:14:53,100
because they're much more
binary than the other version.

231
00:14:53,100 --> 00:14:57,950
Of course why the even numbers are working
is that we are used protocol 0 for

232
00:14:57,950 --> 00:14:59,200
that line for the even numbers.

233
00:14:59,200 --> 00:15:02,050
Which was, of course, the most human
readable of all the protocols.

234
00:15:02,050 --> 00:15:04,870
There's probably no good reason why
you would ever want to do it that way.

235
00:15:04,870 --> 00:15:08,150
Except maybe to demonstrate that
the method can't automatically detect

236
00:15:08,150 --> 00:15:10,230
the correct protocol version to use.

237
00:15:10,230 --> 00:15:14,480
Now to talk about security protocol
versions before version two performed

238
00:15:14,480 --> 00:15:18,220
a safety check when but
the problem was the program

239
00:15:18,220 --> 00:15:23,230
would refuse to call functions or
class instructors that weren't marked.

240
00:15:23,230 --> 00:15:25,870
Now these checks were
removed in version 2 on

241
00:15:25,870 --> 00:15:29,330
the basis that the security checks
hadn't been extensively audited.

242
00:15:29,330 --> 00:15:30,430
And as it turns out,

243
00:15:30,430 --> 00:15:34,710
a number of bugs made them easy to
circumvent in most Python versions.

244
00:15:34,710 --> 00:15:35,220
As a result,

245
00:15:35,220 --> 00:15:41,590
it was safe If we publicizing the fact
that pickling uses an insecure protocol,

246
00:15:41,590 --> 00:15:45,680
rather than people trusting a protocol
that haven't been thoroughly checked.

247
00:15:45,680 --> 00:15:48,840
And even protocols that have
been checked such as SSH For

248
00:15:48,840 --> 00:15:51,220
arguments sake, have security flaws, but

249
00:15:51,220 --> 00:15:55,030
the flaws are far more dangerous if
the protocol is trusted, so we think

250
00:15:55,030 --> 00:15:59,530
prior from the python team made the right
decision by publicizing that fact.

251
00:15:59,530 --> 00:16:03,180
So what the message here is that
You should really only unpickling

252
00:16:03,180 --> 00:16:05,230
data that you can trust.

253
00:16:05,230 --> 00:16:08,710
Pickling is fun for storing your program's
data but shouldn't be used when dealing

254
00:16:08,710 --> 00:16:12,530
with data of untrusted sources such as
over the internet for argument's sake.

255
00:16:12,530 --> 00:16:14,820
So rather than repeated several times and
hope that one or

256
00:16:14,820 --> 00:16:18,340
two people actually take the notice
what we'll do is we'll demonstrate

257
00:16:18,340 --> 00:16:21,380
how easy it is to wreak
havoc by unpickling data.

258
00:16:21,380 --> 00:16:24,190
They can't be trusted and
interesting enough this [INAUDIBLE]

259
00:16:24,190 --> 00:16:26,240
will give us a chance to look
at another way to pickle and

260
00:16:26,240 --> 00:16:28,860
un pickle using the dumps and
loads method.

261
00:16:28,860 --> 00:16:30,910
These are very similar to dump and
load, but

262
00:16:30,910 --> 00:16:33,740
instead of writing to
we're reading from a file.

263
00:16:33,740 --> 00:16:38,150
What they do is send data to or get
data from a bytes object, in other words

264
00:16:38,150 --> 00:16:42,810
a sequence of bytes So we're gonna
create a row byte sequence that uses

265
00:16:42,810 --> 00:16:47,500
pickle.load to create an object that it
represents, but exactly the same effect

266
00:16:47,500 --> 00:16:51,300
could be reduced by saving the bytes in
a file and calling the pickle.load method.

267
00:16:51,300 --> 00:16:52,680
So the short program's gonna be here, and

268
00:16:52,680 --> 00:16:56,930
what I'm gonna do is I'm gonna paste
it on screen just to show you.

269
00:16:56,930 --> 00:16:59,650
You wanna use the right one for
your operating system.

270
00:16:59,650 --> 00:17:02,540
And what we're going to do,
is just after everything else there,

271
00:17:02,540 --> 00:17:04,230
we're going to paste that code in there,

272
00:17:04,230 --> 00:17:07,250
leave the pickle out, import cause
that's already at the top of the line.

273
00:17:07,250 --> 00:17:10,980
But we're going to put another one in
here, to separate, separator there.

274
00:17:10,980 --> 00:17:14,369
Now you're going to want to use
one of these lines of code.

275
00:17:14,369 --> 00:17:17,500
and I'm obviously going to use the top
one because I'm running on a Mac, but

276
00:17:17,500 --> 00:17:19,720
if you're a Linux user
you'd also use that, but

277
00:17:19,720 --> 00:17:23,140
if you're a Window's user you're going
to want to use the one on the next line.

278
00:17:23,140 --> 00:17:26,430
If your looking at that carefully you're
probably going to get an idea what's going

279
00:17:26,430 --> 00:17:30,710
to happen here, but I'll leave you in
suspense and not tell you until it's done.

280
00:17:30,710 --> 00:17:33,430
So I'm going to delete that because we
don't want to run a Windows command

281
00:17:33,430 --> 00:17:36,140
on a Mac, but you'd leave that in
if you were on a Windows machine.

282
00:17:36,140 --> 00:17:39,730
So we're going to delete that now And what
we'll do is we'll comment out all this

283
00:17:39,730 --> 00:17:43,420
other code now because
we've already got our file.

284
00:17:44,660 --> 00:17:46,090
We've already got imelda.pickle.

285
00:17:46,090 --> 00:17:49,740
And just pay attention to
imelda.pickle over here.

286
00:17:49,740 --> 00:17:51,580
And I'm gonna run it and
watch what happens.

287
00:17:53,070 --> 00:17:53,870
Did you see what happened?

288
00:17:53,870 --> 00:17:55,300
It actually disappeared.

289
00:17:55,300 --> 00:17:58,630
So obviously, very,
very simple example of what the code

290
00:17:58,630 --> 00:18:03,670
Was the code to remove a file, RM is
remove file in a Linux or Mac machine.

291
00:18:03,670 --> 00:18:09,510
And of course, if you use a Windows
machine, del is the command to delete.

292
00:18:09,510 --> 00:18:13,610
Bottom line is just by executing
that command pickle.load S,

293
00:18:13,610 --> 00:18:15,030
it actually deleted that file.

294
00:18:15,030 --> 00:18:19,260
So this is obviously a very simple
example, and it only deleted a single file

295
00:18:19,260 --> 00:18:22,280
And no real damage was done but
it doesn't take much effort to imagine

296
00:18:22,280 --> 00:18:27,100
far worse results that could result
from using a from a untrusted source.

297
00:18:27,100 --> 00:18:31,630
Now our data was a representation
of an os dot system object

298
00:18:31,630 --> 00:18:34,560
that's used to execute
operating system commands.

299
00:18:34,560 --> 00:18:39,040
And because our stream included
the command to delete the file

300
00:18:39,040 --> 00:18:42,510
The simple act of loading a data file
resulted in another file being deleted.

301
00:18:42,510 --> 00:18:43,990
That's the point I wanna make here.

302
00:18:43,990 --> 00:18:45,090
We're loading a file, and

303
00:18:45,090 --> 00:18:47,270
just loading that file caused
something else to be deleted.

304
00:18:47,270 --> 00:18:50,650
And again, a very simple example but
you can imagine some

305
00:18:50,650 --> 00:18:56,040
spywares could be created or could be
caused by easy untrusted sources here.

306
00:18:56,040 --> 00:18:57,940
So the pickle module itself's very useful.

307
00:18:57,940 --> 00:19:00,030
And it's great for storing and
retrieving your own data.

308
00:19:00,030 --> 00:19:04,150
And there's also ways to prevent
the security problems we've just seen.

309
00:19:04,150 --> 00:19:06,300
So it is possible to use
pickle in production code,

310
00:19:06,300 --> 00:19:08,240
but these methods are quite advanced.

311
00:19:08,240 --> 00:19:10,870
I mean We won't be until
much later in the course.

312
00:19:10,870 --> 00:19:13,650
What you really need to do is heed
the warning at the web server I'm about to

313
00:19:13,650 --> 00:19:14,280
put on the screen.

314
00:19:14,280 --> 00:19:17,750
If you do that you'll have no trouble
using the pickle module to desist your

315
00:19:17,750 --> 00:19:18,470
programs data.

316
00:19:18,470 --> 00:19:21,520
So let's just open that up briefly
before we end the lecture.

317
00:19:22,750 --> 00:19:26,480
Here's the link and
link will be in the resources section and

318
00:19:26,480 --> 00:19:31,400
just have a look at that and it talks more
and more detail about Serialization and

319
00:19:31,400 --> 00:19:35,110
again if you just heed the warnings
on that page you'll be good to go.

320
00:19:35,110 --> 00:19:36,680
Alright, so
we'll see you in the next video.

