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