Effective PyCharm (2021 edition) Transcripts
Lecture: Sourcery plugin
0:00 We've seen that PyCharm has many many refactoring is and it's really good,
0:05 the ones that it does great.
0:07 There's actually a couple of higher order ones that could be useful to us.
0:12 There are some extensions and plugins for PyCharm.
0:15 That might might be something you want to consider.
0:17 So for example, here's Sourcery 'sourcery.ai'
0:21 This is a plug in that has a free version and a paid version that you
0:24 can add to PyCharm that will do more refactoring help in much the same
0:29 way that you would expect. Right.
0:30 These little code intentions with 'alt+enter' it's like that but it has sourcery refactoring suggestions
0:35 as well. Let's take a look in our project.
0:39 There's not too many errors and warnings going on.
0:41 If you go through here, you don't see anything.
0:44 But let's go over to this dragon.
0:45 There's something interesting here when we were in this section with PyCharm,
0:49 it did tell us that this was not needed,
0:53 but it didn't have this yellow box here.
0:57 Technically the reason this is not needed is python says,
1:01 well you're gonna go this path and created or this path and created and then use
1:05 it here. To me. I really like to have it declared for sure.
1:09 What if we change this to an if else all of a sudden this becomes a
1:12 potential run time error about that variable not being defined.
1:16 So I like to do it this way,
1:18 we had this warning, we can address that in a second but what we're going
1:21 to see will actually take this away anyway,
1:24 We're riding this out in a long hand four line type of thing here.
1:30 Sourcery says, you know what?
1:31 We could replace us with. Just an if expression instead of a statement,
1:35 we could just replace it like this.
1:38 'fire_modifier = 5' If breathes fire.
1:41 Self published fire else one and now we don't need that.
1:44 There's no conditional really. Fire modifiers always going to be defined.
1:49 There's a bunch of things like this that are pretty neat.
1:52 For example, let's look at another one that we can contrive here.
1:57 Now let's suppose I was going to go over here and create something like a list
2:02 of all the levels of the creatures.
2:04 So we could say levels equals this.
2:06 Now again, this is contrived.
2:08 So I'm not saying it really makes sense in the context of our game,
2:10 but just to show off some examples,
2:12 we could say 'for_c in creatures' ' levels.append C.level'.
2:19 All right, so that's all pretty good.
2:21 But notice now Sourcery not PyCharm is suggesting that we can do better.
2:26 All right, let me make a comment here so we can see it permanently if
2:32 we go over here and says you know what?
2:33 That could actually be a list comprehension,
2:36 convert the loop into a list comprehension boom,
2:39 there we go. So levels levels are.
2:42 Now just see that level four,
2:43 seeing creatures. So these are the higher order things that sorcery does that.
2:47 PyCharm doesn't kind of, the more Pythonic type of things,
2:52 let me show you one more conditional that I think is going to be pretty cool
2:56 So over here we have our statement like this,
3:01 what if we wanted to say is strong,
3:04 it's going to be base role if base role is not none, else,
3:15 not strong, something like that.
3:19 Okay, actually we'll just do the test like if based role,
3:23 so notice now we get this re factoring suggestion,
3:26 well then we got to print out now this says we can simplify the expression using
3:33 or so here we had an if condition if it's five,
3:39 if this case else one, but check this out,
3:41 we can just say is strong base roll or not strong,
3:44 so we have not just converting,
3:47 you know like a conditional over to an expression but even these non obvious simplifications like
3:55 it's either base role or is this other one?
3:56 You don't need this whole if expression so I personally like sorcery,
4:01 I do use it some of the time but it's not a super strong,
4:05 you must go and use it endorsement,
4:07 this is more awareness, like hey look there's these other tools and if you really
4:11 get into re factoring you can check them out, it will take you further.