1
1

00:00:01,380  -->  00:00:02,670
<v Jose>Hey guys, welcome back.</v>
2

2

00:00:02,670  -->  00:00:04,370
In this video, we're going to start
3

3

00:00:04,370  -->  00:00:06,940
working on our item model.
4

4

00:00:06,940  -->  00:00:09,420
A model, as we discussed earlier on,
5

5

00:00:09,420  -->  00:00:13,840
is something that our application deals with internally.
6

6

00:00:13,840  -->  00:00:16,820
So whenever we want to use
7

7

00:00:16,820  -->  00:00:18,830
a thing in our programme
8

8

00:00:18,830  -->  00:00:21,280
and we want to be able to think of that thing
9

9

00:00:21,280  -->  00:00:23,570
as if it were something in the real world,
10

10

00:00:23,570  -->  00:00:25,833
we often will wanna create a model for it.
11

11

00:00:26,920  -->  00:00:29,430
For example, our application will have a model,
12

12

00:00:29,430  -->  00:00:33,440
which is the item, item that belongs to a store
13

13

00:00:33,440  -->  00:00:37,410
and that you can get a price of and things like that.
14

14

00:00:37,410  -->  00:00:40,810
So all of those interactions that we can do with an item,
15

15

00:00:40,810  -->  00:00:44,850
thing in the real world, like check where it is sold
16

16

00:00:44,850  -->  00:00:48,920
or find out what price it is or get its name,
17

17

00:00:48,920  -->  00:00:52,390
all those sort of things we can put into our item model.
18

18

00:00:52,390  -->  00:00:54,500
So we're going to go ahead and create that
19

19

00:00:54,500  -->  00:00:58,020
by going into Models and then right-click it
20

20

00:00:58,020  -->  00:01:01,090
and go to New Python file
21

21

00:01:01,090  -->  00:01:03,890
and we're gonna create one called Item.
22

22

00:01:03,890  -->  00:01:06,513
Now, before we get into coding the item,
23

23

00:01:07,580  -->  00:01:11,040
you have to think about what you want it to be able to do
24

24

00:01:11,040  -->  00:01:13,163
and how you want to interact with it.
25

25

00:01:14,020  -->  00:01:18,380
So before we code it, I'm going to just cut this,
26

26

00:01:18,380  -->  00:01:21,230
so keep it saved in your clipboard,
27

27

00:01:21,230  -->  00:01:26,203
and I'm gonna think about how we want to use the item model.
28

28

00:01:27,380  -->  00:01:31,050
Ideally, I would like to be able to create an item,
29

29

00:01:31,050  -->  00:01:34,470
give it the things that an item needs,
30

30

00:01:34,470  -->  00:01:37,860
and get it to load the price
31

31

00:01:37,860  -->  00:01:40,260
or give me the price of the item.
32

32

00:01:40,260  -->  00:01:43,400
So basically, I would like to be able to do something like
33

33

00:01:43,400  -->  00:01:47,880
item is a new item, give it whatever the item needs,
34

34

00:01:47,880  -->  00:01:51,920
which, in my case, is going to be a URL, a tag_name,
35

35

00:01:51,920  -->  00:01:53,920
and a query.
36

36

00:01:53,920  -->  00:01:56,720
And then I would like to be able to item.load_price.
37

37

00:01:58,460  -->  00:02:00,720
Okay, this is
38

38

00:02:00,720  -->  00:02:04,370
essentially the interface of the item,
39

39

00:02:04,370  -->  00:02:07,603
what the item interactions look like.
40

40

00:02:09,470  -->  00:02:12,640
So in order to do this, we do have to,
41

41

00:02:12,640  -->  00:02:14,300
of course, go ahead and create the item
42

42

00:02:14,300  -->  00:02:16,450
and the item is going to have two methods,
43

43

00:02:16,450  -->  00:02:19,220
a constructor method so that we can pass it
44

44

00:02:19,220  -->  00:02:21,710
the URL, the tag_name, and the query,
45

45

