Modern Python Projects Transcripts
Chapter: Writing code
Lecture: Sourcery

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I want to mention one more tool that I found quite recently,
0:03 and I think it could be very useful,
0:05 especially for beginner python programmers. It's called sourcery and what it does,
0:10 it gives you suggestions how to re-factor your code.
0:13 You can install it in VSCode or PyCharm.
0:16 And as you write, python code sourcery will sometimes show you pop ups that your
0:21 code can be re-factored. Something to keep in mind is that sourcery is not
0:25 open source. There is a paid plan,
0:27 but for now, it only applies when you want to use sourcery as part of
0:31 your continuous integration pipeline on GitHub.
0:34 But the plugin for VSCode is free to use.
0:36 So, let me show you how it works in VSCode.
0:39 First open the extensions marketplace and search for sourcery and then click install.
0:45 Once it's installed, you have to follow the installation instructions from here,
0:49 so you will have to go to sourcery Page and create an account to get a
0:53 personal token. Let's click this link.
0:57 You'll have to create an account.
0:58 You can either use Google or GitHub.
1:00 I usually use GitHub and once you log in,
1:03 you will see your personal token.
1:05 I'm obviously not going to show you my personal token,
1:08 but let's pretend you create an account you logged in and you copied the token from
1:12 the next page. You go back to the VSCode and you open the
1:17 settings and search for sourcery. And you have to copy this personal token here.
1:25 Once you copy it, closed the settings and we are ready to go.
1:29 If you set up sourcery correctly, now we should see some hints about our code in
1:33 the places where it could be refactor.
1:35 However, with the current version of sourcery,
1:37 it doesn't seem to work outside of the workspace.
1:42 As you can see, I have this purple bar at the bottom of VSCode
1:45 which means that I'm not in the workspace.
1:48 I just opened a single file.
1:50 So let's try to open the workspace.
1:53 Let me reuse one of the previous ones that I used for other lessons.
1:57 Let's go with Django. It doesn't really matter.
2:01 So now I need to reopen my ugly3 file actually close this and here it
2:09 is. Those blues quickly underscores come from sourcery.
2:14 And here we can see the deep of how sourcery would re-factor our code.
2:18 In this case, we have two identical print statements in the If, so we can
2:22 take it outside of the if, so we can click the quick fix.
2:29 And it's re-factored for us.
2:34 In the second example, we are creating at list using a for loop.
2:37 But sourcery suggest that we using this comprehension and it will work the same.
2:43 We click yes, and we get a code that does exactly the same,
2:47 but with just one line. So sourcery is a pretty great tool,
2:51 and I really hope it will stay free to use with code editors like VSCode
2:54 And even though I have been writing Python for many years,
2:58 I still find this plugin very useful.
3:00 It can spot some re-factoring is that I don't easily see, so I can highly recommend it.