1
00:00:05,120 --> 00:00:07,190
so as I mentioned towards the end of the

2
00:00:07,190 --> 00:00:09,130
previous video one thing that really

3
00:00:09,130 --> 00:00:11,060
stood out with the various flow charts

4
00:00:11,060 --> 00:00:12,950
we look at is that the new steps that we

5
00:00:12,950 --> 00:00:15,620
added for both artist and album are

6
00:00:15,620 --> 00:00:18,050
pretty much identical and as a result

7
00:00:18,050 --> 00:00:20,060
that sounded like a good candidate for a

8
00:00:20,060 --> 00:00:21,970
function so what we're going to do is

9
00:00:21,970 --> 00:00:24,640
write a function to handle that and i'm

10
00:00:24,640 --> 00:00:26,590
going to create the function called

11
00:00:26,590 --> 00:00:28,000
find object and then we're going to

12
00:00:28,000 --> 00:00:30,130
add that before this method load data

13
00:00:30,130 --> 00:00:31,960
on line 82 so let's go ahead and do that

14
00:00:31,960 --> 00:00:39,530
so def is going to be...

15
00:00:39,530 --> 00:00:42,190
...

16
00:00:42,190 --> 00:01:49,820
...

17
00:01:49,820 --> 00:01:53,420
that's all we have to do to to

18
00:01:53,420 --> 00:01:55,580
create this will now work with both the

19
00:01:55,580 --> 00:01:57,620
artist and the album and you can probably

20
00:01:57,620 --> 00:01:59,210
see there that performs a brute-force

21
00:01:59,210 --> 00:02:01,070
search in that checks the name attribute

22
00:02:01,070 --> 00:02:03,500
for each item in turn and returning the

23
00:02:03,500 --> 00:02:05,750
matching items as I mentioned finds one and

24
00:02:05,750 --> 00:02:08,690
obviously on line 87 returns none

25
00:02:08,690 --> 00:02:12,710
it wasn't able to find anything now there

26
00:02:12,710 --> 00:02:14,510
are faster ways to search the list but

27
00:02:14,510 --> 00:02:16,760
what they generally needs that

28
00:02:16,760 --> 00:02:17,920
require the list to be sorted

29
00:02:17,920 --> 00:02:20,020
so we can't use in here unless

30
00:02:20,020 --> 00:02:21,730
we sort of the list after each addition

31
00:02:21,730 --> 00:02:24,130
and doing that would make the code

32
00:02:24,130 --> 00:02:26,470
far less efficient the code will probably

33
00:02:26,470 --> 00:02:28,390
very slow if their were huge number of

34
00:02:28,390 --> 00:02:31,120
artist but if there were a huge number

35
00:02:31,120 --> 00:02:32,440
then we probably wouldn't be trying to

36
00:02:32,440 --> 00:02:34,630
hold everything in memory so so one

37
00:02:34,630 --> 00:02:36,040
approach with a huge data file would be

38
00:02:36,040 --> 00:02:38,650
to sort the data in the file first and

39
00:02:38,650 --> 00:02:40,510
then process the sorted data rather

40
00:02:40,510 --> 00:02:42,340
than sorting the list after each artist

41
00:02:42,340 --> 00:02:43,120
is added

42
00:02:43,120 --> 00:02:45,130
ok so now we have a function to return

43
00:02:45,130 --> 00:02:47,290
an object if it exists in the list

44
00:02:47,290 --> 00:02:50,350
that's pass to it the this is the find

45
00:02:50,350 --> 00:02:52,540
object function now we've got that we

46
00:02:52,540 --> 00:02:54,940
can modify the load data function to use

47
00:02:54,940 --> 00:02:57,250
new algorithm and use find object

48
00:02:57,250 --> 00:02:58,630
to fix the problem with the earlier

49
00:02:58,630 --> 00:03:00,940
program let's go ahead and do that so

50
00:03:00,940 --> 00:03:02,410
I'm just going to move this down on so

51
00:03:02,410 --> 00:03:03,790
you can see what we're working on with

52
00:03:03,790 --> 00:03:06,460
this method first change we want to make

53
00:03:06,460 --> 00:03:08,680
is this first test here after we've got

54
00:03:08,680 --> 00:03:10,810
a new artist we're going to append that

55
00:03:10,810 --> 00:03:12,790
at the time with the first new artist so

56
00:03:12,790 --> 00:03:16,660
we're going to do an artist list....

57
00:03:16,660 --> 00:03:19,450
...

58
00:03:19,450 --> 00:03:21,040
...