00:02:21,710  -->  00:02:24,040
and it's going to have a load_price method
46

46

00:02:24,040  -->  00:02:26,020
that is going to go our web store
47

47

00:02:26,020  -->  00:02:28,250
and it's going to load the price.
48

48

00:02:28,250  -->  00:02:30,270
Essentially, what we're doing is we're taking the code
49

49

00:02:30,270  -->  00:02:34,060
that we had here in app.py at the start of this video
50

50

00:02:34,060  -->  00:02:38,170
and we're going to put it into an item model, an item class,
51

51

00:02:38,170  -->  00:02:42,270
and sort of split it out so it looks a bit better.
52

52

00:02:42,270  -->  00:02:45,950
We're gonna do class Item here in item.py
53

53

00:02:45,950  -->  00:02:48,480
and then we're gonna have an init method,
54

54

00:02:48,480  -->  00:02:50,480
which is gonna do nothing for now.
55

55

00:02:50,480  -->  00:02:53,270
And that's what pass means in Python.
56

56

00:02:53,270  -->  00:02:55,080
It means do nothing.
57

57

00:02:55,080  -->  00:02:57,713
And then we're gonna have a load_price method.
58

58

00:02:59,130  -->  00:03:01,780
And again, pass because we haven't yet decided
59

59

00:03:01,780  -->  00:03:03,320
how we're gonna split this out.
60

60

00:03:03,320  -->  00:03:06,920
Now, in here, I'm gonna paste what I had in my app.py
61

61

00:03:06,920  -->  00:03:08,620
just so we don't lose it
62

62

00:03:08,620  -->  00:03:13,620
and we're going to move this code over into these methods.
63

63

00:03:14,320  -->  00:03:16,560
Now, when you create an item,
64

64

00:03:16,560  -->  00:03:18,850
what we want to do is we want to store
65

65

00:03:18,850  -->  00:03:22,830
all the relevant properties inside the object
66

66

00:03:22,830  -->  00:03:25,320
so that this method can then access
67

67

00:03:25,320  -->  00:03:27,930
those properties that are stored inside itself,
68

68

00:03:27,930  -->  00:03:29,690
the object itself.
69

69

00:03:29,690  -->  00:03:33,070
So those are gonna be the URL,
70

70

00:03:33,070  -->  00:03:35,573
the tag_name, and the query.
71

71

00:03:36,640  -->  00:03:38,090
So let's do that.
72

72

00:03:38,090  -->  00:03:43,090
Self.url is going to be, in this case, this one here.
73

73

00:03:43,150  -->  00:03:45,200
And we're going to improve on this a little bit
74

74

00:03:45,200  -->  00:03:47,210
as we progress through the video
75

75

00:03:47,210  -->  00:03:51,143
so don't be too worried if something isn't what you expect.
76

76

00:03:52,330  -->  00:03:56,693
And then self.query is going to be this.
77

77

00:03:58,320  -->  00:03:59,153
Cool.
78

78

00:03:59,153  -->  00:04:01,760
So of course, we're gonna have to import these things,
79

79

00:04:01,760  -->  00:04:03,730
but we're gonna do that at the top of the file.
80

80

00:04:03,730  -->  00:04:06,810
We'll have to do that because the load_price method
81

81

00:04:06,810  -->  00:04:08,693
is going to require these things.
82

82

00:04:09,890  -->  00:04:12,520
So what is the load_price method gonna do?
83

83

00:04:12,520  -->  00:04:14,740
Well, it's basically gonna do all of this
84

84

00:04:15,830  -->  00:04:19,490
because it's going to go ahead and
85

85

00:04:19,490  -->  00:04:22,440
request this URL, it's going to get the content,
86

86

00:04:22,440  -->  00:04:25,340
then it's going to parse it using Beautiful Soup,
87

87

00:04:25,340  -->  00:04:29,290
and then it's gonna find the element that is related
88

88

00:04:29,290  -->  00:04:32,240
to this item, this tag_name and this query,
89

89

