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

Talk Python's Mastodon Michael Kennedy's Mastodon