Coding 101 15 (Transcript)
Shannon Morse: Today on Coding 101 we are sorting with the sorting
hat but will you end up in the slithering Python house? We’ll see.
Netcasts you love. From People You Trust. This is Twit!
Bandwidth for Coding 101 is provided Cachefly. At C-A-C-H-E-F-L-Y dot com.
Shannon: This episode of Coding 101 is brought to you by
Lynda.com. learn what you want, when you want with
access to over 2400 high quality online courses and training videos. All for one low monthly price. To try it free for seven days
visit Lynda.com/c101. That's L-Y-N-D-A dot com slash C one zero one.
[Intro Plays]
Fr. Robert Ballecer:
Welcome to Coding 101, it’s the Twit show where we let you in to the secrets of
the code monkey. I'm father Robert Ballecer.
Shannon: Ooh secrets. We’re like magicians, and I'm Shannon
Morse. And today we are going to get you all coded up in everything you need to
know about Python to become a code warrior.
Fr. Robert: Right, right now we do want to talk about this that
you got to do in programming called a sort.
Shannon: A thing?
Fr. Robert: It’s a thing.
Shannon: Ooh a sort.
Fr. Robert: It’s a sort but you know what before we get to sort,
as always it’s time for some Snubs compiled.
Shannon: I'm scared of the sort. Should I be scared?
Fr. Robert: Yes, be very, very afraid.
Shannon: I actually think this might help me because last week
I wanted to sort this stuff and I had no clue how to do it.
Fr. Robert: And now you will.
Shannon: So let me show you my code on my computer. So I did
this thing in IDLE where I just created a simple code that printed out people
that are going to my wedding and whether they ever played yes
or no. So it just lists them out Shannon Morse has replied yes, duh. My
fiancé has replied yes, I sure hope so. And then it goes down you know Officiant Tom who’s totally a real person, has replied yes.
And then if they have not replied, it’ll put in all caps like, daddy Morse has
not replied. He better reply because he’s walking me down the aisle. And then
press enter to exit so very simple, very easy to use. What I would love to do
is get this to sort in alphabetical order or by yeses
and by nos and then not replies down at the bottom.
It must be so much easier. So right now this is what I have.
Fr. Robert: Now let’s just you know, let’s just walk through that
code though because your code’s beautiful.
Shannon: Thank you.
Fr. Robert: Of course the first line is—
Shannon: Dale help me.
Fr. Robert: I mean that's because Dale’s our code warrior. But you
got the print of who has RSVP’d to my wedding as a yes or no. then you've got the function that we showed people of bringing
something in. You used text file to open up a file—
Shannon: Exactly, yup there's my text file.
Fr. Robert: …and then you weed into that list or that array. Now
you've got a forloop in there along with some if and
else statements that basically looks for yes no or not
replied.
Shannon: Exactly that's exactly what I did. It looks specifically
for the yes and it will split the guest, the name of the guest with this split
function right here and then the little dash. Now if I show you my text for the
actual code, it simply has Shannon Morse dash yes and then a comma to separate
each of the guests. So very simple, very easy to use.
Fr. Robert: Nice.
Shannon: And can be very, very helpful if you're planning a
wedding apparently.
Fr. Robert: Well as we said, these are all very basic programs but
the idea of bringing an external data source into your code is incredibly
important.
Shannon: I totally agree.
Fr. Robert: It’s one of the fundamental things you have to do if
you're going to programming in the modern world.
Shannon: It really is, now of course as always we do have a
couple of our viewership codes that I wanted to show as well so for this week I
had a couple of really good ones that I wanted to check out. So there two that
I wanted to show you examples of. The first one is called equations. So this is
from—
Fr. Robert: Ooh I like how this looks.
Shannon: And let me look up his name real quick. This is from
Darryl. So Darryl basically made an equation right here so it takes all these
different—
Fr. Robert: It was nice of him to spell it out.
Shannon: So let me show the text so this make a little bit more sense. Okay so he has these different equations which are
written out with a plus in the beginning. So it’s 12.5 plus 24.3 and then it
gives you the equation in the actual output over here and it goes down and it
show you each of them. He also included a couple of errors so you can see what
it would print out if it gave you an error.
Fr. Robert: Nice, nice.
Shannon: And I like a subtraction, multiplication, plus,
division and then down here we have a percentage. So 5 and then what is that, 5
divided by 3 equals 2.
Fr. Robert: 5, yeah, yeah well I like this because it’s a basic
list. You've got your operator, you've got your
numbers on which it’s going to perform the operation. It shows you exactly
what's it’s pulling in and then it’s going to show you the output.
Shannon: I’ll show you what it looks like in IDLE. Oops, wrong
button. There we go. Okay so lots of notes, again thank you so much for
including those Darryl and then if we scroll down you'll see that he has a forloop in here. he has if
statements, elif which we learned about last week and
else. And then he has this cool one down here that says try these except this
else and then another if. So weird.
Fr. Robert: Right, right and that’s his way of sanitizing it.
Shannon: Yes it is.
Fr. Robert: So he told you got and a value you would not expect.
Shannon: Very cool, so thank you so much for sharing that with
us Darryl. And the third one that I wanted to check out is from Gutmer. So he did a simple one that show you a bunch of different prime numbers. And if I look at his example and I
bring up in IDLE and I show you the text document that he decided to bring over
for this. Okay so he has a whole bunch of numbers—
Fr. Robert: Nice.
Shannon: …written out so very simple. And then if you look at
his code, he’s basically taking these numbers and determining if they are a
prime number and if they are not.
Fr. Robert: Oh this is perfect because he’s got that function that
actually does the test. We know that a prime number is only divisible by itself
and by one.
Shannon: Yes, yes.
Fr. Robert: So basically what his prime function does is it tests.
It tests a couple of different parameters so I think the first thing that I
would do is I would tests if it’s divisible by two. If it’s divisible by two
and it’s not two it means it’s not a prime number.
And then there's a couple of different tests that you
can run mathematically to decide whether or not a number is a prime. He hard
coded that in to his program.
Shannon: Pretty cool huh? Then if we go back over here, so it
makes a lot more sense now that you seem to code.
Fr. Robert: Right.
Shannon: So 8191 is a prime number, prime, prime, prime, prime, prime. Let’s see the sum of all numbers is our
file is, and then he gives you the sum of all of them. The largest number and
the smallest number as well. Pretty cool.
Fr. Robert: Fantastic.
Shannon: Yeah.
Fr. Robert: Mod, mod, mod.
Shannon: So thank you both for sharing and of course if you guy
have any code that you want share with is I can always do that our Google plus
community. And I believe we just got the actual Google Plus name for our
community too.
Fr. Robert: And we’re almost at a thousand.
Shannon: We are almost at a thousand. We are so close.
Fr. Robert: I would say definitely by the end of this module, we’d
be at a thousand, yeah.
Shannon: I'm so excited too, I can't wait.
Fr. Robert: And by the way, so folks we have been getting some
emails from people who are like “Well, where do we send you the code? Where can we upload the code if we--?” the one place that it
makes sense to put it is into the G Plus community. I'm sorry if you don't use
G Plus but it’s too difficult to juggle all the sources of code we may be
getting. If you put it in the G Plus community, not only will we see it but the
other members of the Coding 101 community can see it and they can vote up or
down whether or not they want to see it on the show. That’s just you know one
of the things we do because we love you.
Shannon: Oh yeah and we get so many good examples so thank you
to everybody who have been sharing those.
Fr. Robert: Absolutely so Shannon, I like this but you know what
else I like?
Shannon: What's that?
Fr. Robert: I like knowledge.
Shannon: Oh I like knowledge too.
Fr. Robert: Knowledge is good right.
Shannon: You know where I've been getting a lot of my knowledge
lately?
Fr. Robert: Where?
Shannon: There’s a website online where you can watch video
tutorials about everything.
Fr. Robert: I have heard of this mythical website.
Shannon: Yeah it’s true. It’s called Lynda.com and they have
thousands and thousands of video courses made by professionals that will teach
you everything from Java to Motion Design to Graphics to Photography. Gosh what
else, Python? I mean if you think of it, they probably have a video on it. They
basically have 2400 high quality online video courses from all these different
types of genres. They offer things in software and creative and business
skills. Whether you want to learn Python code or explore the foundations of
programming, you can improve your photography with Lynda.com. With a
subscription their members receive unlimited access to thousands of high
quality and engaging video tutorials across a wide variety of subjects, there's
everything. So do you want to build your first IOS app but you're not a
programmer quite yet. Of course you watch this show but Lynda.com has a new
course called programming for non- programmers, IOS 7 which will allow you to
build your first IOS app in a single afternoon. You can learn the most
important concepts in IOS and the app development process. By the end of the
course you'll have a finished app and a basic understanding of x code. That's
the tool set for developing IOS apps. You'll know the building blocks like
variables and functions conditional statements and interface design.
Fr. Robert: We've taught all those things.
Shannon: Yes we have.
Fr. Robert: I love it.
Shannon: I love it. I love it love it. And you can find links
to this course and many more over at Lynda.com/c101. So personally I've been
using Lynda.com for years.
Fr. Robert: Yeah.
Shannon: Absolutely for years, everything from just simply how
to create my own Adobe PDFs that people can use to fill out their own forms
because it’s you know a little complicated when you just open up the software.
And there were video tutorials on how to do that on Lynda. And recently I've
been using it for coding experience for Coding 101. Everything
from Python to this really cool concept about IOS 7 and doing that for
non-programmers. I love the fact that they have those 2400 courses on
there and there so much better than you know just videos made at home on Youtube. Yes those have their place but these are so much
better. They're taught by professionals who are passionate teaching. They
really want to get you excited about what you're learning. And you can do it
whether you're a beginner or you're intermediate or you're advanced. So no
matter where you are at in your tutorials you'll be fine. And you can watch
from your computer, your tablet or your mobile device especially if you're on
the go all the time like I am. I commute a lot. Whether you have fifteen
minutes or fifteen hours, gosh I wish I have fifteen hours to watch all day.
Fr. Robert: I’d love some fifteen hours.
Shannon: Each course is structured so you can learn from start
to finish. You can also search the transcripts to find quick answers and real
along with the video. Lynda.com offers certificates of completion when you
finish a course, which you can publish to your LinkedIn profile which is great
if you're a professional in the field. And it’s only 25 bucks a month for
access to the entire Lynda.com course library. For 37.50 a month you can
subscribe to the premium plan which includes exercise file that let you follow
along with the instructors project using the exact
same projects assets that they do. And you can try Lynda.com right now for free
with the seven day trial. Visit Lynda.com/c101 to access the entire library.
That's over 2400 courses for free for seven days. That's L-Y-N-D-A dot com
slash see one zero one. And of course we thank Lynda.com for their support. We
love you guys.
Fr. Robert: Well we love anyone who brings the knowledge and
Lynda.com drops the knowledge on you like a bomb.
Shannon: Yes, that's true. Like a bomb.
Fr. Robert: Like a bomb. Actually I don't think they want to get
associated with bombs.
Shannon: Bomb, like a boss.
Fr. Robert: Like a sort.
Shannon: Like a boss.
Fr. Robert: Like a boss sort.
Shannon: Like a boss sort.
Fr. Robert: Okay, all right now let’s talk about sorts.
Shannon: So speaking of sorts.
Fr. Robert: Stay away from bombs and bosses and let’s talk about
sorts. Now we’d like ot give
you some of the fundamental pieces of programming that you're going to need
right?
Shannon: Right.
Fr. Robert: Like for example, variables of course and conditional
operators.
Shannon: Yes.
Fr. Robert: Of course, and now we've taught you how to bring data
into your program right?
Shannon: Loops, right yeah.
Fr. Robert: Right, here’s the thing though, one of the advantage
of doing things in a computer, doing things in a programing language is that
computers are much better at reorganizing data than we are.
Shannon: Yeah, absolutely it takes me forever to sort anything
whether it’s alphabetical or from smallest to largest.
Fr. Robert: Right and even if you're crazy savant good at sorting,
a computer can just pile through the millions, billions, trillions of
calculations you need to sort a list, a data set, an external file of any size.
Shannon: Exactly.
Fr. Robert: Something that the human brain – you know we’re not
great at doing right?
Shannon: Right.
Fr. Robert: So let’s teach you how to do a simple sort. Now when
we talk about sorting, what we’re talking about is letting loose your computer.
Using the power of a programming language to take a predetermined set of
information and then running some sort of operation on it that will perform
some form of sort. Now what do I mean, well how about this,
let’s imagine I have a list of names.
Shannon: Okay.
Fr. Robert: Okay so I've got a list of names, let’s say it’s just
26 names. Like you do for your registry right?
Shannon: Yeah.
Fr. Robert: Now let’s say I would really like for this to be in
alphabetical order according to first name.
Shannon: Yes.
Fr. Robert: So now I take that list which has the first names on
it right? And to somehow reorganize that list in order to make it in
alphabetical order from a to z.
Shannon: Yeah so how do you do it?
Fr. Robert: Well if we’re in the real world, so just hear—
Shannon: So if I was doing it in the real world I would look
through the list and look for a, b and cs and stick
them over here and then you know t through and then stick them over there and
then everything else in the middle.
Fr. Robert: Right and then you just kind of – you randomly sort of
put them where they're – oh this—
Shannon: Yeah just kind of fit them in and scoot them over.
Fr. Robert: …this is Agnes but this is Adam so—
Shannon: It’s trying to sort through all of your CDs and DVDs
back in like the early 2000s late 90s.
Fr. Robert: Yup.
Shannon: And putting all that stuff in order.
Fr. Robert: Exactly, exactly well here’s the thing, doing it in
programming is almost the same thing that you would do in the real world.
Remember how we always talk about breaking down the real world into
understandable chunks.
Shannon: Yeah.
Fr. Robert: You do it the exact same way. The logic is the same,
which is you just compare the values of each name that
are relevant. So the first letter, the second letter, the third letter,
whatever it may be.
Shannon: Okay yeah.
Fr. Robert: And you decide, does it go
before or after this entry.
Shannon: That makes sense.
Fr. Robert: Because ultimately, when we do a sort, we’re always
comparing two values.
Shannon: Right.
Fr. Robert: Does this go before or after. Does this go before or after. Okay so break down, follow me on this logic, let’s say I have that list of names.
Shannon: Okay.
Fr. Robert: The first thing I’d want to do is I would want to take
something from that list and put it into like my sorted pile.
Shannon: Okay.
Fr. Robert: And then I’d want to take another entry and compare it
to the thing in the sorted pile and say should this go before or after.
Shannon: So let’s take the first entry which could be Shannon
and compare it to the second entry called Padre.
Fr. Robert: Right exactly and so we go “Oh, well does this go before or after according to the value that I want to sort
it by?”
Shannon: Yes okay.
Fr. Robert: Now let’s say those two names are now in there. Let’s
take a third name like Adam and it’s going to compare against both of the
entries in that list to determine where it’s going to be.
Shannon: Shannon and Padre. Right.
Fr. Robert: Right so pretty simple calculation.
Shannon: Yeah.
Fr. Robert: Now but a computer can do it so much faster.
Shannon: Yay.
Fr. Robert: All right now let me show you how it actually works
inside of a computer program. I you could show my project shot here. I decided
to do this instead of the blackboard because people get upset with the
blackboard. This is – let’s just say this is what my external list looks like.
Shannon: Okay.
Fr. Robert: I just brought in a bunch of numbers, 1, 23, 6, 4, 99. Obvious not it numerical order.
Shannon: Right.
Fr. Robert: Now what I want to do is I want to use computer logic
to sort this. So like I said, the first thing I'm going to do is take the first
entry and I'm going to bring in over to the clean side so this is my sorted
side right.
Shannon: Okay, yeah.
Fr. Robert: Now I'm going to say “I'm done with this, now I'm
going to go over to the second entry” and I'm going to bring it over and I'm
going to compare it to one. I'm going to say “Is it greater or less than one?”
Shannon: And it’s great.
Fr. Robert: And it’s greater than so I’ll drop it below.
Shannon: Okay.
Fr. Robert: Then I'm going to go down to the next entry and I'm
going to bring it over. I'm going to compare it six to one.
Shannon: Ah okay.
Fr. Robert: Is six greater than one, yes it is. All right so it
moves down. Now it’s going to compare it to twenty-three. Is six greater than
twenty-three?
Shannon: No it’s not.
Fr. Robert: No it’s not. Without what it’ll do is it’ll say “I'm
going to copy this in to the position where twenty-three was and I'm going to
move twenty-three down to a lower position.”
Shannon: Oh okay. So then it does the same for four.
Fr. Robert: It’s going to do the same thing so it’s this, nope,
this nope, boom. This is going to come in, it’s going to compare against all
the values and then it’s going to end up at the bottom.
Shannon: Okay.
Fr. Robert: That, using poker chips, is what a sort does.
Shannon: Awesome.
Fr. Robert: That's the basic methodology of happens I your computer when you run a sorting algorithm.
Shannon: And now I'm going to take these home and go gamble.
Fr. Robert: Well no, you're going to take it for coupons, coupons.
Shannon: Thank you.
Fr. Robert: So then it’s really easy to understand right?
Shannon: Yeah that's totally easy to understand.
Fr. Robert: Now here’s the—
Shannon: But how do you put into your code?
Fr. Robert: How do you put it? And that is good question. Here’s
the fun thing, if I was working in a really basic language, I would actually
have to program all the logic for that sort.
Shannon: Right.
Fr. Robert: That's not that hard but it’s you know why do it? In Python there's actually a sorting method.
Shannon: Ooh.
Fr. Robert: This is sorting function. If you could go to my
screen, what you see here is what's called, the sort. So I've got a list, I've
just created a list called sort me and it’s filled with those same numbers so
one—
Shannon: You need to sort me.
Fr. Robert: …twenty-three – yeah the sorting hat. One, twenty-three, six, four and ninety-nine. And then I
just use the forloop, for entries and range from zero
to five, print sort me. So all it should do is reprint that.
Shannon: Okay.
Fr. Robert: One, twenty-three, six, four, ninety-nine.
And then I put a pause then I did this. Sort me, which is a list remember
that's the list where all the numbers are.
Shannon: Yes.
Fr. Robert: And I added that, that's all I have to do.
Shannon: So that, let’s see, period sort the parenthesis—
Fr. Robert: That calls the method.
Shannon: …the parenthesis and the semicolon. That is the
method.
Fr. Robert: Yeah it calls the method, the function called sort.
Shannon: Okay.
Fr. Robert: And so what I'm doing in Python is I'm saying “Run the
method called sort on the list called sort me.”
Shannon: Oh.
Fr. Robert: Okay and then I do a print again. So if I run this
program what I get is something that looks like this. So 1, 23, 6, 4, 99 that's
what the list was.
Shannon: Yeah.
Fr. Robert: Now remember, as soon as I hit this enter key it’s
going to run the sort on the list.
Shannon: It’s going to run it again.
Fr. Robert: And now it puts it in order.
Shannon: That's cool.
Fr. Robert: 1, 4, 6, 23, 99.
Shannon: Oh neat, okay.
Fr. Robert: The cool thing about this – actually is that actually
in the frame, yeah. The cool thing about this is this is all built into Python.
I did have to program anything. I you go back to my code here it’s going to
show you that that's an integrated function. That is a built- in function with
Python. And actually you're going to see that in most advanced languages. They
know that sort is such a basic lesson. It’s such a basic function that they're
going to include it in one way or another.
Shannon: Ah okay I see what you did so you have the name of the
thing with all of your different, all of your different variables inside of the
two brackets.
Fr. Robert: Right.
Shannon: And then you included the – let’s see, the range which
is one is zero and then all the way up to five.
Fr. Robert: Right.
Shannon: Five would be would be 99.
Fr. Robert: Right.
Shannon: And it just sorts those or it just prints out sort me.
That range.
Fr. Robert: There you go.
Shannon: Raw input press enter and
then it sorts it into an actual zero to five or 1 to 99.
Fr. Robert: Now again, this is a basic function to program so I
hard coded the list but there no reason why I couldn't, you say that external
text file right?
Shannon: Yeah.
Fr. Robert: So there were some people who were just throwing a
bunch of numbers at me, well yeah make a text file filled with numbers
separated by commas, import them into your program, open up that text file, get
them into a list and then you could sort it.
Shannon: That's awesome.
Fr. Robert: I’ll put it back to file.
Shannon: This is perfect for me. This is going to save me so
much time.
Fr. Robert: Right, right and remember—
Shannon: Oh I can't wait to use it.
Fr. Robert: Here’s the cool thing, when it’s small like this we’re
almost saying “Well you just did it on the table.”
Shannon: But when you have a 150 guests going to your wedding
you kind of got to put them in to some kind of list that makes sense.
Fr. Robert: Precisely. And that's why we think sorting is not just
useful to know but it’s absolutely vital.
Shannon: Oh why it totally is. I'm super excited to use this.
Fr. Robert: Yes, actually you know what? Don't we have someone we
could talk to who could maybe show us how to use this in a more advanced way
then just sorting a couple of numbers?
Shannon: I have a feeling we have somebody on the line named
Dale.
Fr. Robert: Dale come in. Our code warrior, ladies and gentleman Mr. Dale Chase from
Discovery Digital Networks. He is a programmer, he is an artiste, he is our code warrior.
Shannon: Hello sir.
Dale Chase: What's going on, I am in the house, how are
you guys doing?
Shannon: Yay he’s in the house.
Fr. Robert: We’re sorted.
Shannon: We’re doing great thank you.
Dale: Word, word. So yeah I am – we’re actually going to get
into some Youtube API stuff and sorting some results
and printing out video titles and links.
Shannon: Oh boy that sounds scary.
Fr. Robert: Now for the folks at home who are getting freaked out,
it is a little bit freaky but don't worry because what Dale is actually going
to be doing is he’s going to show us how to use an API. It’s a way to link into
services like Youtube, in fact there's a lot of
services on the internet that use an API. You start with and API key that
allows you to access their service to both pull and push data to them. What
he’s going to show you is to use that within Python so you can start pulling
not just from an external file that you've created but from Youtube itself.
Dale: Yeah, yeah I'm using one of their calls it’s just that
their basic query search call that doesn't really require any authentication
but you do need an API key. You know you're essentially registered with them to
use the service.
Fr. Robert: Now we’re not going to show them your API key because
that would be bad.
Dale: No, no, no actually so I've got my API key hidden in
an external text file which I am pulling in and using as a variable here API
key.
Shannon: Oh cool.
Dale: Yeah so let’s just take it from the top. So I'm
importing a few modules here to do the HTTP request to Youtube.
I am using URL LIB which is a module that really I'm just grabbing for one
function so I can URL encode the user input because it’s a get request which
means it’s going to into the address bar. And so those things that go into the
address bar – like non word characters like exclamation marks, comma that sort
of thing need to be converted into character that will play well in the address
bar. I believe it’s called what, Unicode encoding? Unicode,
padre?
Fr. Robert: Yeah, yeah, yeah.
Dale: So I'm using URL LIB 2 to actually make the http
request. It gives me access to a function called, where is it, URL open. And
then I'm importing JSON, which is sort of like another version of XML almost.
It’s just a way to kind of transport information, you know large amounts of
data, different kinds of data from one place to another in a nice collection of
– nice object that you can then access things out of later. So the responses
that we get from Youtube will be in JSON. So I need
this module to decode it and then work with when I get back.
Shannon: Oh okay.
Dale: So the next line here is me grabbing my API key from a
text file, assigning it to this variable then using the same variable after I
read the text file. I'm just using API key again just you know because I'm not
very imaginative when it comes to API key variables.
Shannon: Just keep things simple.
Dale: Yeah. Then I'm getting user input to search for the
video and assigning that to a variable called search term.
Shannon: And they could type in anything that they want there
as far as the video is concerned?
Dale: Anything.
Shannon: Okay.
Dale: Yup totally, because the next line I'm going to use
URL Lib’s quote plus function to URL encode the input so that it’s safe to use
in the next line.
Shannon: I'm a little confused by that.
Dale: Okay say let’s say when you put in your search you're
using non-letter characters, like you're using exclamation marks or ampersands
or anything like that. If you put in ampersand in the address bar, it’ll think
that it’s reached the end of you know an argument that you need.
Shannon: Oh.
Dale: You know how you see ampersands, question marks in the
address?
Shannon: Yes.
Dale: Those are get variables that you're then submitting to
the website so it can get the information it wants to do that it needs to do what
it wants to do.
Fr. Robert: You see that all the time on Youtube.
Shannon: Yes, I do all the time.
Fr. Robert: You can remove everything after the ampersand normally
because all it’s doing is adding extra arguments to
whatever search you did.
Shannon: Yeah I've done that before and I was kind of surprised
that it just gave me the same website.
Fr. Robert: Just works yeah, go figure, yeah.
Dale: Yeah and I actually get into a little bit to it. So
you've got your URL encoded input now. So now I'm creating essentially the URL
that's going to be the request. So I've got the Google API’s domain, Youtube V3, which is the version 3 of the Youtube API. An then so heres their call, which is search. And a requirement for
their call is this thing called parch, which I am setting to snippet which
means I'm going to get back with their calling, their main sort of metadata
chunk snippet. So I need to set my part to snippet so I can get that. That's
the information that I want back.
Fr. Robert: And I should note that – Bryan if you go ahead, go to
my screen, when he’s pulling this code, it’s actually, it’s based off of
commands that you – Oh I should probably close your key.
[laughter]
Fr. Robert: We’re pulling from commands that are listed in the Youtube API. I you search for the Youtube API version 3, they will give you all the commands that you can pull in that
API.
Dale: Yes, yes actually I happen to have it open here or
did. Yeah this is it right here actually. Here is their thing, here’s the
reference and here is a list of their categories of calls and I'm actually
using the search list call.
Shannon: Oh wow.
Fr. Robert: And don't freak out about this because we've actually
been doing calls. Every time you call a function or a method in Python or C#,
you're actually doing a call. All that's different about this is we’re calling
to a remote server. We’re calling to a different system, it’s actually not running locally.
Shannon: That's cool. I like it.
Dale: All right, so the next line – actually let’s go a little
further down. So here’s that ampersand I was telling you about. So this
separates – oh sorry, first, so I've got q equals here, which is q is the
required argument here. I guess q is for query.
Fr. Robert: Right.
Dale: So the query equals search term. So that's what you
entered so it’s not going to use what you entered for the search and then I'm
just tacking on here on my API KEY so they know it’s me and I'm allowed to make
this call.
Shannon: Ah okay, I see.
Fr. Robert: Yup.
Shannon: I see what you did there.
Dale: So the next line is the response. So this call – oh
this is the URL Lib 2, URL open function that actually makes the call to Youtube and then I'm getting their response and putting it
in a variable called response. I then take that response, which is a JSON
object and I use this JSON function to essentially decode it so that I can now
work with it.
Fr. Robert: Right. You know we actually got a good question in
chat from M5 who wants to know “Well how do we know the API calls are in Python
or is that not a concern?”. It is concern because -
and this is the beauty of functions, and this is the beauty of calls and APIs.
All that happens when I make a call to an API is I have, just like in Python,
the name of the function and then the parameter I want to pass it. Those go
over to the API and it returns me whatever value it’s supposed to return to me.
So it really doesn't matter what language I'm programming in, API is working
like a call should work, I just send it data and it gives me data back that it
processed on its side. I don't have to care what kind of coding they running on the remote side.
Dale: That's right, as long as you can do an HTTP request,
that's all that matters.
Fr. Robert: It’s all good, it’s all good.
Dale: Yeah. And so I'm declaring my list here. I'm calling
it Video Metadata.
Fr. Robert: Um-hmm.
Dale: And so now I'm going to do a little bit of a forloop where I cycle through my response. Let me –
actually I should probably just show you what this looks like really quick, the
actual response from Youtube.
Shannon: Do it.
Dale: Yeah, so let’s actually just use this real quick. I'm
going to go down – they have actually a way to play around with their APIs
right there on the site and since this is a call that doesn’t need any
authentication, I'm not going to go through the [?] process. I'm just going to simply
pop in snippet and then scroll all the way down to query. And I'm going to type
in Peter Capaldi. And just scroll all the way down to
execute.
Fr. Robert: By the way, that's an awesome – we’re going to make
sure to put the link for this page into our show notes because this does give
you all the different arguments. All the different calls that
you can make to the API.
Shannon: Yeah.
Dale: And so here’s my response.
Fr. Robert: There we go, so once you've made the call, this right
here that we’re seeing on the screen, this is the data that actually comes back
from Youtube.
Dale: Yeah this is a JSON response and so as you can see
I've got – there's some information here, some stuff that doesn't really make
much sense to most folks but then the total results, there are really a 142,000
videos about Peter Capaldi.
Shannon: So these are the results based on your search?
Fr. Robert: Right, right.
Dale: That can't be right.
Fr. Robert: No that's about right. Doctor Who,
you got crazy popular.
Dale: So what you'll see in my code is I make reference to
items here and then – so within items is an array of items which are – which is
the metadata for the various videos. So I go into items and then I will pick
out my snippet. I’ll check out what the kind is, the
ID and then the video ID for the Youtube video. So
this is what you see in your URL when you actually – or out of the Youtubes videos.
Fr. Robert: Right.
Dale: So I will use this information now to loop through
give you the response that I want to give you.
Fr. Robert: Folks so right now, I know there are some people who
are freaking out, they're thinking this is way, way too complicated. Let me
bring you down just a little bit. This s really – when you
get down to it. No more complicated than the functions that we created
to pull data from an external file and do the sort. So the thing that we just
did with the sort, that's essentially all he’s doing. The only difference is
the amount of data he’s pulling and where he’s pulling it from.
Shannon: Um-hmm.
Fr. Robert: All we need to do, once we get that data back from the Youtube API, is search for the particular pieces of
string, the little particular pieces of information that we need to be able to
sort, that we need to be able to then do something useful with Youtube. So I know you're freaking out, but seriously look
at the code, relax and just realize it’s actually not that bad.
Shannon: It’s not?
Fr. Robert: It’s really not, I'm serious, I'm serious. I’ll guide you through it. All right, okay continue, sorry.
Dale: Yeah, we’re almost there. So the first thing I'm doing
is – so I'm going for videos items, so for video in video items. So what I was telling
you before, I'm looking inside of item in the response because that’s what I'm
concerned with. That's where the video data is. And that's the array of data
there. So for each item in there is going to be a video, and so now I'm going if video ID kind is Youtube video.
Shannon: Oh.
Dale: And I'm making this distinction because actually for
me since I was logged in as my – you know I was authenticated and logged in as
myself, if I did a search for myself one of the responses would actually be my
channel information which would break the code.
Fr. Robert: Right.
Shannon: Oh okay.
Dale: That's a little further down so I wanted to make the
distinction to make sure that the item that I'm looking at is a video. So
that's what this line is for. And so then I go video metadata.append,
video snippet title. So now I'm getting the title of the video and putting it
into my list.
Fr. Robert: There you go, and see we've done this, we've done
this. So I know this is really complicated but so back up and all he’s doing
with that if statement is he’s making sure, again this is – remember what we
talked about sanitizing, he’s sanitizing the incoming data from Youtube and saying “I only want things that are actually
videos.”
Shannon: Youtube videos.
Fr. Robert: So he’s looking at the ID in the kind, which is
information that was returned and saying “If it matches Youtube videos, then I know that that information has to deal with a Youtube video.”
Shannon: And then he wants to stick it in his list and type out
the title.
Fr. Robert: Stick it in the list. And we've done that right. We
know how to put information into lists.
Shannon: That's cool.
Fr. Robert: All right.
Shannon: I get it.
Fr. Robert: We freaked out, we had to
calm the freak.
Dale: So I started out just doing titles and I said “Oh you
know what, it would be nice to have the actual link too”, so I kind of just—
Shannon: Oh yeah that's a good point.
Dale: Yeah so I kind of just tacked it on. And so that's why
I've got this slash n for a new line. And then I'm kind of building the URL. So
I put in HTTP slash Youtube.com slash watch, you know v equals, this is what
you see in the address bar and I essentially build the URL and so then I tack
on video ID, video ID.
Shannon: Oh cool.
Fr. Robert: Yeah you still – and remember, we've done that before,
he’s just appending little pieces of data he already has on the list. He knows
that every single thing that he wants to send back to Youtube is going to start with HTTP youtube.com watch. What's unique is what comes
after and it’s coming out of the list that he created.
Shannon: Right okay.
Dale: That's right, and then I sort it alphabetically,
essentially because I sort it by the title because that's what appears first.
Fr. Robert: Right.
Shannon: Ha cool.
Fr. Robert: And we just showed the audience how to do that. So
that stuff in – I'm serious I know people are going to freak out. That stuff in
the middle, it looks crazy scary, it really isn’t because all it is is data.
Shannon: I think it mostly looks scary because we’re not
familiar with Youtube’s APIs and things like that.
Fr. Robert: Right, right but if you would've think of all that
code as just a way to open up an external file, it works almost exactly the
same way. Dale’s doing a little bit of sanitization of the data that's coming
back to make sure he’s only pulling out the chunks of data that he thinks is
important but it really is no different than just getting a stream of external
data.
Dale: And yeah that's pretty much it. And then I kind of
print out the search results. I loop through video metadata for metadata and
video metadata. And then I print metadata and then I just add this slash n just to kind of give things some nice spacing. That's what
these slash ends are for. Python sees those and they just create an actual new
line space.
Fr. Robert: Right, right.
Dale: So it’s like a carriage return or the BR tag and HTML.
Shannon: So can you run this.
Fr. Robert: Let’s run this.
Dale: Yeah let’s do it, let’s do this.
Shannon: Yes.
Dale: All right. So search for a video. Let’s do Peter Capaldi and actually I—
Fr. Robert: Do you want 145,000?
Dale: No actually by default the API will only, this call
will only return five results by default.
Fr. Robert: Right, right.
Shannon: I can add another argument to make my max results I
think, 50 per call.
Fr. Robert: Right and that's just on Youtube’s side because they don’t want one person just completely taxing their server by
making crazy calls with a million results.
Dale: Yes, yes which I do every day anyway.
Fr. Robert: Goes right away around it.
Dale: So here we go, so search for a video, Peter Capaldi. And there you go.
Shannon: Cool.
Dale: So you've got them out of – this is I guess showing up
first because of the asterisk.
Shannon: Asterisk, yeah.
Dale: Yeah, so here we go, here are the links that are
generated here.
Fr. Robert: Nice, nice, nice and that's it.
Dale: That's it, yeah.
Fr. Robert: That's exactly – so again, you brought in some
external data, you sorted it the way that you wanted it and you presented it in
a format that people could understand.
Shannon: I could see where this would be very handy as a
podcaster using the Youtube API to track our Youtube videos.
Fr. Robert: Um-hmm, and beyond that, so if we wanted to get kind
of, I'm not going to call it grayhab but it’s kind of
morally questionable, people use APIs like that all the time to scrape
information out of services and it’s exactly what we did. You would just write
a function that would keep hitting the API and just keep getting more and more
data, more and more of the records until you had all the information that you
wanted. So you know, this again, this is a fundamental part of programming and
it’s an incredibly useful tool. I will say this, if you're going to practice
this and I hope you do. You will have to get your own API key. Either that or,
Bryan if you could go my screen, go ahead and use this API key, it’ll work just
fine. No but seriously go to Youtube, get yourself an
API key and practice. You can take Dale’s code, put your API key in to it and
just make it work. Snubs, and I think you can agree
with me on this, once you make it work with Dale’s code, start tweaking it.
Shannon: Oh yeah.
Fr. Robert: See if you could sort it in a different way. Why not
get a few more records?
Shannon: Get a few hacks, get them in
trouble with Youtube.
Fr. Robert: Yeah, yeah.
Shannon: I'm okay with this.
Fr. Robert: Now, Dale thank you for this, I mean this is the sort
of stuff that it’s difficult to digest at first but I think we've just given
our code monkeys an incredible resource.
Shannon: It’s hard to understand that code. It is a very hard
to understand the imports and then the stuff, integrating Youtube into it but I'm brave and I will try my best to make it work. And then when I
get frustrated I’ll call Dale.
Fr. Robert: Yeah, and I have to say, I'm glad that you chose Youtube Dale because there are, as Patrick Delhanty notes, there are a lot of APIs that are not well
documented and you just basically have to keep pounding on them until you find
out what kind of data they were doing.
Dale: Yeah, yeah.
Fr. Robert: Youtube is actually pretty
well documented.
Dale: It depends on what you're trying to do.
Fr. Robert: For our people who just want to pull basic data out of
it, it should work just fine.
Dale: Yeah, I mean their library, you know they have
libraries for PHP and JAVA and I think in Python as well. Yeah and like some of
the stuff is better documented than others but totally, it’s a lot of fun to
play around with though.
Fr. Robert: Dale, not that you've kind of blown minds, where do
you think we’re going to go next week?
Dale: Gee I don't know.
Shannon: Go farther?
Dale: I don't know.
Fr. Robert: I think we need to pull back a tiny bit and let
people's brains recuperate a little bit. So maybe we need to combine some of
the lessons we've already got to—
Shannon: Right now I want to do some sorting.
Fr. Robert: To sort, yeah.
Shannon: For sure.
Fr. Robert: And maybe output to a file because we already read
from a file, I think we need to be able to push back out, yeah?
Dale: That's brilliant.
Shannon: That's cool.
Fr. Robert: Yeah let’s do that so now you got the circle will be
complete.
Shannon: Circle of code.
Dale: Dale Chase, Discovery Digital Networks, thank you so
very much for being our code warrior sir. It is always great to have you each
and every single week and now that you've done some brain explodage,
I think your reputation as a legendary code warrior is cemented.
Shannon: Hahaha.
Dale: Awesome.
Shannon: I was already there.
Dale: Thanks for having me guys.
Fr. Robert: You take care, I’ll see you
next week.
Shannon: Oh where can we find him?
Fr. Robert: Oh duh, I'm sorry, wait hold a bit. Wait, hey Brian
can you cue up my jam.
Shannon: We should probably mention where we can find Mr. Dale
Chase.
Fr. Robert: You thank you for that. Oh yeah, so I mean if like the
Doctor Who—
Shannon: Especially if you listen to music by Doctor Who.
Fr. Robert: …Dale, you are not just a programmer, you are an
artiste. Tell us a little bit about your art.
Dale: Yeah I make mostly geek centric hip-hop music, some
sort of jazzy infused smoove type of raps that talk
about geek culture like as you see I've got like a Who’s Your Doctor jam. Yeah,
hacker enthusiast coding where you know all that's stuff’s in there. Just chek it out at dchase.bandcamp.com.
Fr. Robert: And seriously folks, get over there, get some of his
music. It’s not just fun to listen to, it’s actually really, really good.
Shannon: It is and he gets some great guests to sing with him.
Fr. Robert: Why the listeners are going “What's going on?” Again,
Dale Chase thank you, thank you, and thank you.
Dale: Thank you, take care guys.
Shannon: Bye.
Fr. Robert: Now I know that was a lot. I know that we really kind
of hit our audience a little bit buy you know what's
good? We got show notes.
Shannon: Oh I was going to say rice krispies treats but those are good too.
Fr. Robert: Those are good too, yes. But right now I think we do
need the show notes more than the sugar.
Shannon: Yeah like crazy, so we have our Github link as long as all of our show notes. You can find those at twit.tv/c101 or
slash code.
Fr. Robert: Yeah, slash code, also don't
forget that we’re also on iTunes. And we’re actually still doing really well.
Shannon: Yeah we are, it’s crazy.
Fr. Robert: I don't know what's going on?
Shannon: People watch us on iTunes.
Fr. Robert: Spread the word. Tell people about Coding 101. Tell
them that you know what, we’re a little bit different.
We like to give people code, we like give them learning but we also want to
have a little bit of fun. So we’re going to give you stuff that let’s you geek out.
Shannon: I have a project for you guys today. Share the show
with one of your friends. Just one.
Fr. Robert: Just one. Just one.
Shannon: Yeah just share it with one friend.
Fr. Robert: And post it on G Plus and then we’ll send you
stickers.
Shannon: Oh that's a good idea. Do we have stickers, we should
get stickers made.
Fr. Robert: We do.
Shannon: Let’s do stickers.
Fr. Robert: Let’s do stickers. Actually yeah, we should do – yes
okay.
Shannon: Okay.
Fr. Robert: New project stickers.
Shannon: We’ll talk later. We’re also on Youtube of course you could try this. You could try this code on our show notes and put
those in order from Coding 101 number one down to number, what are we on? 15?
Fr. Robert: 15.
Shannon: Oh my goodness, 15 episodes already and those are over
at Youtube.com/twitcoding101.
Fr. Robert: That's right also we've got a crazy vibrant G Plus
community.
Shannon: We do.
Fr. Robert: It’s about – what are we up to? 900,
yeah 900 and some members. We’re almost at a thousand.
Shannon: Yay thank you so much for joining us there.
Fr. Robert: These are crazy active members. If you go our G Plus
community, you can post questions. You can post your code which Shannon will
then bring on to the show.
Shannon: Yes I will.
Fr. Robert: In other words, if you want to know anything about
programming you got to join us. Be part of the Coding 101 army by going to plus.google/twitcoding101.
Shannon: plus.google.com/twitcoding101.
Fr. Robert: Do it now, do it.
Shannon: That's how you get there. And share your examples to
me and I’ll show them off.
Fr. Robert: Um-hmm, um-hmm. Now if you're not into the G Plus
group, I think you can also find—
Shannon: We’re on Twitter.
Fr. Robert: Yeah.
Shannon: I'm on Twitter all the time. Talking
about everything from weddings to couponing to code.
Fr. Robert: I'm mostly talking about code.
Shannon: Yeah that's kind of scary.
Fr. Robert: That's kind of scary but you can find me at
Twitter.com/padresj that's @padresj.
Shannon: And I am @Snubs.
Fr. Robert: And finally, finally you know we actually do this show
live, we’re live right now, this is the real world. If you can find us on
Thursdays at 1:30 pm pacific time and as long as you're there, you’re freaking
Snubs out, you can also join us in the chat room at irc.twit.tv.
Shannon: Are people listening to us?
Fr. Robert: They are, I know right.
Shannon: Hello.
Fr. Robert: It’s kind of weird stuff.
Shannon: Is that why we wear these things? That's crazy.
Fr. Robert: Actually mine’s not connected to anything. Yeah but
until next time, I'm father Robert Ballecer.
Shannon: I'm Shannon Morse. End of line.
Fr. Robert: End of line.
Shannon: Doo, doo, doo, oh look at my face. Uh-oh.
Fr. Robert: Time vortex.
Shannon: I wish I had a tortoise.
Fr. Robert: I would, I would time travel the heck out of that
thing.
Shannon: Time space, time space, time space. Space,
space, space, space.
Fr. Robert: Time and relative dimension.
Shannon: Is this like a preset in the tricaster?
Fr. Robert: No he’s bumpin’ it.
Shannon: It’s really cool!