00:04:32,240  -->  00:04:35,263
and then it's gonna sort of get the price and so on.
90

90

00:04:36,583  -->  00:04:38,760
So of course, when we paste that in,
91

91

00:04:38,760  -->  00:04:40,570
we do get a bunch of grey lines
92

92

00:04:40,570  -->  00:04:44,860
and these lines say, Shadows name response from outer scope.
93

93

00:04:44,860  -->  00:04:46,300
So this is not an error.
94

94

00:04:46,300  -->  00:04:48,780
The grey lines are not an error in PyCharm.
95

95

00:04:48,780  -->  00:04:50,220
But what it does say is that
96

96

00:04:50,220  -->  00:04:53,300
you have this variable called response inside this method,
97

97

00:04:53,300  -->  00:04:55,190
but you also have a variable called response
98

98

00:04:55,190  -->  00:04:56,790
outside the method.
99

99

00:04:56,790  -->  00:05:00,510
And so whenever you use the response variables somewhere,
100

100

00:05:00,510  -->  00:05:03,240
so just here, really you're using this one
101

101

00:05:03,240  -->  00:05:05,790
and not the one on the outer scope.
102

102

00:05:05,790  -->  00:05:07,080
And that's fine, that's what we wanna do.
103

103

00:05:07,080  -->  00:05:09,190
But we no longer need this code here
104

104

00:05:09,190  -->  00:05:10,480
because we've already pasted it in.
105

105

00:05:10,480  -->  00:05:11,930
So now, we're gonna delete that
106

106

00:05:11,930  -->  00:05:14,010
and notice that the grey lines go away.
107

107

00:05:14,010  -->  00:05:16,010
Now, we do have some red lines.
108

108

00:05:16,010  -->  00:05:18,610
The red lines are because the URL, tag_name,
109

109

00:05:18,610  -->  00:05:20,710
and query variables that we have earlier
110

110

00:05:20,710  -->  00:05:22,520
are no longer defined.
111

111

00:05:22,520  -->  00:05:24,730
Now that we're inside the method,
112

112

00:05:24,730  -->  00:05:28,720
we do have to use the item properties.
113

113

00:05:28,720  -->  00:05:31,440
So instead of the big, all-caps URL,
114

114

00:05:31,440  -->  00:05:34,470
which was a constant defined outside of the class,
115

115

00:05:34,470  -->  00:05:36,410
we're gonna use self.url,
116

116

00:05:36,410  -->  00:05:40,620
which is going to be the property stored inside self,
117

117

00:05:40,620  -->  00:05:43,220
inside the object that we're currently dealing with.
118

118

00:05:44,150  -->  00:05:46,660
And similarly here, we're gonna use self.tag_name
119

119

00:05:46,660  -->  00:05:48,063
and self.query.
120

120

00:05:49,040  -->  00:05:53,130
Now, at the end, we are gonna say that self.price
121

121

00:05:53,130  -->  00:05:55,720
is the float, without commas.
122

122

00:05:55,720  -->  00:05:58,170
And we're also going to return self.price
123

123

00:05:58,170  -->  00:06:00,930
just in case that the caller of this method
124

124

00:06:00,930  -->  00:06:05,670
wants to use the price immediately without using self.price.
125

125

00:06:05,670  -->  00:06:07,670
Now, we do get another grey line here,
126

126

00:06:07,670  -->  00:06:09,390
another thing that is not an error,
127

127

00:06:09,390  -->  00:06:12,150
but PyCharm is going to make a recommendation.
128

128

00:06:12,150  -->  00:06:15,790
Now, what it says is that you've an instance attribute price
129

129

00:06:15,790  -->  00:06:18,160
defined outside the init method.
130

130

00:06:18,160  -->  00:06:19,710
So this is very important
131

131

00:06:19,710  -->  00:06:22,960
because it's quite an un-Pythonic way of doing things.
132

132

00:06:22,960  -->  00:06:24,740
What it's telling you is
133

133