59
00:03:21,040 --> 00:03:23,710
...so if you get down to here with the

60
00:03:23,710 --> 00:03:26,740
else so else if the art the new artist

61
00:03:26,740 --> 00:03:28,900
name is not equal to artist field so we just

62
00:03:28,900 --> 00:03:30,700
read details for new artists we want to

63
00:03:30,700 --> 00:03:32,680
change this a little bit now that's

64
00:03:32,680 --> 00:03:34,210
because we're now not storing the current

65
00:03:34,210 --> 00:03:35,770
album in the current artist collection because

66
00:03:35,770 --> 00:03:38,260
we're dealing with the new artist on line

67
00:03:38,260 --> 00:03:40,600
104 so i'm going to delete this second

68
00:03:40,600 --> 00:03:43,480
line here and still going to change the

69
00:03:43,480 --> 00:03:46,390
comment now to do be corrected what

70
00:03:46,390 --> 00:03:48,130
we're doing we're going at this point we are

71
00:03:48,130 --> 00:03:51,280
retrieve the artist object if there is

72
00:03:51,280 --> 00:03:53,710
one

73
00:03:53,710 --> 00:03:59,290
otherwise there isn't one we're going to

74
00:03:59,290 --> 00:04:03,400
create a new artist object and add it to

75
00:04:03,400 --> 00:04:07,810
the artists list so at this point we can

76
00:04:07,810 --> 00:04:09,610
put new artists going to use that

77
00:04:09,610 --> 00:04:12,340
find object method that's going to be

78
00:04:12,340 --> 00:04:16,959
new artist...

79
00:04:16,959 --> 00:04:19,420
...

80
00:04:19,420 --> 00:05:15,460
...

81
00:05:15,460 --> 00:05:18,430
ok so at this point we've got the code

82
00:05:18,430 --> 00:05:20,380
there for that and so checking to see

83
00:05:20,380 --> 00:05:22,750
whether new artists is none if it is

84
00:05:22,750 --> 00:05:25,390
going to create a new artist field bearing in mind

85
00:05:25,390 --> 00:05:26,890
we are creating it based on the

86
00:05:26,890 --> 00:05:28,540
artists field and we're testing for new

87
00:05:28,540 --> 00:05:30,610
artists here and then we're going to

88
00:05:30,610 --> 00:05:33,640
append the new artist as well to the artist

89
00:05:33,640 --> 00:05:35,040
to the artists list

90
00:05:35,040 --> 00:05:37,210
otherwise if the new artist name or new artist.

91
00:05:37,210 --> 00:05:39,370
name attribute attribute is

92
00:05:39,370 --> 00:05:41,950
not equal to the record we just

93
00:05:41,950 --> 00:05:43,390
or the entry we just read from the file this

94
00:05:43,390 --> 00:05:46,450
artist field that we've got up when we are going

95
00:05:46,450 --> 00:05:47,980
through the artist . text file that's

96
00:05:47,980 --> 00:05:49,900
the case you can see that we've read the

97
00:05:49,900 --> 00:05:51,910
details for a new artist we retrieve the

98
00:05:51,910 --> 00:05:53,890
artist object if there is one using the

99
00:05:53,890 --> 00:05:55,980
find object function otherwise we're

100
00:05:55,980 --> 00:05:57,730
going to create a new artist object and

101
00:05:57,730 --> 00:05:59,230
add it to the artists list you can see me

102
00:05:59,230 --> 00:06:00,540
doing that there's so new artist equals

103
00:06:00,540 --> 00:06:01,880
find object

104
00:06:01,880 --> 00:06:04,550
the artists field the artists list

105
00:06:04,550 --> 00:06:07,010
the two parameters and then coming

106
00:06:07,010 --> 00:06:08,450
checking to see whether that was

107
00:06:08,450 --> 00:06:10,850
none which meant that the artist field

108
00:06:10,850 --> 00:06:13,190
wasn't found under the name attribute

109
00:06:13,190 --> 00:06:14,990
for the in the artists list so that's

110
00:06:14,990 --> 00:06:16,880
the case i'm going to create a new

111
00:06:16,880 --> 00:06:19,610
artist object the artist based on the

112
00:06:19,610 --> 00:06:21,740
artists field and we're going to do artists

113
00:06:21,740 --> 00:06:24,200
.list . append or

114
00:06:24,200 --> 00:06:26,300
artists_list.append that new

115
00:06:26,300 --> 00:06:28,580
artists and we get down to here

116
00:06:28,580 --> 00:06:30,080
setting new album to none that was

