1
00:00:00,558 --> 00:00:02,791
- [Narrator] Hi and
welcome back to the course.

2
00:00:02,791 --> 00:00:05,514
In this video we are going to be improving

3
00:00:05,514 --> 00:00:09,303
these things a bit by
using filter functions.

4
00:00:09,303 --> 00:00:12,998
Instead of this iteration,
which looks a bit ugly,

5
00:00:12,998 --> 00:00:16,089
we're going to do something

6
00:00:16,089 --> 00:00:20,256
which is item equals filter.

7
00:00:21,596 --> 00:00:22,836
And now what we're gonna do is

8
00:00:22,836 --> 00:00:25,967
we're going to filter the list of items

9
00:00:25,967 --> 00:00:29,231
that is our in memory database.

10
00:00:29,231 --> 00:00:32,111
And we are going to just get the one

11
00:00:32,111 --> 00:00:33,944
that matches the name.

12
00:00:35,143 --> 00:00:37,905
Now if you've looked at the lecture

13
00:00:37,905 --> 00:00:39,579
in lambda functions,

14
00:00:39,579 --> 00:00:40,740
we discussed briefly the

15
00:00:40,740 --> 00:00:42,502
filter function there and

16
00:00:42,502 --> 00:00:45,633
you may be able to
implement this on your own.

17
00:00:45,633 --> 00:00:48,173
I would recommend that
you try that just now.

18
00:00:48,173 --> 00:00:49,876
It'll improve your learning if you try

19
00:00:49,876 --> 00:00:51,793
this on your own first.

20
00:00:55,239 --> 00:00:57,077
Now that you've done that,

21
00:00:57,077 --> 00:00:59,707
the solution is to say filter

22
00:00:59,707 --> 00:01:01,824
and then we're going to write a lambda

23
00:01:01,824 --> 00:01:04,968
function for what we're
going to filter by.

24
00:01:04,968 --> 00:01:05,875
So what we're gonna do is say

25
00:01:05,875 --> 00:01:09,958
lambda x: x name equal equal name

26
00:01:11,467 --> 00:01:13,629
and then comma items.

27
00:01:13,629 --> 00:01:16,126
So the filter function
takes two arguments.

28
00:01:16,126 --> 00:01:19,780
First a filtering function

29
00:01:19,780 --> 00:01:22,795
and then the list of items
that we're filtering.

30
00:01:22,795 --> 00:01:24,072
Essentially what's going to happen is

31
00:01:24,072 --> 00:01:25,276
exactly the same thing that happened

32
00:01:25,276 --> 00:01:26,267
in the foreloop.

33
00:01:26,267 --> 00:01:28,124
We're going to go through each item,

34
00:01:28,124 --> 00:01:29,940
execute this function and

35
00:01:29,940 --> 00:01:33,944
see if the item's name x matches

36
00:01:33,944 --> 00:01:36,751
the name which is the parameter.

37
00:01:36,751 --> 00:01:39,183
And if it does, then
we're gonna return it.

38
00:01:39,183 --> 00:01:42,434
So how do we do it essentially?

39
00:01:42,434 --> 00:01:44,884
How do we return the item?

40
00:01:44,884 --> 00:01:48,693
The filter function
doesn't return an item.

41
00:01:48,693 --> 00:01:52,508
Potentially there could be many items

42
00:01:52,508 --> 00:01:54,799
generated by this filter function.

43
00:01:54,799 --> 00:01:57,980
So it cannot possibly return a single one.

44
00:01:57,980 --> 00:02:00,960
However it also doesn't return a list.

45
00:02:00,960 --> 00:02:03,370
It returns a filter object and

46
00:02:03,370 --> 00:02:05,323
a filter object is an object that you can

47
00:02:05,323 --> 00:02:07,408
call a couple of methods on,

48
00:02:07,408 --> 00:02:08,491
such as list.

49
00:02:09,343 --> 00:02:11,017
And if you call the list method

50
00:02:11,017 --> 00:02:13,146
on this filter object, guess what?

51
00:02:13,146 --> 00:02:15,582
It returns a list of all the items

52
00:02:15,582 --> 00:02:18,560
that matched the filter function.

53
00:02:18,560 --> 00:02:21,553
Now we should only have one item