00:06:24,740  -->  00:06:27,490
inside the init method, you defined three properties,
134

134

00:06:27,490  -->  00:06:31,380
URL, tag_name, and query, and not price.
135

135

00:06:31,380  -->  00:06:34,680
Price is not defined anywhere in here.
136

136

00:06:34,680  -->  00:06:37,750
But you do define self.price
137

137

00:06:37,750  -->  00:06:40,120
in line 23, inside this other method,
138

138

00:06:40,120  -->  00:06:42,420
which is not the init method.
139

139

00:06:42,420  -->  00:06:44,610
You should not do that generally in Python
140

140

00:06:44,610  -->  00:06:46,360
for a simple reason.
141

141

00:06:46,360  -->  00:06:49,640
While it is completely valid code,
142

142

00:06:49,640  -->  00:06:53,590
what you end up with is the information is dispersed
143

143

00:06:53,590  -->  00:06:55,220
between the two methods.
144

144

00:06:55,220  -->  00:06:59,490
The init method tells a reader, whoever is reading this code
145

145

00:06:59,490  -->  00:07:01,560
that there are three properties.
146

146

00:07:01,560  -->  00:07:03,800
But it turns out there is a fourth property,
147

147

00:07:03,800  -->  00:07:06,633
which is hidden away down here in this other method.
148

148

00:07:07,530  -->  00:07:10,850
Ideally, we would want to define all the object properties
149

149

00:07:10,850  -->  00:07:13,030
in the init method so that when you come back
150

150

00:07:13,030  -->  00:07:15,790
and read this code, you can very quickly see
151

151

00:07:15,790  -->  00:07:19,050
what properties are in this object.
152

152

00:07:19,050  -->  00:07:23,010
So we're gonna say self.price equal none.
153

153

00:07:23,010  -->  00:07:25,210
Notice how I'm not using zero
154

154

00:07:25,210  -->  00:07:28,980
because the price of an item after it's been loaded
155

155

00:07:28,980  -->  00:07:30,290
could be zero.
156

156

00:07:30,290  -->  00:07:31,950
I'm using none because that means
157

157

00:07:31,950  -->  00:07:34,820
we haven't loaded the price yet.
158

158

00:07:34,820  -->  00:07:37,090
Once we load the price, this will stop being none
159

159

00:07:37,090  -->  00:07:38,390
and it will be a number.
160

160

00:07:38,390  -->  00:07:41,060
And that number could be zero, unlikely,
161

161

00:07:41,060  -->  00:07:42,583
or it could be something else.
162

162

00:07:44,090  -->  00:07:48,450
Now that we have this here, we can go back into our app.py
163

163

00:07:48,450  -->  00:07:52,610
and we can say that we're gonna import item
164

164

00:07:52,610  -->  00:07:56,010
from models.item import Item.
165

165

00:07:56,010  -->  00:07:57,430
We're gonna import that
166

166

00:07:57,430  -->  00:08:00,693
and we're gonna pass it in the URL, tag_name, and query.
167

167

00:08:01,600  -->  00:08:04,300
But actually, one second, we don't have to do that just now
168

168

00:08:04,300  -->  00:08:06,970
because they are already defined in here.
169

169

00:08:06,970  -->  00:08:10,260
So at the moment, this is what we'll have.
170

170

00:08:10,260  -->  00:08:11,273
We can print it out.
171

171

00:08:14,476  -->  00:08:16,170
And we're gonna press play.
172

172

00:08:16,170  -->  00:08:19,130
And you can see down here we get 1469.0
173

173

00:08:19,130  -->  00:08:21,240
so exactly the same as we had before.
174

174

00:08:21,240  -->  00:08:22,600
And why shouldn't you?
175

175

00:08:22,600  -->  00:08:24,850
You are essentially running the exact same code.
176

176

00:08:24,850  -->  00:08:27,970
We just copied and pasted it and split it up.
177

177

00:08:27,970  -->  00:08:32,940
Now, one of the good things about objects is that
178

178

