Ruff is the FUTURE of Python development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for the longest time code quality in Python has been an interesting question to answer there have been plenty of Tools around you know flake is a particularly popular one I've never really used flaker I tend to use the individual tools more um so you know the black eyesore Bandit separately um you know there's pilot as well there's all these choices and they all have their own issues from you know speed to convenience to whatever well there is a new tool that's been around for a little while um called rough and it's been written in Rust for those rust stations out there and it is blisteringly fast it is very easy to use it is insanely quick my work code base is 215 000 lines long and ruffle into the entire thing in less than a second it really is that fast um and today I'm going to be showing you how to do a basic setup of it how to install it how to get the Pi Project on Tunnel working and I'm also going to be showing you in a spirit of you know videos I tend to do on this channel showing you a few um codes kind of in action you know it works the same way as flake 8 when it comes with codes for those of you that haven't worked with flake before I will go more into detail about that but I'll probably also make a separate video kind of going through all the different codes as well um but I'm going to be covering some more of the Obscure ones you know there's the ones that everyone that's used flake air off before kind of knows about but I'm going to be going over some of the more useful obscure ones so it'll actually install rough all you need to do is run when my terminal decides to load lock up pip install rough just like that and it will install I don't think it has any dependencies no it doesn't and it's 0.0.252 at time of recording so it's by no means stable uh you know they're adding a lot of stuff um you know stuff keeps getting added and you know a few things will change here and there generally speaking I think it is relatively stable it's mainly bug fixes that tend to break anything if anything at all once that's installed we can start using it immediately so I'm going to create a new file just called test.pi and import random and then I'm going to run a rough on this because it will flag up an area you see I have the visual code extension installed let me actually show you that real quick while I'm here uh so there's this vs code extension by Charlie Marsh this is the one you want um and this essentially just you know loads rough into vs code really nice all you have to do is install it and it's there already it uses the project.tomall um in your directory but we'll be talking about those in a bit um I was talking about that because it's saying you know it's giving us the yellow underline if we scroll to the bottom here it's a random import but unused rough f401 and if we do rough check which is the command we use to check Dot um it will you know find this error f401 random imported but unused uh one really nice thing about uh rough is that you can have potentially fixable options and I've added this line just to kind of show this off a little bit better but if you're around rough check dot with uh Double Dash fix the things that you can fix it can't fix everything but the things that you can fix will be fixed so as you can see the the um the import has been removed completely it's no longer a problem which is actually a really nice thing to have because I don't think flake really has any of that capability and I'm pretty sure pilot doesn't either um one thing I'm less sure about with it is that you know you can do show fixes to actually show what it's going to do but it doesn't seem to do it all the time I'm not sure if it's just the specific plugins or whether it's rough itself um but you know the Double Dash fix kind of just does its own stuff automatically I'm not necessarily a huge fan of that but maybe I'll get used to it as time goes on I don't know so now we've gone over you know the bear basics of the installing and usage of rough we can talk about configuration so rough is very configurable as you would expect it to be it does have a default configuration and that default configuration looks I've messed that up massively but it looks something like like this ignore the fact I can't use Mac OS to save my life so it selects your E and F code automatically so what that means is that every code that starts with that will be selected so if you have you know your F I think this one was F40 something about f401 um you know you could have F4 in that as well and it starts with F4 but if you were to put F4 in here for example we would only select ones that start with F4 if you were to put F5 you know this error wouldn't be flagged which is how those codes work for those of you that have never used flak 8 before um you have your fixable things that basically just tries to fix everything it can how's your excludes it has your you know certain things uh to make rough more usable however we can Define our own uh setting once I actually figure out how on Earth to separate these two windows so all you need to do um to do that is create a Pi Project dot toml file and you can have your tool dot rough in here so everything under this header will be read by rough um as a configuration option so something you might particularly want to use are extend uh select and we'll come back to that in a second but essentially that extends the default selections so rough selects E and F by default if you want to use more it's recommended that you use extend select rather than just select as if you select you'll have to specify enf manually unless you don't want them in which case you're probably better off I don't know um you know have your ignore which you know works very much the same way so you know if we wanted to ignore this f401 error that we had before then we can put that in here and we will ignore just that error as I've seen before if you put you know F4 in here then it would now ignore all f400 to f499 errors uh so that's how that works I'm going to get rid of that for now because I don't want that to happen in our demos uh I'm also going to turn show fixes equals true again this is the thing that doesn't work 100 at the moment but it is nice to kind of see what it's going to fix when it can give you it and you also have a Target version so this isn't the version of python that you're using currently um by default this uses python 3.10 but this is basically if you need to create code compatible with earlier versions of python you can you know specify a different version here so for example my project that I've used this for originally needs to support python 3.7 so I can put python37 in there and this doesn't affect much This only affects I believe a single code uh but it is useful to have you know just in case I'm wrong and it actually affects more than that I'm not 100 sure for specific tools if they have options you can say do tool.rough.isort I think would be in and you can have your own settings here I don't have any examples of them at the moment but the one thing I did want to show you was particular codes now I'm only going to show you three I have a setup that has a lot of codes enabled um by default but I'm going to show you some obscure ones that you may not have heard of even in flake 8 that I think are actually really really useful and that is C4 um t c h and Sim uh and for those of you that is bugging I'm gonna make this alphabetical because that bugs me too uh it's a C4 is comprehensions so basically this will look um through your code and tell you if you can make your comprehensions any better Sim is code simplification so it will tell you if you can simplify your code and TCH is kind of a type checking thing um so if there's anything particularly you could do better with type checking that would tell you that I'm going to show you one example of one code from each um in our test top high file so if I were to say for the C4 if I were to say create a set from a generator um so if you do actually we don't need that do we at all so I for I in range 10. it will now flag that up and it will give us an error c401 unnecessary generator rewrite a set comprehension so that's told me oh I can rewrite it like this and this is giving me a different error unnecessary set comprehension rewrite using set okay this is giving us something where we don't need a comprehension in this case um but you know it can tell you whether or not you can simplify your comprehensions or as I say if you don't need your comprehensions at all there are plenty of codes for that and it's actually really nice because comprehensions can be kind of awkward and something to help you almost pause is actually really useful the common simplification uh the one example I know from that looks for nested if blocks that don't need to be nested if blocks um to say if we had you know n equals five and then x equals three if we were to do if oh if n is greater than five and then if you were to put in here if uh X is less than four uh Prince wear golden well again error specifically Sim 102 saying use a single if statement instead of nested if statements so now it will tell us that we can do this yeah we're [ __ ] type this instead and suddenly our code has been simplified it's now much more readable and we're not dealing with unnecessary nested things Uh current simplifications can do much further as well I believe it could probably do nested for Loops if you don't need you know multiple for Loops or whatever um but that's just one example of that and the other one TCH which is actually really nice so if we import uh typing and say if well you know let's do just a death Funk and if we import if we have a date time uh if we import date time like that and this is going to be a date time on date time and then it's going to return none for example and then we just print the date time dot date time as it is um then we won't get the error at the moment because it's telling us that we've uh are not using the import so I'm just going to do type type checking and now it should I don't know why this isn't slanging it up actually if I if I activated the wrong one yeah I'm an idiot I'm supposed to be printing DT here so now that we're not actually using the code uh we go up here and it's giving us an error say move standard Library import date time into a type checking block so what this means is that because we're only using it as a type paint annotation we can actually move this in here so we don't import it at runtime and that's you know as someone that uses these type checking blocks a lot that is actually really really useful um to have because it just means that you're not importing unnecessary things at runtime meaning you know your code can be more efficient it's probably not going to make a huge difference um but you know with python it is good to get every optimization you can get so those are just some examples of things you can do in rough it has plenty more it's not fully vacate complete it's not fully piling complete things are still being added but there are a lot of rules already in there and to see which ones you can use you can do a rough linter and these are all the ones that have been ported over so far so the app is particularly useful this is um so when you're well this is actually what the target version is used for primarily so you can use this to see if there are any upgrades you can make to your code so instead of doing you know dot update for dicts um you can use the merge operator which I believe is fast huh someone correct me if I'm wrong about that but I do believe it does something different under the hood Yeah by doc star which works for Doc strings yeah perfect naming which is naming conventions uh Macabre which is code complexity you also have a lot of flake 8 Stuffs the flick it prints um which confusingly is T20 uh which is a funny one you have login formats raising so you have all these things and you have pilots and you have rough specific rules as well down here you even have specialized ones for you know pandas and you know there's a numpy one up here somewhere there's a load of different rules you can activate um I actually have I'll show you my configuration very quickly these are the ones that I have activated for a product of mine or project of mine sorry as you can see there is a lot of things that I have active and it doesn't slow it down at all it's as fast to check all these things as it was when it was just checking enf and you can see I'm using extend select here not select so enf are still being caught here though I should probably activate W as well which had to face the main warnings and I have all these things down here um to you know ignore certain errors I will do another video at some point whether that'll be the next video I don't know uh covering all the individual rules you can do at the moment and giving examples of each um some of them I don't think are particularly useful so whether I cover those I don't know um but hopefully that should have given you enough reason and enough of an introduction to rough uh to really take it on board and implement it in your project because it's really not that difficult to add to your own projects at all which is really nice if you like this video at any point then consider liking to let me know and also subscribing so you don't miss out on any future videos like this if you have any you know comments or questions or you know things you want to see me do in future videos uh feel free to leave a comment in the comment section below I read them all so your feedback is greatly appreciated if you want to support your channel monetarily you can do so in one of two ways the first of which is to become a channel member using the join button below the second way is to become a patreon user Link in the description one pan a month I need to do can be on the screen of these people and I will see you in the next video for whatever we do next I don't know what that might be that might be these rough codes I might do something else in between uh we'll see so I'll see you for that
Info
Channel: Carberra
Views: 131,074
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: bcAqceZkZRQ
Channel Id: undefined
Length: 14min 24sec (864 seconds)
Published: Mon Feb 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.