00:0.04 Are you ready to do some cool things with our editor of course.
00:3.19 So we're going to work on a fun project here.
00:5.64 Let's go over to this podcast directory and now this is empty.
00:8.55 We're gonna create a new PyCharm project here,
00:12.94 go again. Go to PyCharm open directory or you just drag and drop on
00:17.01 Mac OS. We are in our empty project and it says we grabbed a
00:23.66 random broken interpreter for you. Let's not do that.
00:26.62 Let's go and add an interpreter to create a new virtual environment.
00:32.54 We're gonna need to install a couple things you want to make sure you have this
00:35.17 isolated environment. Now, one thing I always always do when I create a new
00:40.93 virtual environment in python is the tools that manage the packages of that environment are almost
00:47.46 certainly out of date. They're almost always out of date.
00:50.45 So for example, notice our virtual environment is activated here but I do a pip
00:55.1 list, going to show a couple of things and it says,
00:57.88 oh by the way, your version of pip is out of date.
01:1.54 Also your setup tools is and so is your wheels.
01:3.74 So something I always do is this just to make sure that we have the latest
01:13.74 tools to work with. We could have also done that over here in the python
01:17.85 packaging section, but that does it one at a time.
01:20.64 I'm just kind of a fan of having it done in the terminal.
01:23.04 We have the brand new project that we're ready to work with.
01:26.24 We're going to have 2 python files,
01:28.12 one that orchestrates the flow of the application and one that does the actual heavy lifting
01:33.04 the work. The first one let's call that program.
01:39.94 And the second one, let's call this service as it's going to go download some
01:44.21 stuff from the internet for us.
01:46.24 And because we're going to be using external packages,
01:49.09 we want people to be able to download this and install it and run it by
01:53.5 installing the things we need. We do that through a 'requirements.txt' file.
02:1.24 It's empty. And that's okay for now.
02:3.14 We're gonna start over here with our program and one of the things I like to
02:6.26 do is always have the highest level concept of our program right at the top and
02:11.51 then the functions that make that up below.
02:13.9 So we're gonna write a function called main over here and we'll put some stuff in
02:17.48 there. Maybe we're going to do something like that's something I'll print out a header
02:23.31 and here we'll just do a little print notice as we type a couple of things
02:28.31 about the editor that are really interesting Python obviously uses four spaces to indicate scope.
02:36.05 Right So if you indent here,
02:37.58 these four spaces, this print is part of the function,
02:40.31 not something that happens after it.
02:41.94 What's really awesome is as you type,
02:46.24 it knows if there's a colon and the next thing you're gonna do is go and
02:49.72 have four spaces. So that's really great.
02:51.47 We'll have a print statement here and let's just put little star across enter again.
02:58.5 Have that like this one other thing that's really amazing that I use all the time
03:3.08 is I actually wanna have three print lines now.
03:6.07 Go down and type print again.
03:8.24 But what's better is if I could just say I want exactly this again,
03:11.31 I could copy and paste it but I can hit on Mac.
03:14.16 OS 'cmd+d' or 'ctrl+d' on Windows or Linux and it makes another one and another
03:18.58 one just like that. So I'll just go put something here and we'll just keep
03:26.22 it nice and easy. Note,
03:28.0 just simple four manages eyeball it here.
03:29.67 Okay, so this is our little print header thing and what we need to do
03:35.08 is we're gonna go over to our main we want to use services or features from
03:40.9 our service file. So in the editor I can obviously go up here and type
03:45.71 import it. All sorts of good auto complete or all the built ins.
03:51.02 I could even import 'antigravity' which is fun but not helpful or what we could
03:55.29 import is our service right there.
03:57.84 Right. But let's imagine. I hadn't done that yet.
04:0.76 I just know that over in the service module there's going to be a function called
04:5.53 download info. Figure out what that is in a minute notice right away.
04:9.98 PyCharm says, well if you're going to use this thing called service there is
04:12.84 a braking problem. There's reds quickly as you can see the little red error up
04:17.7 here, the error up here and there's this light bulb and the light bulb called
04:23.61 a code intention. This one is not actually helping us here,
04:26.78 but we go over there now we get the red light bulb which shows us the
04:29.58 error and it says do you want to import that?
04:31.38 Because that's what's required for this to work?
04:34.44 Oh yeah, I did want to do that.
04:36.44 You could of course use the mouse.
04:38.23 But really what I normally do is like over your hit,
04:40.89 'Alt+enter', enter and it goes to the top.
04:44.34 Beautiful. Now I said I want to have this function called download info.
04:49.37 So the idea is we're going to go to the podcast is the talk python on
04:53.72 the podcast. We're going to download the rss feed and parse it and then we'll
04:58.47 use the service to ask questions about it.
05:0.41 Like what is the title of this episode?
05:2.22 What is the number of that episode and so on.
05:5.13 Will be able to ask questions about it.
05:7.11 But let's just go and download that information first.
05:10.54 I notice there's a problem right away.
05:13.25 It says, you know, this thing doesn't exist.
05:16.34 Well, guess what we alt + enter it.
05:18.74 It will go and create that function over on the services again.
05:22.59 This is part of that understanding the entire project structure,
05:25.68 right, I'm over here working in program and it says,
05:28.94 well, you need to modify service,
05:30.98 that module. So let's do that,
05:33.24 That's great. But when you go over here and focus on how this function works
05:37.42 for now. What we want to do is we want to go to the talk
05:41.32 python to me podcast and pull down the rss feed.
05:44.18 Let's have a quick look at that.
05:46.24 Over here. We've got a source version.
05:48.55 Just a view, source version of the Source and it is pretty opaque.
05:53.49 There's a lot of like embedded html and all sorts of stuff,
05:56.94 but I took this URL and I threw it into a visualizeer that
06:1.39 views xml a little bit better code beautify.
06:4.96 So what we got here is rss at the top and within there there's a thing
06:9.05 called Channel and that describes stuff about like the title and description and then there's a
06:14.11 bunch of items. Here's an item,
06:17.25 here's an item, here's an item and each item has a title.
06:21.95 A link here is the enclosure for the MP3 as a pub date and so
06:27.72 on. What we want to do is somehow download and then understand this file.
06:34.84 Maybe that sounds complicated and when you look at it over here it feels like it's
06:38.4 probably going to be complicated, but remember this is python,
06:42.44 it's going to be fine. Now.
06:44.46 Right away you can see the URL variable is creat out right here.
06:49.44 What does that mean? If I click on it,
06:50.87 I'll even get a little more info.
06:52.04 It says this variable is not used,
06:55.02 right We have a variable,
06:56.01 it's not used as if we had an argument like is admin or whatever.
07:1.84 You can also see the same type of thing.
07:3.31 Here's an argument, not user parameter.
07:5.21 Here is a local variable not used.
07:7.36 And generally that means something is either getting missed forgotten or you're doing stuff that no
07:12.68 longer makes sense and you should remove it.
07:15.14 But the reason it's great for us is we just haven't quite got started yet.
07:19.03 So now what we're gonna do is we're gonna go and use requests.
07:22.48 Remember over here we have no requests or Python packages,
07:28.88 we have no request. So we're gonna say the responses 'requests.get(url)
07:34.11 All right, well what's happening here?
07:37.61 Clearly another one of these errors so we can get some help from PyCharm.
07:42.44 It says, how about this?
07:44.25 I think request is a package on PyPI.
07:47.24 How about we install that and do an import requests at the top.
07:50.79 Yes, please do that. Fantastic.
07:54.54 Now why does this has requests?
07:57.24 Not sure. Okay, now,
07:58.27 what we can do also it has another warning,
07:59.97 it says we want to put this into the requirements.
08:2.84 Now sometimes it's a great idea to pin your version.
08:7.14 Sometimes you don't want to because this might evolve over time,
08:10.05 I'm just going to specify by itself.
08:11.68 But if this was your app,
08:12.94 you're maintaining it, you probably want to pin the version.
08:15.02 So we hit okay, here we go.
08:16.72 Now requests is recorded in there And it's ready to go.
08:20.14 So what else can we do?
08:21.2 We want to make sure this succeeded and requests as this cool thing that if you
08:25.07 don't get a 200 or a two,
08:26.6 a one or a redirect that then results in one of those,
08:29.67 it'll crash. You can say raise for status and notice all the awesome help that
08:34.43 we get here. All I said was dot but remember I could just type status
08:38.25 he says, oh, status code or raised for status,
08:41.54 which did you want? So that is really,
08:44.86 really cool and we can get down and just get the text as 'resp.text'
08:48.05 So now we've got to this point,
08:52.48 presumably we've got all this gnarly looking stuff and let's just do a quick print of
08:57.17 the text and see what happens.
08:59.06 Let's try to run this. Well,
09:0.25 we haven't got no run configuration so we can add configuration slowly or we can right
09:5.72 click and say run and get it quickly.
09:7.86 But when I run it it's not going to do what you expect.
09:11.42 Don't think this print statement won't run.
09:14.44 Great. Run It ran exited with code zero,
09:17.5 that's success. But no output what happened over here.
09:22.84 Unlike some languages. C++,
09:24.81 C# and so on.
09:26.19 The fact that of a main method means nothing to python.
09:28.75 If we don't call it explicitly,
09:31.64 it's not going to run. So you're probably familiar with this,
09:34.21 this concept at the end. So we say 'if__name = __ main',
09:43.14 that means this thing is being invoked directly and not imported.
09:46.31 So you should do the thing that you do,
09:48.44 which will be to run Main.
09:50.64 This is so common that in PyCharm they're going to help us out.
09:54.9 So instead of typing that if I just type main,
09:58.54 the thing with the parentheses at the top,
10:0.41 that's just auto completing the function I wrote.
10:2.49 But the one at the bottom,
10:3.48 see the great text if dunder name equals under main.
10:6.79 Boom, that's one of these things called a live template in PyCharm.
10:11.95 We also have them. So if I like a class A I want to have
10:15.26 a property, I could just type prop and then type name of the property without
10:20.0 auto complete. There there we go.
10:21.65 So there's all these cool live templates that are available for us and they work sometimes
10:27.21 in classes sometimes at the function level,
10:29.86 sometimes in html. Sometimes in python,
10:32.54 you can go over to the settings and search for live templates and then say,
10:38.32 well I'm interested in the ones for python,
10:40.66 like completing dictionary comprehension or something like that.
10:45.84 Okay, so very, very cool feature of the editor of these live templates and
10:50.32 let's just go around the main do a little cleanup.
10:53.62 Now when we run it. Here we go,
10:56.44 there's our super crazy Xml down there.
10:59.91 Notice how large that scroll bar goes.
11:2.54 So it looks like this is working.
11:4.54 This download is working, but we don't want to just download it.
11:8.04 We want to parse it and understand it again.
11:10.77 It sounds complicated, but because it's python,
11:13.54 not so much. Let's have up here at the top.
11:16.89 Let's have this thing called episodes.
11:18.9 And let's make this be a dictionary.
11:21.64 Right? So we're going to store by maybe having the show ID.
11:24.56  Or episode ID here and then information about it.
11:28.64 Then what kind of information do we want?
11:30.35 We're going to use this thing called a named 'tuple'.
11:32.85 We could use a class but there's so many features in the editor around named 'tuple'
11:36.19 I'd like to show you.
11:37.25 So let's have a quick look at that.
11:39.74 I'm going to have this type,
11:40.77 I'm going to call me putting this order an episode.
11:44.04 This will be a named tuple.
11:46.94 Again, I don't have to remember to import it happens automatically for us.
11:53.14 PyCharm also groups stuff like so this is from the standard library and this is
11:56.76 an external thing. So for example,
12:2.54 I had something else that came from the standard library.
12:5.34 It groups them up here and then puts the third party stuff there.
12:7.96 Obviously when we don't care about cache.
12:10.44 So here we're gonna have a named tuple and what goes in here.
12:13.95 So I know there's arguments. But what are they?
12:17.74 I can hit 'cmd+p' and it will actually show you all of the information.
12:23.08 That's super super helpful right now what we gotta do is say the name like episode
12:28.3 and then a string that has the fields.
12:31.03 So let's have a title link and uh date.
12:35.66 And also let's give it an ID.
12:37.13  Like an episode ID.
12:38.42 Okay beautiful. So this is looking really good.
12:44.14 That's what we want to store and want to create one of these episodes and we're
12:47.39 going to put it in there.
12:48.5 We'll look it up by it's ID.
12:51.44 Well how are we going to do that?
12:52.34 How are we going to parse all of this text apart?
12:55.94 What we're gonna do is going to create a dom xml document object model.
13:1.63 Using an elementary where does 'Elementree' come from?
13:6.71 I'm not sure. Let's find out.
13:8.84 Oh look it comes from 'xml.etree.
13:10.91 .ElementTree. There we go.
13:13.8 That's what we want. And then it has this function 'fromstring' like this text
13:19.58 Now, do we need this variable text over here separately?
13:23.51 Probably not. We could just in line that right?
13:25.8 We could take this and just write that there.
13:27.77 I could go and change the code or I could go to this and hit 'ctrl+t'
13:31.35 and type in line to see more about this later the refactory in support
13:36.14 but let's just clean that up a little bit there.
13:39.04 Not really returning things. Wouldn't stop there and now what we need to do is
13:42.55 just loop over all of the items.
13:45.33 Remember this means something very specific when we talk about items,
13:49.09 it's this thing that has the title and the link in the pub date.
13:54.54 Do we get the items? Well we use expats,
13:57.52 we go over here and we say find all,
14:1.04 we have the rss and then we have channel in the way of item together item
14:9.14 and let's just print out the length item to make sure that we're getting something here
14:14.36 Alright again, wait for to download 324.
14:18.7 Perfect. That looks like we're getting something that makes a lot of sense.
14:23.84 So let's go out here and just quickly loop over these and save that information.
14:28.34 I'm gonna want the number of the item as well as the actual episode.
14:33.56 So let's say idx,
14:34.45  item in item not the items itself but we want to turn that
14:41.78 into a tuple with the number and then the value.
14:44.8 So this would be enumerate, You can also say 'start=0' because our podcast
14:51.31 actually started on episode zero. Fantastic.
14:55.51 Now let's just go over here and create one of these episodes.
14:59.86 We'll say here's an episode, remember this is our named tuple what goes in here
15:4.92 now it shows me real quick but if I move away and I forget that I
15:8.03 get 'cmd+p' again, see title,
15:10.43 link date and ID.
15:11.64 Okay so the title, this would be 'item.find'.
15:17.34 We can just give it this and go and get the text because we got what
15:20.5 we find is a node we don't want the node,
15:22.38 we want just the value. And again we're going to do this a couple times
15:26.09 So 'cmd+d'. 'cmd+d'.
15:29.04 So on this will be the link notice the link is highlighted for the name tuple
15:34.69 and this is really amazing.
15:35.9 See and has these variables and it knows their arguments just by the fact that they're
15:39.56 there. That's really cool. What's the next one date would be pub date like
15:44.95 that And then the last one is just going to be the item and we're going
15:50.59 to go to our episodes and put episodes will go to the ID
15:55.37  Is going to be E.
15:57.45 Like this. Fantastic. So if we go over here we should be able to
16:5.68 see something if we just print on our episodes our update is having a problem and
16:10.79 I believe that because that's a capital D.
16:12.69 Let's again here we go. Look at that episode zero is actually this.
16:19.9 It looks like we've got it backwards doesn't it?
16:21.97 So we're going to need something like blend of 'items -id' there we go.
16:27.97 Let's try this minus one. Perfect 323.
16:36.95 and we go a little to the right 322.
16:42.04 322 Fantastic. Look at that,
16:44.25 we're downloading this off the internet and we've been kind of bouncing back and forth between
16:49.37 writing code in the future. Writing code see in the future but hopefully that felt
16:53.87 entirely seamless to you. A couple of things I want you to notice.
16:57.42 We did not go to the documentation,
16:59.93 we didn't go to the documentation for requests.
17:2.08 We didn't go to the documentation for E tree.
17:4.71 We didn't go and like have to look back at this when we were down here
17:9.16 to try to remember. Although I think I did once but that's just weird habit
17:12.04 I wouldn't have had to I could have just hit 'cmd+p' downside of their
17:19.04 and it showed me well that one was the date and so on.
17:22.24 Why didn't we have to do that?
17:23.63 That's because the editor is supporting us all along the way.
17:26.93 We didn't have to go to PyPI.
17:28.33 to realise we're going to install requests.
17:30.49 I just said I think I want to use requests.
17:32.03 It's like well that's a package.
17:34.24 We should probably put that in your 'requirements.txt'.
17:36.27  And we should import it up here and we should pip install
17:39.23 it. All of that stuff happened seamlessly.
17:42.34 This is just scratching the surface and we're going to keep going on this example.
17:46.64 I want to pause and kind of reflect on what we've done so far and appreciate where we are.