00:08:32,940  -->  00:08:35,920
you can create objects with different values.
179

179

00:08:35,920  -->  00:08:38,540
So what we're gonna do is we're going to accept
180

180

00:08:38,540  -->  00:08:42,230
the URL, tag_name, and query
181

181

00:08:42,230  -->  00:08:45,300
in the constructor parameters
182

182

00:08:45,300  -->  00:08:49,040
and then we're gonna say self.url is the URL parameter,
183

183

00:08:49,040  -->  00:08:51,780
self.tag_name is the tag_name parameter,
184

184

00:08:51,780  -->  00:08:54,463
and self.query is the query parameter.
185

185

00:08:55,310  -->  00:08:58,190
Good thing about this, you can now go here
186

186

00:08:58,190  -->  00:09:00,270
and notice, by the way, how you get a small error here
187

187

00:09:00,270  -->  00:09:02,980
saying that the parameters are unfilled.
188

188

00:09:02,980  -->  00:09:05,210
So now, we can come back in here and say
189

189

00:09:05,210  -->  00:09:08,570
that the URL is that, the tag_name is that,
190

190

00:09:08,570  -->  00:09:09,670
and the query is that,
191

191

00:09:09,670  -->  00:09:13,430
and we can pass them into this particular item constructor
192

192

00:09:13,430  -->  00:09:14,490
as we have before.
193

193

00:09:14,490  -->  00:09:17,820
I jumped ahead of myself there earlier on the video.
194

194

00:09:17,820  -->  00:09:20,370
But there we go, now we have exactly the same thing.
195

195

00:09:20,370  -->  00:09:21,590
Same thing is running.
196

196

00:09:21,590  -->  00:09:26,410
Only now, we are passing the item URL, tag_name, and query
197

197

00:09:26,410  -->  00:09:28,670
to this particular constructor,
198

198

00:09:28,670  -->  00:09:31,320
which means this item variable
199

199

00:09:32,360  -->  00:09:34,910
is related to this URL.
200

200

00:09:34,910  -->  00:09:38,960
But you could create another item,
201

201

00:09:38,960  -->  00:09:39,880
which has
202

202

00:09:40,860  -->  00:09:45,020
a different URL, but the same tag_name and query
203

203

00:09:45,020  -->  00:09:46,840
if they are from the same website.
204

204

00:09:46,840  -->  00:09:48,610
Of course, if they are from a different website,
205

205

00:09:48,610  -->  00:09:49,740
you're gonna need to find
206

206

00:09:49,740  -->  00:09:51,643
the tag_name and the query separately.
207

207

00:09:52,770  -->  00:09:55,240
Right now, I don't have a second URL
208

208

00:09:55,240  -->  00:09:58,470
with an item that is a valid URL, but you could do this
209

209

00:09:58,470  -->  00:10:01,073
and then this would load the second item.
210

210

00:10:02,000  -->  00:10:04,620
The benefit of that, of course, is that now, your item class
211

211

00:10:04,620  -->  00:10:06,220
is a bit more generic.
212

212

00:10:06,220  -->  00:10:10,480
What you have is the ability to load different items
213

213

00:10:10,480  -->  00:10:12,860
using the same code, whereas before,
214

214

00:10:12,860  -->  00:10:14,610
we didn't really have that ability.
215

215

00:10:16,480  -->  00:10:18,840
All right, so I hope this is helpful.
216

216

00:10:18,840  -->  00:10:22,100
We've now created our item model, which is going to be
217

217

00:10:22,100  -->  00:10:25,100
one of the building blocks of our application.
218

218

00:10:25,100  -->  00:10:28,160
And moving forwards, we're going to be saving these items,
219

219

00:10:28,160  -->  00:10:29,830
we're gonna be able to use them
220

220

00:10:29,830  -->  00:10:32,290
and extract them from a database and so forth,
221

221

00:10:32,290  -->  00:10:34,733
so that we can start building up our app.
222

222

00:10:36,000  -->  00:10:37,550
I'll see you on the next video.
