#100DaysOfCode in Python Transcripts
Chapter: Days 43-45: Consuming HTTP services
Lecture: Demo: Data version two: Better results
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's make two quick improvements before we wrap up
0:03
this application. First, we're always searching for runner.
0:05
How exciting is it to always run this program
0:08
and just get the same results?
0:09
Let's actually make this a thing that the users can input.
0:13
So we'll begin by creating a variable.
0:14
We could just go and type a variable name, assign the value
0:17
and put it over here, and use PyCharm to refactor.
0:20
Say I want a variable called keyword.
0:22
That's pretty cool.
0:23
Make sure it still runs, it does.
0:25
But let's actually go and say input.
0:27
We'll ask the user for input, and we'll give them a prompt.
0:31
We'll say keyword of title search for movie,
0:34
or something like this.
0:36
So now when we run it down here I can search for away
0:39
and we get The Runaways.
0:41
I can search for runner and we should get Bladerunner,
0:46
and we can search for fight, Fight Club, whatever.
0:50
Whatever we want to search for, we can now do that.
0:52
This is already really awesome, but notice the API is,
0:56
I'm going to call it crummy.
0:57
So we're going to fix that with one more thing.
0:59
We're going to use this thing called collections.namedtuple
1:03
So down here I'll say I'm going to define this type that
1:05
represents a movie.
1:07
It's going to be a collection.namedtuple, and the way it works
1:10
is you say the name that it thinks its own name is,
1:13
and then you say all the fields that go in there.
1:16
Now this turns out to be quite a pain, so let's go back
1:19
over here and see what we get.
1:21
IMDB score, we have a rating, an we have all these things.
1:24
So this is the typical value here, it's going to look like this
1:29
So in order for this to work well, we have to have all these
1:32
values listed in series over here.
1:35
So, I'm just going to type this in, then I'll speed it up.
1:43
Phew, there I've typed them all in and I'll maybe do a few
1:45
line breaks here just so it fits on the screen.
1:48
I just typed in the top level elements here.
1:50
So what we can do, is we can actually instead of just
1:54
churning JSON results, we can actually go over them
1:57
and return these types, and we'll see why that's a benefit
2:00
in just a second.
2:01
So we'll say this, we'll say for our end results,
2:06
you know, the hits, maybe H, I don't know.
2:09
We're going to need a list, going to say movies, that's a list.
2:12
I'm going to put this in here, so we'll say movies.append
2:15
and we need to append one of these movies.
2:18
So we can create a new one and we have to type all of these
2:21
values in here.
2:22
We have to say IMDB code equals R.get IMDB code.
2:26
Title equals R.get title, and so on.
2:31
Or, there's a way to unpack a dictionary, which is what this
2:34
is, back into keyword arguments with the names being the
2:38
keys just like that.
2:40
And, the way you do that is you
2:42
say **, the dictionary name.
2:44
So it's as if you said IMDB code equals whatever value it
2:47
has for IMDB code.
2:49
Title equals whatever value it has for title.
2:52
It's just a super short, simple way to do that.
2:54
So now if we return movies, up here is not going to work the
2:58
same, but now we can just say R.title, and things like that.
3:05
HasScore, take that little bit away.
3:09
HasScore, let's say,r., what did we call it?
3:13
IMDB Score, that, now let's try that.
3:19
Now we are going to search for runner, stick with that.
3:22
Boom! Look how cool that is.
3:24
Now, we're still not where we quite really wanted to be.
3:26
If I hit dot, we get not, so as in not helpful!
3:31
One final trick of what we're going to do, let's go over here
3:34
and we can use what's called type hint in Python.
3:38
Python is a dynamic language, it has no syntactical typing,
3:42
but you can give the editors hints, and I can say this
3:44
is actually a string, so you say colon type name and the
3:48
return value is a list which is actually defined in a
3:52
typings module, so got to import that.
3:55
So we want typings.List.
3:56
Notice the, let's put at the top here.
3:59
And it's going to be a list of movies like this, okay.
4:03
So with a little bit of a hint, I can come over here and
4:07
now I type r., look at that, director, duration,
4:10
IMDB code, IMDB score.
4:13
Let's just add one more.
4:15
With code r.code, now that's the way we like to write.
4:22
All the help we can get.
4:24
Let's search one more.
4:26
Anything on computer, of course!
4:28
Hackers with code TT whatever HasScore 6.2.
4:32
Awesome, there you have it.
4:34
This is how we can consume the movie API.
4:37
When you've broken apart into our, our sort of user
4:41
interaction bit of code here, our API access code here.
4:46
We use requests to actually get the data and convert it
4:51
to JSON and we used a namedtuple to help package it up into
4:55
something that makes more sense to our program, as well
4:58
as adding a little type hints, so that the editor actually
5:01
leverages that help we gave it.