Effective PyCharm Transcripts
Chapter: The Editor
Lecture: Working with the editor demo: Downloading RSS

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Are you ready to do some cool things with our editor of course. So we're going to work on a fun project here.
0:06 Let's go over to this podcast directory and now this is empty. We're gonna create a new PyCharm project here,
0:13 go again. Go to PyCharm open directory or you just drag and drop on macOS. We are in our empty project and it says we grabbed a
0:24 random broken interpreter for you. Let's not do that. Let's go and add an interpreter to create a new virtual environment.
0:33 We're gonna need to install a couple things you want to make sure you have this
0:36 isolated environment. Now, one thing I always always do when I create a new
0:41 virtual environment in Python is the tools that manage the packages of that environment are almost
0:48 certainly out of date. They're almost always out of date. So for example, notice our virtual environment is activated here but I do a pip
0:56 list, going to show a couple of things and it says, oh by the way, your version of pip is out of date. Also your setup tools is and so is your wheels.
1:04 So something I always do is this just to make sure that we have the latest tools to work with. We could have also done that over here in the Python
1:18 packaging section, but that does it one at a time. I'm just kind of a fan of having it done in the terminal.
1:24 We have the brand new project that we're ready to work with. We're going to have 2 Python files,
1:29 one that orchestrates the flow of the application and one that does the actual heavy lifting the work. The first one let's call that program.
1:40 And the second one, let's call this service as it's going to go download some stuff from the internet for us.
1:47 And because we're going to be using external packages, we want people to be able to download this and install it and run it by
1:54 installing the things we need. We do that through a 'requirements.txt' file. It's empty. And that's okay for now.
2:04 We're gonna start over here with our program and one of the things I like to
2:07 do is always have the highest level concept of our program right at the top and then the functions that make that up below.
2:14 So we're gonna write a function called main over here and we'll put some stuff in
2:18 there. Maybe we're going to do something like that's something I'll print out a header
2:24 and here we'll just do a little print notice as we type a couple of things
2:29 about the editor that are really interesting Python obviously uses four spaces to indicate scope. Right So if you indent here,
2:38 these four spaces, this print is part of the function, not something that happens after it. What's really awesome is as you type,
2:47 it knows if there's a colon and the next thing you're gonna do is go and have four spaces. So that's really great.
2:52 We'll have a print statement here and let's just put little star across enter again.
2:59 Have that like this one other thing that's really amazing that I use all the time is I actually wanna have three print lines now.
3:07 Go down and type print again. But what's better is if I could just say I want exactly this again, I could copy and paste it but I can hit on Mac.
3:15 OS 'cmd+d' or 'ctrl+d' on Windows or Linux and it makes another one and another
3:19 one just like that. So I'll just go put something here and we'll just keep it nice and easy. Note, just simple four manages eyeball it here.
3:30 Okay, so this is our little print header thing and what we need to do is we're gonna go over to our main we want to use services or features from
3:41 our service file. So in the editor I can obviously go up here and type import it. All sorts of good auto complete or all the built ins.
3:52 I could even import 'antigravity' which is fun but not helpful or what we could import is our service right there.
3:58 Right. But let's imagine. I hadn't done that yet. I just know that over in the service module there's going to be a function called
4:06 download info. Figure out what that is in a minute notice right away. PyCharm says, well if you're going to use this thing called service there is
4:13 a braking problem. There's reds quickly as you can see the little red error up
4:18 here, the error up here and there's this light bulb and the light bulb called a code intention. This one is not actually helping us here,
4:27 but we go over there now we get the red light bulb which shows us the error and it says do you want to import that?
4:32 Because that's what's required for this to work? Oh yeah, I did want to do that. You could of course use the mouse.
4:39 But really what I normally do is like over your hit, 'Alt+enter', enter and it goes to the top.
4:45 Beautiful. Now I said I want to have this function called download info. So the idea is we're going to go to the podcast is the talk Python on
4:54 the podcast. We're going to download the rss feed and parse it and then we'll use the service to ask questions about it.
5:01 Like what is the title of this episode? What is the number of that episode and so on. Will be able to ask questions about it.
5:08 But let's just go and download that information first. I notice there's a problem right away. It says, you know, this thing doesn't exist.
5:17 Well, guess what we alt + enter it. It will go and create that function over on the services again.
5:23 This is part of that understanding the entire project structure, right, I'm over here working in program and it says, well, you need to modify service,
5:31 that module. So let's do that, That's great. But when you go over here and focus on how this function works
5:38 for now. What we want to do is we want to go to the talk Python to me podcast and pull down the rss feed. Let's have a quick look at that.
5:47 Over here. We've got a source version. Just a view, source version of the Source and it is pretty opaque.
5:54 There's a lot of like embedded HTML and all sorts of stuff, but I took this URL and I threw it into a visualizeer that
6:02 views xml a little bit better code beautify. So what we got here is rss at the top and within there there's a thing
6:10 called Channel and that describes stuff about like the title and description and then there's a bunch of items. Here's an item,
6:18 here's an item, here's an item and each item has a title. A link here is the enclosure for the MP3 as a pub date and so
6:28 on. What we want to do is somehow download and then understand this file.
6:35 Maybe that sounds complicated and when you look at it over here it feels like it's probably going to be complicated, but remember this is Python,
6:43 it's going to be fine. Now. Right away you can see the URL variable is creat out right here. What does that mean? If I click on it,
6:51 I'll even get a little more info. It says this variable is not used, right We have a variable,
6:57 it's not used as if we had an argument like is admin or whatever. You can also see the same type of thing. Here's an argument, not user parameter.
7:06 Here is a local variable not used. And generally that means something is either getting missed forgotten or you're doing stuff that no
7:13 longer makes sense and you should remove it. But the reason it's great for us is we just haven't quite got started yet.
7:20 So now what we're gonna do is we're gonna go and use requests. Remember over here we have no requests or Python packages,
7:29 we have no request. So we're gonna say the responses 'requests.get(url) All right, well what's happening here?
7:38 Clearly another one of these errors so we can get some help from PyCharm. It says, how about this? I think request is a package on PyPI.
7:48 How about we install that and do an import requests at the top. Yes, please do that. Fantastic. Now why does this has requests? Not sure. Okay, now,
7:59 what we can do also it has another warning, it says we want to put this into the requirements. Now sometimes it's a great idea to pin your version.
8:08 Sometimes you don't want to because this might evolve over time, I'm just going to specify by itself. But if this was your app,
8:13 you're maintaining it, you probably want to pin the version. So we hit okay, here we go. Now requests is recorded in there And it's ready to go.
8:21 So what else can we do? We want to make sure this succeeded and requests as this cool thing that if you don't get a 200 or a two,
8:27 a one or a redirect that then results in one of those, it'll crash. You can say raise for status and notice all the awesome help that
8:35 we get here. All I said was dot but remember I could just type status he says, oh, status code or raised for status,
8:42 which did you want? So that is really, really cool and we can get down and just get the text as 'resp.text' So now we've got to this point,
8:53 presumably we've got all this gnarly looking stuff and let's just do a quick print of the text and see what happens. Let's try to run this. Well,
9:01 we haven't got no run configuration so we can add configuration slowly or we can right click and say run and get it quickly.
9:08 But when I run it it's not going to do what you expect. Don't think this print statement won't run. Great. Run It ran exited with code zero,
9:18 that's success. But no output what happened over here. Unlike some languages. C++, C# and so on.
9:27 The fact that of a main method means nothing to Python. If we don't call it explicitly, it's not going to run. So you're probably familiar with this,
9:35 this concept at the end. So we say 'if__name = __ main', that means this thing is being invoked directly and not imported.
9:47 So you should do the thing that you do, which will be to run Main. This is so common that in PyCharm they're going to help us out.
9:55 So instead of typing that if I just type main, the thing with the parentheses at the top, that's just auto completing the function I wrote.
10:03 But the one at the bottom, see the great text if dunder name equals under main. Boom, that's one of these things called a live template in PyCharm.
10:12 We also have them. So if I like a class A I want to have a property, I could just type prop and then type name of the property without
10:21 auto complete. There there we go. So there's all these cool live templates that are available for us and they work sometimes
10:28 in classes sometimes at the function level, sometimes in HTML. Sometimes in Python,
10:33 you can go over to the settings and search for live templates and then say, well I'm interested in the ones for Python,
10:41 like completing dictionary comprehension or something like that. Okay, so very, very cool feature of the editor of these live templates and
10:51 let's just go around the main do a little cleanup. Now when we run it. Here we go, there's our super crazy Xml down there.
11:00 Notice how large that scroll bar goes. So it looks like this is working. This download is working, but we don't want to just download it.
11:09 We want to parse it and understand it again. It sounds complicated, but because it's Python, not so much. Let's have up here at the top.
11:17 Let's have this thing called episodes. And let's make this be a dictionary. Right? So we're going to store by maybe having the show ID.
11:25 Or episode ID here and then information about it. Then what kind of information do we want? We're going to use this thing called a named 'tuple'.
11:33 We could use a class but there's so many features in the editor around named 'tuple' I'd like to show you. So let's have a quick look at that.
11:40 I'm going to have this type, I'm going to call me putting this order an episode. This will be a named tuple.
11:47 Again, I don't have to remember to import it happens automatically for us.
11:54 PyCharm also groups stuff like so this is from the standard library and this is an external thing. So for example,
12:03 I had something else that came from the standard library. It groups them up here and then puts the third party stuff there.
12:08 Obviously when we don't care about cache. So here we're gonna have a named tuple and what goes in here. So I know there's arguments. But what are they?
12:18 I can hit 'cmd+p' and it will actually show you all of the information.
12:24 That's super super helpful right now what we gotta do is say the name like episode and then a string that has the fields.
12:32 So let's have a title link and uh date. And also let's give it an ID. Like an episode ID. Okay beautiful. So this is looking really good.
12:45 That's what we want to store and want to create one of these episodes and we're going to put it in there. We'll look it up by it's ID.
12:52 Well how are we going to do that? How are we going to parse all of this text apart?
12:56 What we're gonna do is going to create a dom xml document object model. Using an elementary where does 'Elementree' come from?
13:07 I'm not sure. Let's find out. Oh look it comes from 'xml.etree. .ElementTree. There we go.
13:14 That's what we want. And then it has this function 'fromstring' like this text Now, do we need this variable text over here separately?
13:24 Probably not. We could just in line that right? We could take this and just write that there.
13:28 I could go and change the code or I could go to this and hit 'ctrl+t' and type in line to see more about this later the refactory in support
13:37 but let's just clean that up a little bit there. Not really returning things. Wouldn't stop there and now what we need to do is
13:43 just loop over all of the items. Remember this means something very specific when we talk about items,
13:50 it's this thing that has the title and the link in the pub date. Do we get the items? Well we use expats, we go over here and we say find all,
14:02 we have the rss and then we have channel in the way of item together item
14:10 and let's just print out the length item to make sure that we're getting something here Alright again, wait for to download 324.
14:19 Perfect. That looks like we're getting something that makes a lot of sense.
14:24 So let's go out here and just quickly loop over these and save that information. I'm gonna want the number of the item as well as the actual episode.
14:34 So let's say idx, item in item not the items itself but we want to turn that into a tuple with the number and then the value.
14:45 So this would be enumerate, You can also say 'start=0' because our podcast actually started on episode zero. Fantastic.
14:56 Now let's just go over here and create one of these episodes. We'll say here's an episode, remember this is our named tuple what goes in here
15:05 now it shows me real quick but if I move away and I forget that I get 'cmd+p' again, see title, link date and ID.
15:12 Okay so the title, this would be 'item.find'. We can just give it this and go and get the text because we got what
15:21 we find is a node we don't want the node, we want just the value. And again we're going to do this a couple times So 'cmd+d'. 'cmd+d'.
15:30 So on this will be the link notice the link is highlighted for the name tuple and this is really amazing.
15:36 See and has these variables and it knows their arguments just by the fact that they're
15:40 there. That's really cool. What's the next one date would be pub date like that And then the last one is just going to be the item and we're going
15:51 to go to our episodes and put episodes will go to the ID Is going to be E. Like this. Fantastic. So if we go over here we should be able to
16:06 see something if we just print on our episodes our update is having a problem and I believe that because that's a capital D.
16:13 Let's again here we go. Look at that episode zero is actually this. It looks like we've got it backwards doesn't it?
16:22 So we're going to need something like blend of 'items -id' there we go. Let's try this minus one. Perfect 323. and we go a little to the right 322.
16:43 322 Fantastic. Look at that, we're downloading this off the internet and we've been kind of bouncing back and forth between
16:50 writing code in the future. Writing code see in the future but hopefully that felt entirely seamless to you. A couple of things I want you to notice.
16:58 We did not go to the documentation, we didn't go to the documentation for requests. We didn't go to the documentation for E tree.
17:05 We didn't go and like have to look back at this when we were down here to try to remember. Although I think I did once but that's just weird habit
17:13 I wouldn't have had to I could have just hit 'cmd+p' downside of their and it showed me well that one was the date and so on.
17:23 Why didn't we have to do that? That's because the editor is supporting us all along the way. We didn't have to go to PyPI.
17:29 to realise we're going to install requests. I just said I think I want to use requests. It's like well that's a package.
17:35 We should probably put that in your 'requirements.txt'. And we should import it up here and we should pip install
17:40 it. All of that stuff happened seamlessly. This is just scratching the surface and we're going to keep going on this example.
17:47 I want to pause and kind of reflect on what we've done so far and appreciate where we are.


Talk Python's Mastodon Michael Kennedy's Mastodon