117
00:06:30,080 --> 00:06:31,370
similar to this code here which I can

118
00:06:31,370 --> 00:06:35,930
now take out this duplication so

119
00:06:35,930 --> 00:06:39,530
move this up so moving down

120
00:06:39,530 --> 00:06:41,060
there we're going to be

121
00:06:41,060 --> 00:06:43,640
processing the new album so

122
00:06:43,640 --> 00:06:45,710
if new album is none the new album

123
00:06:45,710 --> 00:06:47,180
equals album we're creating that new

124
00:06:47,180 --> 00:06:48,530
albums and that doesn't need to change

125
00:06:48,530 --> 00:06:50,480
at all but the other thing or the one

126
00:06:50,480 --> 00:06:51,770
thing we do want to do is you want to

127
00:06:51,770 --> 00:06:54,320
work at the point of creating a new

128
00:06:54,320 --> 00:06:56,450
album we're going to be adding it to the

129
00:06:56,450 --> 00:06:57,680
list so...

130
00:06:57,680 --> 00:07:02,120
....

131
00:07:02,120 --> 00:07:03,710
...so we're having it at the point

132
00:07:03,710 --> 00:07:05,900
that we've created that but otherwise

133
00:07:05,900 --> 00:07:08,240
if the new album . name is not equal to

134
00:07:08,240 --> 00:07:11,090
the album field then we just read the

135
00:07:11,090 --> 00:07:14,030
new album for the current

136
00:07:14,030 --> 00:07:15,620
but again we need

137
00:07:15,620 --> 00:07:17,390
to change because now we're not storing

138
00:07:17,390 --> 00:07:18,740
the album at this point we're doing

139
00:07:18,740 --> 00:07:20,450
something styles are different here now

140
00:07:20,450 --> 00:07:22,160
based on the algorithms we talked about

141
00:07:22,160 --> 00:07:24,380
in the previous video we're going to

142
00:07:24,380 --> 00:07:26,030
retrieve the album object there is one

143
00:07:26,030 --> 00:07:34,610
...

144
00:07:34,610 --> 00:07:36,140
otherwise we are going to create the new

145
00:07:36,140 --> 00:07:38,120
album object and stored in the artists

146
00:07:38,120 --> 00:07:41,570
collection

147
00:07:41,570 --> 00:07:49,790
...ok so that's what we're gonna be doing

148
00:07:49,790 --> 00:07:52,640
so to do that we need to add that find

149
00:07:52,640 --> 00:07:55,490
object call...

150
00:07:55,490 --> 00:07:57,470
...

151
00:07:57,470 --> 00:08:16,400
...

152
00:08:16,400 --> 00:08:18,080
in other words if we found that this new

153
00:08:18,080 --> 00:08:20,230
album didn't exist in the name attribute

154
00:08:20,230 --> 00:08:21,980
for any entry in our list that was

155
00:08:21,980 --> 00:08:24,950
passed then we're going to create the

156
00:08:24,950 --> 00:08:30,860
object so...

157
00:08:30,860 --> 00:08:34,700
...

158
00:08:34,700 --> 00:08:41,030
...

159
00:08:41,030 --> 00:08:42,500
and we're passing the parameters to make

160
00:08:42,500 --> 00:08:45,350
a valid album basically swaps so that new

161
00:08:45,350 --> 00:08:47,350
album is created before calling at album

162
00:08:47,350 --> 00:08:49,190
which wasn't the case in the previous

163
00:08:49,190 --> 00:08:51,830
code again is very similar to how we

164
00:08:51,830 --> 00:08:53,240
process the new artists but we're

165
00:08:53,240 --> 00:08:54,740
obviously dealing with albums at this

166
00:08:54,740 --> 00:08:56,780
point and we still want to keep down

167
00:08:56,780 --> 00:09:00,230
here that code valid

168
00:09:00,230 --> 00:09:02,540
because you want to create a new song object

169
00:09:02,540 --> 00:09:04,100
at that point that's going to be new

170
00:09:04,100 --> 00:09:06,290
song is still equal to song the song field and new

171
00:09:06,290 --> 00:09:08,630
artist that still correct then likewise

172
00:09:08,630 --> 00:09:11,300
for the album new album add song new

173
00:09:11,300 --> 00:09:12,680
song that's going to be correct as well

174
00:09:12,680 --> 00:09:15,400
now that we've done that if we just moved

175
00:09:15,400 --> 00:09:18,200
and look at this last bit of code we

176
00:09:18,200 --> 00:09:20,120
don't need that processes code anymore

