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

Login or purchase this course to watch this video and the rest of the course contents.
0:02 Now let's begin exploring the editor, and I think the right way to go about this is
0:07 to just write a simple little program and talk about all the features that are immediately obvious and stand out for us.
0:13 We're going to go create a little application that will go and download information about the Talk Python To Me podcast
0:19 and say what the titles of the recent episodes are and things like that. We're going to do this and then there will be some features
0:26 that they don't expose themselves because what we're building is not sufficiently complicated
0:32 so we are going to jump around, open some other projects later. But let's get started exploring the editor by— well, editing with it, right.
0:38 Let's go over here and I'll open up the terminal, and I'm going to cd into this directory here, this is the github repo that we're working with,
0:48 and we could go to PyCharm say new project, but with the virtual environments I do find it a little easier
0:53 to just follow the convention, so we can do a make dir, cd into the podcast, and then we can create our virtual environment like so.
1:02 And that little console trick or terminal trick there helps us get going. Remember you've got to say open directory on Mac or Linux,
1:10 but here we can drag it on macOS, so we're going to register that git repository because I want to make sure
1:16 you get this code checked in and you can play with it. Here is our virtual environment, let's click here and make sure that it's up and running,
1:24 yeah, if we ask which Python— that looks right to me. We'll go over here and we'll create a couple of files,
1:31 I'll call this program, and I am going to create one over here called service or something like that, for downloading all the data.
1:42 Okay, so up here, let's define the main method, and we'll do something like
1:49 "Hello, welcome to the Talk Python info downloader," or something like that. It wouldn't be much work to actually edit this
2:01 to have it download the MP3 or AAC files but, why don't I just do this, and let's say we're going to call some functions from this service over here.
2:11 So let's go write a function. Let's say this, download info and we're going to write some function here,
2:24 and then maybe we'll give it the ability to get some information, give it something a little bit richer. I'll pass it and I'll have def get_episode
2:35 and we'll have a number of the episode, I'll call it show_id, or something like that,
2:42 and then we're going to come down here and we'll just do nothing like that. Imagine I would like to call this function,
2:52 so I am going to try to call download, it's going to store the information somewhere, and then, we'll be able to get information back
3:00 like hey, give me the info about this particular episode, now I probably would have the download, just give you all the info
3:06 but, I am trying to create a little bit of state here something we could make slightly more interesting.
3:11 So, I can come over here and I can say download info, and it doesn't look like anything is happening, does it.
3:18 But, notice there is a little squiggly, if I go here, a lightning bolt will show up, and if I hit alt enter, it gives me all sorts of stuff;
3:27 I could import download info, like this, and if I hit that, it's going to actually do the right thing to put it up at the top here.
3:35 Okay, so that's pretty cool, and again, we can navigate back and forth as we saw it. Also, notice over here, this doesn't have squiggles any more,
3:44 this one, not here, but on this part it does seem this parameter show_id is not used.
3:52 This is super helpful in discovering bugs, because maybe this is like is the user admin yes or no, and the function is supposed to check
4:00 whether you're an admin before you can do certain things, if you see these squiggles, they are like, wow, we're not checking,
4:06 we're not using this admin feature, this could be a problem. So that's a really cool feature, you can see obviously that there is a syntax highlighting
4:14 there are various other things, notice here there is a pep 8 no new line, so we can fix that. Now we can go and download this info,
4:24 I kind of prefer, let's do this— for the look at these things like so, so we can come down here and I can say service.
4:32 and you get immediate intellisense, right, download or auto complete, download info and show info,
4:40 so let's suppose we're going to download this information, and then we want to get just some details about various episodes,
4:47 so let's say for show id in a range, let's say 100 to 130, good enough, those will definitely exist. We can go over here and we'll say info=service,
5:03 notice, really nice intelisense there as well, we'll get info and we'll have the show id like this, alright, so this is coming along pretty well,
5:14 let's go and we'll focus on this download thing, so we can again command click and just navigate over there,
5:20 that's a small app we can find it right, but in something larger, command click go over here. So, what do we need to do,
5:28 well, we need to go and actually download something from the website, from talkpython.fm, what we're going to use is
5:35 if we go over here to talkpython.fm/rss it's going to pull up a bunch of info, and Firefox tries to be a little too clever for us,
5:44 so I am not going to worry about that. We'll go over here and we'll say something like url= that, and then we want to use Requests right,
5:54 who wouldn't want to use Requests? We'll say resp=requests.get, now, what's wrong with this code—
6:04 well, in a virtual environment, if we come down here and we say pip list, there is no Requests, right, by the way, pip install-- upgrade setup tools,
6:17 so that's a good idea, this is like 10 major versions out of date. Anyway, not required but always worthwhile.
6:26 So notice that PyCharm knows there is a problem here and actually it knows we're not using url, but if we pass it there, that's happy.
6:33 So what's the problem here, what is Requests, we have no idea, so we'll say import requests, but it was found somewhere else, so that doesn't work.
6:40 Now up here, when it's used like this, it says, maybe you should install the package, it doesn't exist,
6:44 notice down here, what's it doing, it's installing Requests, great. Now, if I had a requirements.txt it would actually also propose we put it there,
6:54 but we are not going to do that for the moment. Okay, so this is working, we can say response.raise_for_status,
7:01 if for some reason it doesn't work, it should work, but if it doesn't, now what we need to do is we need to go and load this up into an xml dom.
7:10 So how do we do that? We use this thing called ElementTree, and we can import this from xml.ElementTree, like so,
7:19 and we'll say fromstring, so resp.text is going to be the raw rss text, and then down here we'll have the dom, so we can do whatever we want with this,
7:33 we can start to loop over this and pull items out, so the first thing that we need to do is come in here and say items,
7:42 and we're going to do some kind of selectron, so we'll say dom.findall('channel/item')
7:49 if you actually look at the rss it's an xml feed, and it has an rss root but you don't say when you're doing searches,
7:56 but then there is a channel which contains many items. So let's just print out the length of items, just to see that this thing is working.
8:05 Now, if I run this, it's probably not going to do what you would maybe thing it's going to do, so we'll say run—
8:13 nothing, darn it. Why is nothing happening— it's because we didn't call main, Python, as you probably know,
8:21 it's not like if you have a static main void it's just going to get called, so what we do is we use this convention, we say if __name__ == '__main__'
8:38 then we'll call main, and that is going to work, with little formatting, just fine. And because these little expressions are very common,
8:45 PyCharm actually has what are called live templates, so if I type main, the top one there is the function I wrote,
8:51 but notice the bottom, if I hit tab while that is selected, it's going to write that same code for us,
8:57 and I'll just go ahead and call whatever function I named it here. So these are live templates, and there is a bunch of them,
9:03 you can create your own, I'll show you how to do that in a little bit. Okay, so we're getting closer, let's run this,
9:08 it's thinking, wow, it found a 140 episodes, you've got to love a good zero based podcast. Okay, so that's awesome, we found a 140 episodes,
9:18 it looks like it's sort of working, let's go over here now, we do need to store this information somewhere,
9:26 and we probably want to convert it, so let's go over here and we'll say from collections import named tuple and down here we'll have episode
9:39 it's going to be a named tuple of— give it the name of the type and then, what's it going to have— it's going to have a title, a link and a pub date.
9:50 And let's also give it a show id. Alright, so this is nice, let's just put this as count,
10:00 or episode count maybe, so it didn't conflict, so we have our episode count and we're going to load up for each one of these things into there,
10:12 so we'll do like this, let's say we'll have a episode data, just a list, actually let's make it a dictionary, that will be even more fun.
10:21 All right, so we'll come through here and what we need to do is loop over every one of the items, so a for item in items,
10:30 this is going to give us our little tiny xml element and then we can do things like this, we can say episode= create one of these
10:39 now, what are we going to do— check this out, PyCharm knows about named tuples, and before I move the mouse
10:46 it went in there and any time you're sitting in a method, you're like what do I pass to it, you hit command or control P
10:52 and it will tell you check out how awesome that is, that comes from defining this named tuple right there, it gives us this, so perfect let's do it,
11:01 let's say item.find and we'll give it the title and then text, so that's a little xml query thing we'll do item.find
11:11 what's next, what are we looking for here, we're looking for a link, so this will be the link,
11:26 pub date text and then we need to convert that into a date we'll just leave it as text, but in a real app we'd convert that to a datetime;
11:32 and then finally, I want to put the item here, so let's do this, say idx for index and we'll do an enumerate on this,
11:41 I'm sure there's some kind of xml query I can do to get the show number out and that's what I'd do in a real app, but I am just kind of feeling lazy
11:50 so let's just say episode count - index because these are the newest or highest number first. We'll do that, and then we'll just say
12:01 episode_data[episode.show_id]= episode it's not append because it's a dictionary.
12:20 Now we can go down here and write this really quick so we'll go over here to episode data and we'll say turn episode_data.get(show_id)
12:33 that way if it's not there it will just give you none rather than crashing, maybe that's what we decide we want.
12:39 Let's go over here and just see that it works, I'll just say print something like this where we print out the number and then the title, so info.
12:51 now look at this, no help, how frustrating so I know this is show_id but I'm going to show you the fix in a second
12:58 let's just see that it works info.title, now let's give this a run. Boom, now it turns out that title on the show number these are all together
13:08 so we got them in that order, but then we ask for them in the other; now it looks like I'm off by one with my number,
13:18 oh yeah, yeah, yeah let's go back to this here, let's just subtract one that should work for us. Here we go, 129, number 129, okay,
13:30 so we've got our various pieces back and we're using over here our non half-closed range so we only get it at the 129 not 130, but this is working.
13:40 So the final thing I want to show you here in this little demo is this part right here, so I got this back service.get_episode, that was cool
13:53 and then I did this and I got nothing, in fact, even inside of service, this if I say dot— nothing, PyCharm doesn't know what this is.
14:04 So let's add type annotations to this for example, so I'm going to say: and the type this is an int, and it returns an episode, not a list.
14:16 So if we come over here and we do this now, dot, ta-da, look at that show_id, link, pubdate so with just the tiniest little nudge
14:25 we can tell PyCharm what comes back from here and actually it's because the dictionary is kind of—who knows what it holds,
14:32 once we get the data back from here, we could pass this from function to function to function and PyCharm would still know hey that's an episode
14:40 because I know where it came from and I know that it started out an episode and it's not changing, you never reassigned it.
14:46 So we'll see that we can use these type hints in really powerful ways you could also use them in the docstring style
14:51 but if you're using Python 3.6 or 3.5 really take advantage of these, because they're awesome.
14:59 I think that wraps up our little demo for downloading some stuff here you can see that we can lift out what the various episodes were
15:06 except for if I go over here, one other cool little feature when this crashed, as I was fiddling around with intellisense
15:13 and left it in a broken state, two things— one, I don't have to save, there's not really a clear save button all the time
15:21 you can go over here and there's probably a save somewhere, but it automatically saves, and when I run it, all the files are saved, that's awesome.
15:28 Also, when I was away and I ran and something went wrong and it shows me right here, here's where the problem is,
15:36 you click it, it takes you straight to it and then highlights it, so that's cool, I just need to put that away because I was fiddling with it.
15:42 Awesome, there we get a little info like I said, you can expand what we're doing here, in terms of pulling the data out of the rss feed
15:52 and you could have like the mp3 or you could pull out the show id there's a place actually where there's an extension to the rss feed for podcast feeds
16:01 that actually includes the episode number and things like that. All right, so pretty amazing I hope you think, and that's just scratching the surface,
16:10 we're going to look at a whole bunch more things that we can do with the PyCharm editor.


Talk Python's Mastodon Michael Kennedy's Mastodon