54
00:02:21,553 --> 00:02:23,298
with a given name,

55
00:02:23,298 --> 00:02:25,553
so we know that this filter function

56
00:02:25,553 --> 00:02:27,511
will most naturally only

57
00:02:27,511 --> 00:02:30,089
return a single item.

58
00:02:30,089 --> 00:02:31,436
So instead of list, which gives

59
00:02:31,436 --> 00:02:33,096
us a list of items and

60
00:02:33,096 --> 00:02:36,384
that would give us a
list of a single item,

61
00:02:36,384 --> 00:02:40,181
we can use next and that gives us

62
00:02:40,181 --> 00:02:44,722
the first item found by
this filter function.

63
00:02:44,722 --> 00:02:47,160
Do notice that we can call next on

64
00:02:47,160 --> 00:02:49,097
this filter function and that would

65
00:02:49,097 --> 00:02:50,887
give us the first item matched

66
00:02:50,887 --> 00:02:52,892
by this filter function,

67
00:02:52,892 --> 00:02:54,658
but we can call next again if there are

68
00:02:54,658 --> 00:02:55,873
more items and that would give us the

69
00:02:55,873 --> 00:02:58,132
second item and then the third item

70
00:02:58,132 --> 00:02:59,699
and so on.

71
00:02:59,699 --> 00:03:00,925
In our filter function, there is only

72
00:03:00,925 --> 00:03:02,882
goin to be one item so we're never gonna

73
00:03:02,882 --> 00:03:06,380
want to call next more than once.

74
00:03:06,380 --> 00:03:09,545
And also do remember, next can raise

75
00:03:09,545 --> 00:03:12,257
an error and it can break our programme

76
00:03:12,257 --> 00:03:14,507
if there are no items left.

77
00:03:15,858 --> 00:03:17,751
For example, if we don't have any items

78
00:03:17,751 --> 00:03:19,807
in our database and we try to

79
00:03:19,807 --> 00:03:21,763
filter it looking for an item

80
00:03:21,763 --> 00:03:24,286
and then we call next on that.

81
00:03:24,286 --> 00:03:26,059
It would break because there are

82
00:03:26,059 --> 00:03:28,754
no items that match the name.

83
00:03:28,754 --> 00:03:32,165
So what we can do is say comma none

84
00:03:32,165 --> 00:03:34,059
and then what happens is that

85
00:03:34,059 --> 00:03:38,303
if the next function doesn't find an item,

86
00:03:38,303 --> 00:03:40,617
it will just return none.

87
00:03:40,617 --> 00:03:44,322
So that's kind of a default value there.

88
00:03:44,322 --> 00:03:45,455
So what does that mean?

89
00:03:45,455 --> 00:03:48,872
Well, that we can say return item is item

90
00:03:49,942 --> 00:03:51,722
and now this item variable which we

91
00:03:51,722 --> 00:03:55,211
declared up here is going to be either

92
00:03:55,211 --> 00:03:57,932
the item that matches the name

93
00:03:57,932 --> 00:03:59,554
or it is going to be none

94
00:03:59,554 --> 00:04:02,243
if none of them matched.

95
00:04:02,243 --> 00:04:04,308
We don't want to return 404 though,

96
00:04:04,308 --> 00:04:07,574
so what we'll do is use
a ternary if statement.

97
00:04:07,574 --> 00:04:11,585
200 if item is not none

98
00:04:11,585 --> 00:04:13,002
else 404.

99
00:04:13,956 --> 00:04:16,507
So we're going to return a code 200

100
00:04:16,507 --> 00:04:18,420
if the item is not none

101
00:04:18,420 --> 00:04:20,117
and if the item was none,

102
00:04:20,117 --> 00:04:22,700
then we're going to return 404.

103
00:04:23,573 --> 00:04:27,214
Now item is not none can be shorthanded

104
00:04:27,214 --> 00:04:30,674
and simplified to be just item.

105
00:04:30,674 --> 00:04:33,963
So 200 if the item exists essentially

106
00:04:33,963 --> 00:04:35,296
and if not, 404.

107
00:04:36,882 --> 00:04:37,715
Okay?

108
00:04:38,922 --> 00:04:41,413
Now we've decided that we're normally

109
00:04:41,413 --> 00:04:44,504
only gonna have unique names for items,

