1
00:00:00,316 --> 00:00:02,080
- [Instructor] Hi and
welcome back to the course.

2
00:00:02,080 --> 00:00:03,131
In this video, we're finally going

3
00:00:03,131 --> 00:00:05,877
to look into the item list resource

4
00:00:05,877 --> 00:00:09,622
and we're going to make it
use the item model as well.

5
00:00:09,622 --> 00:00:12,819
Let's go into our resources
and then look at our item.

6
00:00:12,819 --> 00:00:14,038
The first thing I'm going to do is,

7
00:00:14,038 --> 00:00:16,562
I'm going to remove the import for sqlite,

8
00:00:16,562 --> 00:00:17,705
because at the end of this video,

9
00:00:17,705 --> 00:00:20,011
we're not gonna need that again.

10
00:00:20,011 --> 00:00:23,063
And then we're gonna go down
here into the get method

11
00:00:23,063 --> 00:00:26,586
and we're also going
to delete all of that.

12
00:00:26,586 --> 00:00:29,351
So what is it that we have to do now

13
00:00:29,351 --> 00:00:32,934
to get exactly the same
that we had before?

14
00:00:33,852 --> 00:00:37,185
So as a reminder, the item list returned

15
00:00:38,841 --> 00:00:42,748
items is a list of item.json.

16
00:00:42,748 --> 00:00:43,998
Item.json here.

17
00:00:45,686 --> 00:00:48,336
How are we gonna get that?

18
00:00:48,336 --> 00:00:50,583
Well I'll give you a hint.

19
00:00:50,583 --> 00:00:51,922
We're gonna return this.

20
00:00:51,922 --> 00:00:54,862
It's all gonna be in one line.

21
00:00:54,862 --> 00:00:58,541
And we're going to use SQLAlchemy.

22
00:00:58,541 --> 00:01:01,791
We're going to use the ItemModel.query.

23
00:01:03,660 --> 00:01:06,382
And remember this is a query builder

24
00:01:06,382 --> 00:01:08,465
and it has a .all method.

25
00:01:10,258 --> 00:01:14,425
.all returns all of the
objects in the database.

26
00:01:16,914 --> 00:01:18,375
That's not how we do it,

27
00:01:18,375 --> 00:01:23,057
but you're gonna need that to
complete this small challenge,

28
00:01:23,057 --> 00:01:24,576
so go ahead and pause the video

29
00:01:24,576 --> 00:01:25,409
and give it a bit of thought.

30
00:01:25,409 --> 00:01:26,952
How would you do that?

31
00:01:26,952 --> 00:01:29,793
Knowing that ItemModel.query.all

32
00:01:29,793 --> 00:01:31,600
gives you all of the objects,

33
00:01:31,600 --> 00:01:34,256
how would you return the json

34
00:01:34,256 --> 00:01:37,506
that you need to return in this method?

35
00:01:41,169 --> 00:01:42,230
Hopefully you've got that,

36
00:01:42,230 --> 00:01:44,535
or pause the video if you need more time.

37
00:01:44,535 --> 00:01:48,194
But the way I would do it is
with a list comprehension.

38
00:01:48,194 --> 00:01:51,187
I would do item.json

39
00:01:51,187 --> 00:01:55,354
for item in ItemModel.query.all.

40
00:01:56,976 --> 00:01:58,459
How easy is that,

41
00:01:58,459 --> 00:02:03,148
compared to the massive
spew we had there earlier?

42
00:02:03,148 --> 00:02:06,274
And best of all, this makes sense.

43
00:02:06,274 --> 00:02:08,176
We're just getting all of the items

44
00:02:08,176 --> 00:02:09,984
and then we're iterating over them.

45
00:02:09,984 --> 00:02:11,589
We no longer have to concern ourselves

46
00:02:11,589 --> 00:02:15,467
with rows and data and
cursors and result sets.

47
00:02:15,467 --> 00:02:17,175
We're just getting all of the items

48
00:02:17,175 --> 00:02:20,989
and then applying the
function of the item,

49
00:02:20,989 --> 00:02:23,656
of the object in each iteration.

50
00:02:24,742 --> 00:02:27,711
Now for completeness'
sake, I will also give you

51
00:02:27,711 --> 00:02:28,940
the way that you could do it

52
00:02:28,940 --> 00:02:32,894
without list comprehension
using Lambdas if you prefer.

53
00:02:32,894 --> 00:02:34,684
So we can do that as well.

54
00:02:34,684 --> 00:02:36,894
That way that you would do using Lambdas

55
00:02:36,894 --> 00:02:41,572
is you would say, list
of the filter lambda x.

56
00:02:41,572 --> 00:02:42,643
Sorry, not filter.

