Sourcery is an INCREDIBLE AI refactoring tool for Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I've talked about many tools that can help you with your python code on this channel I talked about black which is a code formatter I talked about my Pi which is a static type Checker I've talked about rough uh which is a you know just a general purpose linter and today I'm talking about sorcery which is an AI powered code refactoring tool um which is as cool as it sounds and it is actually really cool so there are a few ways you can use it and it actually works for more things than just python as well um I've been doing you know JavaScript work for work and it works with that too which is quite nice but I'm just going to be showing you to do with python specifically today so it works on the CLI and you can get extensions for it like in the in vs code which I'll be talking about at the end of the video but yeah in this video I'll be showing you what it can do and how to use it so before you do anything of anything we need to install it so you can do pip install uh sorcery now you only need to do this if you're going to be using it through the CIA lie if you're only going to be using extensions you don't need to do this um but I'm going to be showing you both of course and then what you need to do is you need to do sorcery login and you only need to do this once for a computer so whenever you you know install it into a new virtual environment you won't need to log in again but you do need an account with Source 3D but sorcery is available for free and as far as I can tell it's you know free forever and it should be perfectly fine for all personal projects and stuff even though they do have some paid plans people just use this to log in and then once we've logged in we can start using it so the code is just to put on the screen now and it launches the web browser so you'll see you have this code um and you have this code here if they match hit confirm and then it's going to ask me to sign in I've already signed up so I'm just going to quickly log into my account and there we go we're all set so as I said you only need to do that once and then if secure is just going to keep shouting at me it's been doing that all day for some reason uh but now that we've done that we never need to do it again which is cool and we can go back to visual studio code and we get some kind of you know next steps so we can run source3 in it to set up your repo with a config file and then we can run sort through the review so this is the command um to search for sorcery issues and you can also use it in a pre-commit hook and the CI as well if you want so let's do sorcery in it and then on the side we can see that this sorcery.yaml file is created now in here um you can generally just leave these as defaults but you can also specify a python version if you like so setting this is really useful if you're you know doing something with the python package index or if you're limited to what python version you can use for whatever reason you can set that here so for example you can set the 3.7 and then saw 3 won't suggest any refactoring as well on a later version that I'm just going to set it 3.11 because that's what I'm using in my virtual environment and you can pretty much leave the rest of this as is you can have a look around if you want um but I found the defaults work perfectly fine so this file here contains a series of examples of things that sorcery will be refactoring for us so it has an example of you know using um the plus operator for string concatenation instead of f strings uh it has an example of using tuples and lists on in statements which I learned recently because of this isn't actually the most efficient way to do it but we'll see that in a second we have an example of using a DOT update when you shouldn't be we have an example of using nested div statements when you shouldn't be and then we have a kind of a more complicated example down here so if I just duplicate that one I've actually already duplicated it if I move this to this side and then run um sorcery on here uh we need to I thought I'd already created one okay that was weird it already created one uh or maybe that one yeah that might be the one that already existed actually but it's all good so we know we can do sorcery review and then uh be demos demo two now we can run this and it will tell you how many issues it finds so in total it found 11 separate ones and some of these are kind of compound issues so if we look at this first one for example it's come up with two use f strings for concatenations um essentially referring to the two plus operators that are here so if you had multiple it would you know keep saying it but you can actually click on this and it will take you to a link uh it'll take it as a documentation of where you know you can figure out um like what it's actually trying to tell you to do so next example you can see you know name Plus Age whatever you can set it as that using F strings um and it does the same for all of them and it also tells you you can run the source review Double Dash fix and if you do Double Dash fix and it fixes everything for you now generally speaking it can fix pretty much everything as far as I can tell that I think there are only two things that I've seen that it can't actually fix which are low code quality and don't reassign variable because you need to you know create another variable name if I just do Source review Double Dash fix demos demo 2. and let that run and allow it to run through secure thank you uh we can see that it's actually changed all this code so now we can compare the two so on here you know use the plus operator here use the F strings and one slightly annoying thing about the F strings that I found is if you just do something like um this to have S1 plus S2 uh doing it like this is actually faster than using an F string however it will still tell you to use an F string which is a little bit annoying I can kind of see why it does it um I am tempted to flag it as an issue on the repository because it really shouldn't be suggesting it if it's just going to slow the code down but either way if it's more than two if it's more you know if it's not like this and using F strings it's quicker and I actually do have a benchmark that I created for this uh just to test it and just to show it off so if I run pi benches and then what was it F string bench we can see that using um in a three-part system the F strings is faster in a two-part system the plus syntax is faster uh so yeah it's a little bit annoying but you can live with it I suppose so this second one is actually something I didn't know about until sorcery told me is it sets are actually more performance when it comes to in operations than lists or tuples which as soon as it was pointed out to me it did make sense and that was the case I do also have another Benchmark let's shut it off so I think it's coal bench yeah so I tested in a variety of situations you can see um and this is all three elements it's the same collection you can see that sets are faster if the element does exist and they are twice as fast if the element doesn't which is actually very interesting and they're pretty much exactly twice as fast as well and having the pre-built uh doesn't really seem to change all that much um so it will suggest that which is quite nice the dictionary stuff if you use dot updates um it'll you know shout at you that's obviously not the fastest way of doing it and I also have a bench for that as well I did my research so we do banshees du bench you can see that using uh the square bracket update syntax is actually something like this was more than three times faster um so that's really nice interestingly if I were to get rid of this um if completely and just have this DDOT updates kind of on its own like that it would actually append this to uh the original disk so it would put this here which is actually kind of funny because obviously you know if there's no condition and that's always going to be there so the fourth thing down here uh it's hugely simplified there so it's converted this if x and if Y into If X and Y but because it's either returning true or false it's now using the ball operator um which it actually probably doesn't need to do uh thinking about it it can probably get away with just being X and Y uh come to think of it but if I go back to the terminal you can kind of see what line is it um it would be line 24. yeah so merge nested if Boolean if expression identity and reintroduce else so this reintroduced else is an interesting one I might as well talk about this here so to reintroduce else um anyone that you know likes writing pythonic code will know that you you know flatter is better and you shouldn't be reintroducing else's but the way that sorcery works is that it will suggest it if it can find other optimizations um so it sort of works on this strange almost like tiered system where something has to be in a certain way before it can it's very bizarre but it works so who am I to judge and then this last one down here we have this class demo and it's completely refactored this so it's the same idea where it's you know it's um it's done the same thing where you just have this you know if else expression but it's also reversed the if not instance to say that you return self.n equals other Dot N if instance else not implemented so it's kind of it swaps the if statement around and then it's converted it into an if expression or an if else expression rather than just having it like this so it can be a fairly major refactors as well which is quite nice so I'll show you how it works in the CLI I'm going to be showing you how it works in your editor so we don't need this demo 2 anymore we can go over to extensions and you can install the sorcery extension I imagine this exists for other things like pie charm as well install and then it might take a little while to boot up I remember it took yeah so the first time around it took I think about a minute and a half to like properly boot up and you will have to I've already had this installed and uninstalled it for the sake of the video but you may also have to do this sorcery login through the command process Ctrl shift p and sorcery login it will take you to login page where you can log in if you like and then where it finds areas to refactor it will underline them in yellow and then when you hover over them it will actually give you the refactoring it wants to do so you come down here quick fix and then use F string instead of string concatenation or you can skip it if you want to we can just do it like that and the same thing goes for down here so show you that if I come down here then it will show you new um you know bigger changes as well and then it will show you kind of each individual thing that it wants to do which is quite nice one other thing that I do want to show you with the dictionary while I'm here is that at the moment you know it's saying just to use the uh the brackets operator if you're doing if you're using python 3.9 or more or higher I should say if you don't say to hello world it will still complain but it will now say to use the uh the dict merge operator instead which is kind of cool and if I get rid of this completely it will now underline this line and say that it should just be oh no it's actually telling you to merge them with the merge operator now which is interesting I think if I get rid of this it will say yeah it will say to just you know declared addict as is um so there are many different ways that it can handle that but yeah that is a general introduction of how sorcery works it is actually really nice um I remember I ran it on one of my personal projects and it came up with exactly 100 issues uh which was fun so at some point I'm going to go through them all and and do a big refactoring but yeah this is actually a really cool little tool as I said you do have to have an account but it is free um which is really nice and yeah it should help out you know in the same way that black and white pie and all that do uh in helping you um you know create better code that is more maintainable but if you like this video then make sure to leave a like to let me know and maybe subscribe if you want to see more videos like this if you have any questions about what you've seen or ideas on future videos that I could do leave them Below in the comments I read every single one so your feedback would be greatly appreciated if you want to support this channel monetarily you can do this in one of two ways you can either become a patron or a member using the links and buttons below uh one pound a month on either and you can be on this screen like these people and I'll see you in the next video where we talk about something really cool I'll see you for that
Info
Channel: Carberra
Views: 7,627
Rating: undefined out of 5
Keywords: pyfhon, pytho, pytbon, pytjon, ptyhon, pytyon, ptthon, pyyhon, pythn, pythoh, pythpn, ython, pytgon, pyhon, pytohn, phthon, oython, pthon, pyghon, pythoj, pythno, pythkn, ypthon, pytuon, lython, pyrhon, pythom, pythob, puthon, pgthon, python, pyhton, pythln, pythin, pytnon, pyton
Id: YbYj6lJ4pXg
Channel Id: undefined
Length: 13min 32sec (812 seconds)
Published: Sat May 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.