177
00:09:20,120 --> 00:09:22,010
because with the code with built-in now

178
00:09:22,010 --> 00:09:24,740
handles those last lines of codes for

179
00:09:24,740 --> 00:09:26,180
the last entries that before we had to

180
00:09:26,180 --> 00:09:27,590
do a separate check so we can delete

181
00:09:27,590 --> 00:09:31,460
that and just returned at that point so

182
00:09:31,460 --> 00:09:33,230
these few changes that we made change

183
00:09:33,230 --> 00:09:34,760
the program to use the second algorithm

184
00:09:34,760 --> 00:09:36,980
that we discussed and add new object

185
00:09:36,980 --> 00:09:38,060
to the list we doing that before

186
00:09:38,060 --> 00:09:40,040
populating them with their albums or

187
00:09:40,040 --> 00:09:42,980
souls so we should be good to go that's

188
00:09:42,980 --> 00:09:46,460
our revised algorithm there now our load

189
00:09:46,460 --> 00:09:50,210
data method or function should say so

190
00:09:50,210 --> 00:09:51,740
what we're doing now is we're going over here

191
00:09:51,740 --> 00:09:53,120
we're going to delete check folder.txt

192
00:09:53,120 --> 00:09:54,830
because we're going to

193
00:09:54,830 --> 00:09:58,100
go through and create again we just want

194
00:09:58,100 --> 00:09:59,540
to make sure that it is actually

195
00:09:59,540 --> 00:10:01,610
working so i'm just going to select and

196
00:10:01,610 --> 00:10:04,820
select delete delete

197
00:10:04,820 --> 00:10:06,620
and obviously that's giving us a reminder

198
00:10:06,620 --> 00:10:07,880
that by the way your using that

199
00:10:07,880 --> 00:10:10,100
its found a reference to check file.txt

200
00:10:10,100 --> 00:10:11,480
but of course we're creating that we're

201
00:10:11,480 --> 00:10:13,520
aware of that and we can ignore that

202
00:10:13,520 --> 00:10:14,540
because you can see up here it does say

203
00:10:14,540 --> 00:10:16,250
these occurrences won't be changed which

204
00:10:16,250 --> 00:10:17,810
you would expect in this case so do

205
00:10:17,810 --> 00:10:19,610
refactor which is really just a

206
00:10:19,610 --> 00:10:22,220
deletion so now if we run this is just

207
00:10:22,220 --> 00:10:26,480
do that again we'll run it and let's now

208
00:10:26,480 --> 00:10:28,070
compare the two files against so going to

209
00:10:28,070 --> 00:10:34,340
select got an error here by the looks of

210
00:10:34,340 --> 00:10:38,150
it so probably a typo I made let's just go

211
00:10:38,150 --> 00:10:39,560
back and have a look at that that's

212
00:10:39,560 --> 00:10:42,170
referring to that code of here and

213
00:10:42,170 --> 00:10:44,840
looking at that objects got has no attribute

214
00:10:44,840 --> 00:10:46,910
albums think i made a typo their so that's

215
00:10:46,910 --> 00:10:51,680
on line 122 let's look at that and

216
00:10:51,680 --> 00:10:54,950
defining objects album field and i put new

217
00:10:54,950 --> 00:10:57,290
album albums and of course it should be new

218
00:10:57,290 --> 00:11:01,790
artist . albums and I

219
00:11:01,790 --> 00:11:03,560
think the rest is now correct

220
00:11:03,560 --> 00:11:05,450
ok so i think that's now correct let's

221
00:11:05,450 --> 00:11:07,040
just try running again which would hope

222
00:11:07,040 --> 00:11:08,570
not get an error this time

223
00:11:08,570 --> 00:11:10,880
ok that was better also notice that

224
00:11:10,880 --> 00:11:13,820
check file. txt showed up which is

225
00:11:13,820 --> 00:11:15,680
almost a good sign that it's working

226
00:11:15,680 --> 00:11:17,990
so let's now do what I wanted to do

227
00:11:17,990 --> 00:11:20,390
previously is select both those files and

228
00:11:20,390 --> 00:11:21,320
compare them

229
00:11:21,320 --> 00:11:23,630
so selecting both of them to the

230
00:11:23,630 --> 00:11:26,330
view and compare files i'll just go back

231
00:11:26,330 --> 00:11:30,170
and open that comparison and we can see

232
00:11:30,170 --> 00:11:32,000
now there aren't any differences to the file

233
00:11:32,000 --> 00:11:34,460
and that is actually good news so