57
00:02:42,643 --> 00:02:47,145
List of map, lambda x.json

58
00:02:47,145 --> 00:02:51,990
for ItemModel.query.all.

59
00:02:51,990 --> 00:02:52,823
Okay?

60
00:02:53,693 --> 00:02:55,095
And once again, what this would do,

61
00:02:55,095 --> 00:02:57,534
is it would apply this function

62
00:02:57,534 --> 00:02:59,951
to each element in this list.

63
00:03:01,109 --> 00:03:02,910
Okay, so that mapping is a mapping

64
00:03:02,910 --> 00:03:05,136
of functions to elements.

65
00:03:05,136 --> 00:03:08,116
And then it would make that into a list.

66
00:03:08,116 --> 00:03:11,164
So it would just essentially
do the same thing.

67
00:03:11,164 --> 00:03:13,792
The list comprehension
way is more Pythonic.

68
00:03:13,792 --> 00:03:16,534
If you're programming in Python only,

69
00:03:16,534 --> 00:03:18,453
it is slightly better.

70
00:03:18,453 --> 00:03:21,350
It's more readable, at least I think so.

71
00:03:21,350 --> 00:03:24,326
And I like it and the
Python community in general

72
00:03:24,326 --> 00:03:25,526
likes it better.

73
00:03:25,526 --> 00:03:28,059
I've heard claims that
it is more performing,

74
00:03:28,059 --> 00:03:29,762
it is faster,

75
00:03:29,762 --> 00:03:32,668
which is possible, I'm not really sure.

76
00:03:32,668 --> 00:03:36,198
I think it's about the same
from the stats that I've read.

77
00:03:36,198 --> 00:03:38,764
So you should only use the map

78
00:03:38,764 --> 00:03:41,431
if you are also programming
in other languages

79
00:03:41,431 --> 00:03:43,035
or if you're working with people

80
00:03:43,035 --> 00:03:45,097
programming in other languages, too.

81
00:03:45,097 --> 00:03:48,690
That will find the list on
the map easier to understand.

82
00:03:48,690 --> 00:03:49,945
Okay?

83
00:03:49,945 --> 00:03:54,318
In addition, the map
filter, reduce methods

84
00:03:54,318 --> 00:03:57,180
are more stackable.

85
00:03:57,180 --> 00:03:59,312
So you can reduce a map

86
00:03:59,312 --> 00:04:02,299
or you can filter the
reduce of the map and so on,

87
00:04:02,299 --> 00:04:05,335
whereas it's not so easy
to use list comprehensions,

88
00:04:05,335 --> 00:04:06,794
inside list comprehensions.

89
00:04:06,794 --> 00:04:08,713
It gets confusing quite quickly.

90
00:04:08,713 --> 00:04:10,447
If you have to do something like this,

91
00:04:10,447 --> 00:04:11,801
I would use a list comprehension

92
00:04:11,801 --> 00:04:13,358
unless you're working with people

93
00:04:13,358 --> 00:04:14,950
programming in other languages

94
00:04:14,950 --> 00:04:18,148
or part of your project
is in a different language

95
00:04:18,148 --> 00:04:22,320
that you are using map,
reduce, and filter in.

96
00:04:22,320 --> 00:04:23,469
So for example,

97
00:04:23,469 --> 00:04:25,409
if a lot of your project is in JavaScript,

98
00:04:25,409 --> 00:04:28,237
and you're using filter
and map in JavaScript,

99
00:04:28,237 --> 00:04:30,773
you probably want to use
map and filter in Python.

100
00:04:30,773 --> 00:04:32,109
If you're only working in Python,

101
00:04:32,109 --> 00:04:34,245
you're responsible for the Python code.

102
00:04:34,245 --> 00:04:37,745
You might as well use list comprehensions.

103
00:04:41,565 --> 00:04:44,565
I think it's slightly more readable.

104
00:04:46,117 --> 00:04:48,305
Okay, so that's everything for this video.

105
00:04:48,305 --> 00:04:49,660
Quite a short one there,

106
00:04:49,660 --> 00:04:52,020
but nevertheless,
remember what I'm saying.

107
00:04:52,020 --> 00:04:54,019
It's important that you remember,

108
00:04:54,019 --> 00:04:57,174
other people may be reading
things differently from you

109
00:04:57,174 --> 00:05:00,420
and may be more accustomed
to other code than you are.

110
00:05:00,420 --> 00:05:03,405
So it's important to
work on a team like that.

111
00:05:03,405 --> 00:05:04,951
Well that's everything for this video.

112
00:05:04,951 --> 00:05:07,656
So thanks for watching and
I'll see you on the next one.