110
00:04:44,504 --> 00:04:46,871
so let's ensure that.

111
00:04:46,871 --> 00:04:49,432
So I'm gonna once again use this piece

112
00:04:49,432 --> 00:04:51,397
of code here and I'm gonna say

113
00:04:51,397 --> 00:04:55,104
If the sort of next item

114
00:04:55,104 --> 00:04:56,278
in the filter function,

115
00:04:56,278 --> 00:04:59,611
so the only item that matches this name,

116
00:05:01,516 --> 00:05:02,516
is not none.

117
00:05:05,396 --> 00:05:07,313
So what does this mean?

118
00:05:11,418 --> 00:05:14,294
If we found an item matching this name

119
00:05:14,294 --> 00:05:16,801
and that item is not none,

120
00:05:16,801 --> 00:05:20,775
it means there is an item
that matches the name.

121
00:05:20,775 --> 00:05:23,808
And if there is, we don't want to

122
00:05:23,808 --> 00:05:25,335
create a new item.

123
00:05:25,335 --> 00:05:27,377
All we want to do is return.

124
00:05:27,377 --> 00:05:29,626
And we're gonna return
a message something like

125
00:05:29,626 --> 00:05:33,793
An item with name blah already exists.

126
00:05:37,595 --> 00:05:40,178
Make sure I spell things right.

127
00:05:41,169 --> 00:05:45,002
Dot format, delete that, name.

128
00:05:46,481 --> 00:05:47,314
Okay?

129
00:05:48,706 --> 00:05:52,673
So to recap, if we found an item

130
00:05:52,673 --> 00:05:54,840
and that item is not none,

131
00:05:55,887 --> 00:05:58,422
then we're going to
return this message here.

132
00:05:58,422 --> 00:06:01,523
Remember is not None can be omitted

133
00:06:01,523 --> 00:06:03,663
because that is the default.

134
00:06:03,663 --> 00:06:06,043
So if this thing here exists,

135
00:06:06,043 --> 00:06:07,956
that means it is not none.

136
00:06:07,956 --> 00:06:10,627
Then we're gonna return this message.

137
00:06:10,627 --> 00:06:14,153
Now what is the cause for

138
00:06:14,153 --> 00:06:17,229
the item already existing?

139
00:06:17,229 --> 00:06:18,841
That is a bit of trick question

140
00:06:18,841 --> 00:06:20,594
because the cause for the item name

141
00:06:20,594 --> 00:06:22,427
already existing is not that we've

142
00:06:22,427 --> 00:06:23,942
added it before.

143
00:06:23,942 --> 00:06:27,203
This is not our application's fault.

144
00:06:27,203 --> 00:06:30,153
This is a bad request because

145
00:06:30,153 --> 00:06:32,271
the client should have verified

146
00:06:32,271 --> 00:06:34,708
that this name already exists or

147
00:06:34,708 --> 00:06:35,888
at least we can tell them that

148
00:06:35,888 --> 00:06:37,094
this name already exists but

149
00:06:37,094 --> 00:06:38,958
it is their fault because they

150
00:06:38,958 --> 00:06:41,613
made a request with an invalid name.

151
00:06:41,613 --> 00:06:44,307
It's invalid because
it's already in this app.

152
00:06:44,307 --> 00:06:46,382
So what should the code be?

153
00:06:46,382 --> 00:06:50,549
The status code for this message.

154
00:06:53,459 --> 00:06:55,292
You got it, it is 400.

155
00:06:56,392 --> 00:06:58,504
400 is bad request and that is essentially

156
00:06:58,504 --> 00:07:00,087
what happened here.

157
00:07:01,146 --> 00:07:02,781
Well, that's everything for this video.

158
00:07:02,781 --> 00:07:03,925
Hopefully you learned something and

159
00:07:03,925 --> 00:07:06,678
we have now improved the code here

160
00:07:06,678 --> 00:07:08,478
to have some error control and

161
00:07:08,478 --> 00:07:10,966
also to just be slightly nicer

162
00:07:10,966 --> 00:07:13,442
to read and to edit.

163
00:07:13,442 --> 00:07:14,546
Thanks for everything and

164
00:07:14,546 --> 00:07:15,881
I'll see you in the very next video.

