How to Make Your Python Code More Modular and Reusable

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you're watching the code guy and in this one we're going to talk about how to make our code more reusable in the functions video we moved out this function from the guest the number program but we can go further than that and create reusable packages what is a package a package is something in Python that you can import into your programs for instance this is the random package and it has a function in it called randin so from random package import Rand in and we can create libraries just like this so how do we do it P charm makes it really really easy go to our project folder new python package and we're going to create a package called common and common tells us that this is a package that's going to be shared throughout our programs so it's created a folder here called common and underneath you've got a single file called uncore uncore init.py we could create this manually just by creating a directory and then creating a blank file within so now I want to move out this input function so I can use elsewhere how do I do that very very simple we're going to create a new file you could put it in in it but I strongly suggest against it and we're going to call it input validation and here we got our brand new file I'm just going to create a comment contains input validation functions that will just tell people what this package is all about and then in input validation what exactly are we going to do well we're going to we're going to go back to our guess the number and we're going to take this get numeric input and we're going to cut it out and we're going to paste it in here and I'm going to call it get numeric because it's already called input validation pycharm already saves this for us if you're another ID remember to save it and then we can actually close this so in our program we're still going to need the get numeric input function so how are we going to grab that well it's quite simple just like we grabbed from random here we're going to do from common dot input validation import get numeric and I'm going to get numeric as get numeric input and what that does is it basically says get the numeric function but give it the name get numeric input so now when we run this program it should still work as if get numeric input was written in here enter your guess 50 was too high 23 was the right number so the program works exactly as it did before but now we don't have to worry about that and if I want to write a new program new python file I'm going to call it test and we're going to do our from common dot input validation import get numeric and then in here we can have a function that says get numeric into a variable called test and we just going to run this and you see it detects a number so it finished straight away if we run it again and type into the crap it'll ask us to enter again there is a problem with this and that is get numeric may not be a guess what we need to do is have the ability to pass in the prompt we want to use so I'm going to open up our input validation again so to make this function more reusable we want to be able to change this and also because it's a number we might have a particular range we're looking at so that is what we're going to add to this function so in our get numeric I'm going to put prompt text we also want range start and range end okay so how do we start changing this program first this thing we need to change it to prompt text so if it is a digit we're then going to check if it's in the range if the input num is greater than or equal to range start and input num is less than or equal to range end and python actually has a nice way of handling this and the way we do that is we can move input n between here so range star is less than or equal to input number which is less than or equal to range and that will do exactly the same function although it's shorter let's set that to minus one because someone might actually want to enter zero okay so let us test this out close this again go back to our guess number get numeric input prompt text and we're going to say enter your guess range start is going to be zero and range end is going to be 100 because that's our maximum number okay so we don't really hand handle error message in here so I'm going to take that out but we might want to start and we would just add that as we're going through we've entered our enter the guess we've entered our start range we've entered our end range so let's test this out now if we stop this program and run it enter your guess and we are going to try crap doesn't work we are going to try1 doesn't work we're going to try minus 10 doesn't work and then we're going to try 50 and actually that should have been one or 100 but you get the point and actually I've broken something cuz this is no longer working oh I know what's wrong with this we actually need to check the input num is an INT cuz that's an INT and that is an INT if this number is a digit we're going to cast it to the number so that will now check it and if it's not in range we are going to set it back to minus one and let's actually enter some error messaging here print I'm going to do an F string and we're going to do input out of range and we're going to do range start and range end and I'm actually going to prefix this with not so if that is not in this range then we're going to set the input number to minus one and we're going to print out an error message that we're out of range let's add an else here and then we're going to put print input specified must be a number let's go back to our guess the number and run it so now we are going to enter a guess of crap 101 and let's try 50 guess was too high and the program is running normally now so here we took our function we added a load of validation and error messages to it and we added some parameters so that we can actually control how it works now so it's a lot more functional than it was before but the key thing is if I now go back to our test file and run this again those changes we made and if we run this now it is going to do exactly what we did over in the other program in this one I've shown you how to create reusable packages that you can then use throughout your programs if you have any questions or you found this video particularly useful then please leave a comment it'll help me out a lot in our next video which will be shown top left anytime now we are going to talk about how to create objects in Python which will take your coding to a whole new level and with that thanks for watching and I hope to catch you on the next one
Info
Channel: The Code Guy
Views: 15,206
Rating: undefined out of 5
Keywords: Python, Modularity, Code Reusability, Programming Best Practices, Python Packaging, Function Optimization, Clean Code, Python Modules, Python Functions, Code Maintenance, Python Development, Software Engineering, Coding Efficiency, python the code guy, the code guy, python beginners, python tutorial for beginners, python tutorial
Id: P7wlBTLYyrg
Channel Id: undefined
Length: 7min 1sec (421 seconds)
Published: Mon Apr 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.