234
00:11:34,460 --> 00:11:36,890
note that only a single artist has been

235
00:11:36,890 --> 00:11:38,870
created for ZZ type it's albums get

236
00:11:38,870 --> 00:11:41,180
printed out altogether but because the

237
00:11:41,180 --> 00:11:43,340
list is not sorted they appear in the order

238
00:11:43,340 --> 00:11:45,800
that the artist was added and of course

239
00:11:45,800 --> 00:11:48,530
ZZTop was added just after Aerosmith and

240
00:11:48,530 --> 00:11:50,270
also all the aerosmith albums are

241
00:11:50,270 --> 00:11:52,040
printed together which is a sign that

242
00:11:52,040 --> 00:11:54,230
there's only one aerosmith within the

243
00:11:54,230 --> 00:11:56,540
artists so if i scroll to the end of the

244
00:11:56,540 --> 00:11:59,960
file just got to check down here now i'm

245
00:11:59,960 --> 00:12:01,070
scrolling down the left side of the

246
00:12:01,070 --> 00:12:05,750
window notice some more ZZTop

247
00:12:05,750 --> 00:12:07,760
albums here to the left hand side so

248
00:12:07,760 --> 00:12:08,380
that's good

249
00:12:08,380 --> 00:12:10,730
on and when you are scrolling make sure

250
00:12:10,730 --> 00:12:16,630
you use the scroll bar on this side sort

251
00:12:16,630 --> 00:12:24,550
of move up because if you come over here

252
00:12:24,550 --> 00:12:25,930
and use the scroll bar you get a

253
00:12:25,930 --> 00:12:27,170
different result because we're scrolling

254
00:12:27,170 --> 00:12:30,790
now the check file.txt

255
00:12:30,790 --> 00:12:32,860
instead of our original albums . txt

256
00:12:32,860 --> 00:12:35,290
the bottom line now is there now a

257
00:12:35,290 --> 00:12:37,210
problem which we expected because we

258
00:12:37,210 --> 00:12:38,710
did change the order of those files

259
00:12:38,710 --> 00:12:41,500
ourselves so again just a note here you

260
00:12:41,500 --> 00:12:43,000
can't scroll down either side of the

261
00:12:43,000 --> 00:12:45,010
comparison display further down the end

262
00:12:45,010 --> 00:12:46,360
of the file so make sure that when

263
00:12:46,360 --> 00:12:48,790
you're scrolling you scroll bar for albums

264
00:12:48,790 --> 00:12:51,190
. txt rather than check file. txt to

265
00:12:51,190 --> 00:12:53,440
get the end of it just looking down to

266
00:12:53,440 --> 00:12:59,650
the bottom we can see that theirs line

267
00:12:59,650 --> 00:13:01,840
684 and we can see we've got that for

268
00:13:01,840 --> 00:13:04,930
both our albums . txt and check file.

269
00:13:04,930 --> 00:13:08,170
txt and just going back to our output

270
00:13:08,170 --> 00:13:10,210
looking this down the bottom it now correctly

271
00:13:10,210 --> 00:13:13,150
says 28 artists and the good thing was

272
00:13:13,150 --> 00:13:15,170
that the ZZ top albums and songs will

273
00:13:15,170 --> 00:13:16,990
match now match even though they're not

274
00:13:16,990 --> 00:13:19,240
in the same position in each file so it

275
00:13:19,240 --> 00:13:21,040
looks as I we've got this working

276
00:13:21,040 --> 00:13:23,740
correctly which is fantastic and again

277
00:13:23,740 --> 00:13:31,280
just to note we go back and have a look

278
00:13:31,280 --> 00:13:34,400
this collection here

279
00:13:34,400 --> 00:13:37,220
ZZTop antenna that was the part that

280
00:13:37,220 --> 00:13:39,380
outside of order come back

281
00:13:39,380 --> 00:13:44,720
here we've got the ZZ Top

282
00:13:44,720 --> 00:13:46,190
we've got those but we've still got the

283
00:13:46,190 --> 00:13:51,170
other ZZ top albums and information down

284
00:13:51,170 --> 00:13:52,610
the bottom of the file as well so that is

285
00:13:52,610 --> 00:13:54,140
good it those look like things are working

286
00:13:54,140 --> 00:13:56,240
fine now I'm going to finish the video

287
00:13:56,240 --> 00:13:58,040
out here now in the next video we've got

288
00:13:58,040 --> 00:14:00,380
a mini challenge for you so let's work

289
00:14:00,380 --> 00:14:06,520
on that in the next video

