Effective PyCharm Transcripts
Chapter: Refactoring
Lecture: Moving code

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's talk about another cool and powerful. Refactoring So what we've done over here in this 'actors.py' f is we
0:08 put every class that's going to be participating in our game. So we've got the creature, we've got the wizard,
0:13 we've got the small animal. One of the things I prefer to do in general Not always, but in general is to have a class dedicated to its own
0:22 file. So if I want to go mess with the wizard, I know right where to go and how to make changes to it and so on
0:27 But what I'm going to do is move this wizard over to its own file Now I could go over here and right click new Python file,
0:38 call it something like lowercase wizard and then copy this over and that would work except remember other parts of our program depend upon this.
0:47 So here we say from actors import wizard. If I were to move that code manually over to another file,
0:53 this is going to break. It's going to say there's no wizard and actors because it's not, it's somewhere else wherever I put it,
0:59 we're going to not just want to move the file or move the code into a file. We also need to fix up things like these imports or if I had
1:07 done something like import actors and then actors dot wizard and work with it in this
1:13 way. Right. We need to make sure that all that stuff is consistent.
1:16 There's this really cool re factoring called move and let's do the main animals first and then we'll deal with creature in a moment.
1:23 First of all, let's go and come down here and just hit move and I'm gonna call this wizard and down here we now have the wizard class,
1:33 so we're importing the creature that we can derive from it. And besides a little white space at the end, this looks great importantly over here,
1:43 notice now we have from wizard import wizard and it's gone from up there, so it's not just moving the code is actually keeping everything in sync,
1:50 which is fantastic. Let me do the others as well. Do the dragon and the small animal. So in our game core, all that stuff has been fixed.
2:07 The other thing I would like to do is maybe just call this. Well now it's not actors, it's just the creature.
2:12 So I'm gonna just rename this file two creature base when I do that, notice it renamed and refactors that as well.
2:22 That's not exactly a move, but it's pretty close, make sure everything's running, we can attack something. We have been defeated. Let's try again.
2:35 We have handily defeated. A frog I believe. Noah toad. Alright, we're out so it looks like it's still working now. This is looking cluttered to me.
2:43 I don't know how you all feel about it, but let's go back and sort of give this some naming here.
2:48 Like I'm going to create an actor's folder and I want to put all of these in there. So we are last move action.
2:55 If we just take all of those and drop them in there, notice this is not a move button, but this is a refactor button.
3:02 Search for references. Boom. Go back to our game core from actors dot creature based actors dot small animal actors dot dragon.
3:10 So now our code is nice and organized. Here's the program, here's the game code,
3:15 here's all the animals and there's not a folder and all of those operations, renaming a file, extracting code to another file,
3:22 grabbing a bunch of files and throwing them into another folder.
3:25 All of that remained consistent because they're all under this general manner of a move re factory in PyCharm. Love it.


Talk Python's Mastodon Michael Kennedy's Mastodon