Python Part 12a - Functions Office

Python Part 12a - Functions Office So welcome to this wisel tutorial on writing functions in python it's part one of two the second part will look at advanced features of functions so here's what you'll learn during this tutorial we'll begin by looking at designing functions how to work out what goes into them how to work out what comes out of them and the actual syntax of python functions we'll then look at how you can write them using the example of presenting the runtime of a film in a nice easy to read way and then we'll look at how you can check data types to make sure that the thing.

You pass into a function is the right sort of data and to reduce the chance of runtime errors we'll then do two case studies or two examples one i'm reading in a text file and the second one of turning a release date of a film into how its age or how long ago was since it was made we'll then look at how you can store functions in separate modules to create libraries of useful code before bringing everything together with a full worked example using something called modular programming or modular code i'll explain.

What that is and why it's such a good thing to aspire to and finally in this tutorial we'll look at doc strings a way to document functions so you can see what arguments you pass to them etc at any time you'll be able to click on the link at the top right of the screen to download any files to do with this tutorial and also to do any exercises but at about this time in the tutorial i normally vanish and this times no exception so what i'm going to do is hand you over to my capable assistant sven who'll guide you through the rest of this tutorial.

So let's get started the example function i want to start with will take the duration of a film in minutes and spit it out in hours and minutes as text so for example no time to die lasted 163 minutes a very long 163 minutes in my opinion if you run this program you can see that it tells me it actually lasted 2 hours and 43 minutes so to get this to work i'm going to get you to create or we're going to create together a function called get duration.

Which will turn an integer into a string of text so let's see how you might design this function let's look at the function we're going to write for example it would take no time to die 163 minutes and it will spit that out as 2 hours 43 minutes and it will make adjustments for films which last for example two hours in one minute so we'll make that singular and for films like dances with wills which last an exact number of hours it.

Won't express a minute at all but all that's to come later so here's how you might design a function like this you can think of it as like a sausage machine or like a mincer i remember we used to have one of these in my family house when i was young long time ago you put in one or more inputs so for example for our example we'll input the time in minutes and whenever you have an input you need to consider two things about it what you're going to call it and what data.

Python Part 12a - Functions

Type it has so i'm going to call my argument minutes it seems quite a sensible name and it's going to contain an integer or an int if you think about it the number of minutes is a whole number now you could have more arguments you could have more bits of information going into your mincer but our example doesn't so we'll go straight to the outputs now coming out of our mensa is this it's a time expressed in hours and minutes and again you can do two things you can give the function a name so we'll call.

Ours get duration and you can think what sort of information is coming out of it so in our case it's a string of text and that's how you can design any function so let's look at our specific one so the first thing to do when you're designing a function is to include the word def and def means define what follows then is the name of the function which in our case will be get duration and then in brackets a list of all the arguments the function needs in order to work in our case there'll be one the minutes and finally you say what it returns in.

Our case a string of text indented within the function then will be all the statements it needs these can be either one or two lines or sometimes a couple of hundreds but at the end of it you have to return something so that's a general definition of the syntax of a function for our specific one it would look like this so we'll define the function called get duration we'll define the argument to be passed into it as a minutes and we'll say what it returns a string.

Of text the code to actually turn an integer number minutes into a readable string of text will follow later but i think that's enough theory let's actually write the function so to create my first function i've created a file called get duration function dot py and within this i'm going to define a function so i'll call it get duration and then in brackets i can specify or.

List out the arguments i'm going to pass to it there's only one which i'm going to call minutes and for the moment i won't put a data type we'll come back to that in a second i can then put the data type this will return and to do that i put a dash followed by a greater than sign and then i can put any valid data type in python and finally put a colon and then follow the lines of my function i haven't specified the data type of minutes and what i want to do is show you why you might want to do that if i now type minutes dot there's no.

Intellisense auto completion appears and that's because python has absolutely no idea what i'm going to pass to this function so i can help myself by putting colon int and what that will do is assign a data type to it as we'll see in a bit it doesn't actually force you to use past an integer but it does at least prompt you to think about it and now if i type minutes dot it comes out with functions or methods to do with an integer so that's a big help.

    So what i can now do is write the rest of my function now my intention of this isn't to show you detailed coding so i'm

    Actually going to get i'm going to paste in my code at this point and you of course can get the file from the list of files attached to this tutorial click at the link at the top right of the screen and you can copy in this text too i've also added three lines at the end to test it out so let's just briefly see if it works and then i'll explain what it's doing so if i run that program you can see it's working which is good news and as to what it's doing it's taking.

    The number of minutes let's assume it's no time to die at once 163 minutes and taking the remainder of that when divided by 60 so that will give me 43 minutes there in the mins variable it then takes the original 163 subtracts from that the 43 remainder to give me 120 and divides that by 60 to give me two hours of the film then it has a message called our text which basically says if the hours is more greater or equal to two then it will put a plural in otherwise we'll put a singular i don't.

    Think we've seen this construct before it's a compact way of saying if one thing is true do this otherwise do another sometimes called a ternary operator it then does the same thing to get the minutes in text but for the minutes there are three possibilities either there's an exact number of hours there's one minute more than the exact number of hours or any other possibility and so it just gets a text string to show the number of minutes and finally it joins all that up together and returns it and when you come to run it it tests it out with three different films with.

    Durations of 163 121 and 180 minutes and gives me this output so that's my function what i want to do now is to look at this word int and to see what it means probably not what you think what it helps you to do is to use intellisense within your function we've seen that already so you can type a dot in and it will only come up with things to do with integers it also acts as a reminder to you that when you're using minutes within this.

    Function you can assume that it holds an integer and amount but what it doesn't do is restrict you to passing an integer so it's quite possible for me to go back to one of my functions let's choose this one at the bottom and pass in a string of text so i'm going to pass 163 as a text not an integer and it will crash my function because the first thing we'll try to do is divide it by 60 i'll give the remainder in divided by 60 and that's not possible for a string of text so when i run it.

    It crashes on line 5 of my function get around this there's no way within a function of saying you have to pass in the correct type and this is where python's limitations of not being a strongly typed language for me become most apparent it has advantages but it does have disadvantages too so the only way i can think around this quest problem is to add extra validation to the top of every function to check whether you've got the correct data type being passed in.

    So in this case i'm going to say if and i'm going to use a new function which i don't think we've seen before called is instance and what that will do is check the type of something is correct so the argument i'm checking is the minutes argument and i'm checking if it's an int except i actually want to do the opposite i want to say if it's not an int so i'm going to put the word not in front of that and if it's not an integer then there's no point carrying on i've got a problem and i'll return no such number and i'll put it in capitals just to make.

    It really stand out so now if i run that program you can see it says no such number instead how you deal with a return value return an error return from your function is up to you this isn't a very elegant solution but at least it shows me i've got a problem so i think there's two solutions to this problem one is to solve it by adding additional error trapping at the top of every function that you create to make sure that you're passing the information of the correct data type and the second way of solving it is just.

    Not to not to worry about it and just make sure that when you call your function you pass in information of the correct data type which solution will suit you depends on how big a company you work for how large the system is and whether you're working a team project for most things at wise owl i have to be honest and say i wouldn't bother with it with this extra validation i would just make sure i pass an information of the correct type all the time but the choice is yours.

    So for the first case study on writing a function what we're going to do is read down the text file and read in the lines

    Of films you've got attached to this tutorial 5 files each a csv so if i choose one at random like animation.csv and open it you can see that it contains a list of various animation films and the other four files are similar with different categories or different genres so what we're going to do is read in these lines of text in to a list so to do that we need to go back to our.

    Minsa analogy my minster analogy the arguments being passed into this in this case are twofold firstly we'll pass in the file path and we'll do that to make this generic so we can use it for any csv files in any folder and that we will call file path and we'll make a string argument the second thing we'll pass in is the file name which we'll call file name and will also be a string of text and then coming out of the mincer will.

    Be a single bit of information which is a list of all the lines in the file we'll call the function film underscore lines and the thing being passed out of it will be of data type list so now let's write the function so to do this i've created a file called read csv file dot py and what i'm going to do is create my new function so i will call it file lines did i call it film lines i can't remember now and then in brackets i'll specify the.

    Two bits of information i'm going to pass to it which are the file path which is going to be whoops which is going to be a string and the file name which is also going to be a string and then coming out of this will be a list so functions really can return any type of data you like at all so what i now want to do within my file is i need to open the file and to do this is going to be just the same as it would be outside a function so i can say with.

    Open i'll put my file path and then concatenate that together with my file name i need to be careful about where the backslashes are but i'll come to that in a bit and i'll call that film's file so having opened the file what i now want to do is read down the lines of text so i can do that by get all the lines so to do that i will create a variable which i'll call lines.

    I'll take my films file i'll read in it in it's in in its entirety i'll split the lines up and then i'll pick out everything from the first line onwards so i've done this before it's just a way of avoiding getting the csv titles in every single output from this function and that's all i need to do so at the end of my function i can just return the set of lines i've just created.

    I actually didn't need my variable i could have just put the word return there and simplified it so having done that let's now see if this works so i'm going to test my function so to do this i need to call my function so i'm going to say for line in and rather than putting a list here i'm going to put a function which returns a list so the two bits of information will return i'll firstly add the file path i've got this somewhere in my clipboard down here although not necessarily i.

    Think it's there then the second thing is going to be my file name and my file name is going to be let's go for animations.csv since that's the one i was just looking at you'll notice i've ended my file pass path with a backslash to make sure that when i concatenate the file path and the file name together i still get a valid uh file name so for each line in that list of files what i now need to do is just print out.

    The line i guess to prove it works so what i'll do is just print that line out very simple so let's now try running this if i press alt ctrl n to run it you can see i get the lines of the animation file and the beauty of this is of course if i change animation to something else let's go through musicals instead and run that i get the musicals instead except i think they're actually spelled musical they were so now i've got a function which will.

    Read in any csv file and spit out all but the first line so for my second case study for function what we're going to do is work out how old each film is so we're looking at the secret life of pets which apparently was released on the 8th of july 2016 which would make it at the time of broadcasting or the time of making this video 5 years old so what i want to do is take that string of text and turn it into a number so let's have a look at our mensa so the minster will take in one argument.

    Which will be the date as an eight character string in that format so we'll call that argument ref date and it'll be of data type string the function will spit out the other end one bit of information as all functions do which will be the years since this date as a whole number that will we'll call the function get underscore age and it will return an integer so let's now write this function i've created a file called getfilmage.py.

    And within this the first thing i'm going to do is from the daytime module i'm going to import the daytime function because i'll be working with dates i'll then create my function so df to define a function i'll call it get underscore age as promised and it will take in the reference date except this will actually be as a string and it'll be up to my function to make sure it's a date and what it will do is return an integer the number of years specifically.

    So to do this i'm going to create a variable to hold the the date that's a date so i'll call this this date and what i'll do is take the date time uh modules stript strp time function which never seems to me to have a particularly good name and what this will do is take a string of text in this case the reference date i passed in and pass it.

    I make sense of it that's p-a-r-s-e pass and in this case the code i need to use is percent little d space percent little m space percent big y except they're not spaces they're forward slashes the little characters mean two characters for the day two characters of the month and four characters for the year so that should give me a date i hope and all i now need to do is find the number of days.

    Between uh that date and today's date so i'll put get agent years so to do this i can return the difference between today which is datetime.today oops i'll take the years for that so that'll be at the time of speaking 2021 and from that i'll subtract my reference date or this dates year and that should give me a whole number subject to the usual qualification that.

    If i pass in rubbish in the reference date it can't possibly work so i need to make sure when i'm calling my function i pass in something sensible so now let's test that out so let's just print out the age of any date i like so let's choose the 9th of the 8th 2012 completely random and if i run this you can see it says that's 9 years old which seems about right to me.

    So looking at the function we wrote called get underscore age what i want to do is make this more useful and more generic by storing it not within the same module as where it's been consumed but in a separate module so to do that what i'm going to do is copy it and i think i'll probably need to copy the date time module import too so i'll copy that to the clipboard and i've created a separate file called wall underscore functions dot py it makes life easier if your module names you're.

    Going to import don't include spaces so i'll paste that in there and then i'll do the same thing with the other functions i wrote so i wrote one called film lines and i'll copy that in make sure that i get my indentation correct and i also wrote one called get duration function a slightly longer one that one and i'll copy that in now i've got a separate module holding the three functions and what i can do is import references to that module when i.

    Want to use any of these functions so let's do that so what i'll do is i'll just save that file and i'll close down all the other two so what i'll then do is reference this or use this module in a new file i've created called complete example.py it's not quite yet a complete example but it will be so i've just got in my clipboard there what i want to do i was printing out the results of calling the function get age with a particular date passed to it and not surprisingly that gives me an error because get age no longer exists.

    So what i need to do is i need to make sure that i've imported that module above and you can do this the same way as with any other python module so these are the only difference here is that you wrote the code so you can say import and then the name of the module which was called wall underscore functions and you could if you like give it an alias i'm going to i'm going to call it wall what is our pet name for wisel training by the way originally came from a misunderstanding of winnie the pooh now you can see it's underlined that but.

    I'm not convinced it's a problem if i now go back to my function i need to proceed the name of the function with the name of the module is coming from and when i put that in and try running this you'll see it's actually working perfectly it's giving me the age so why is it underlined the word while underscore functions in red i don't know the answer to that question but i do know how to resolve it what you can do is put a comment after this saying type colon ignore it's something to do with the pylons.

    Linter the thing which checks your code for errors and that removes that irritating warning message and again if i run it you can see it work perfectly so what we're now going to do is bring everything together to write a single program which will loop over all the csv files loop over the lines within them and print out information on each film so i'm just going to comment out that line of code by pressing ctrl forward slash.

    And then we'll import the glob module because i want to loop over files this is an extra to my code and then i can say for file in lob dot glob wonderful names these modules and functions have and then i can paste in the path i've got in my clipboard and what that will do if i add star dot csv in there is automatically go through all the files.

    And check this is working i'm just going to print out the name of the file and you can see it's listing them out there what i now need to do is split that into the path and the file name because irritatingly that's what my function expects so i've actually made life a bit harder for myself so let's write a comment to this effect split so to do this i will create three variables the first one i'll call file path the second one i don't care about i'll call an underscore and this one i will call.

    Filename and i'll set that to take the file and use the r partition function to split it by the final backslash character so what that will do is divide each file name the file path will contain everything up to the backslash the underscore will contain the backslash itself which i'm not interested in and the file name will contain the actual file name something.csv so now what i can do is i can call my.

    Function to read in that file you remember the file the function returned a list of lines so i'm going to create a variable called lines and i'm going to call my function which is in the wall module and was called well if i can't remember what it was called i can go back to world functions and it was called film lines so i can copy that in it expects to be pathed two bits of.

    Information a file path and a file name and there's just one other complication i need to make sure that i just have a trailing backslash at the end of my file path which is why i've added that in because otherwise i think it would fail so that should now give me a list of lines what i can now do is loop over these lines so for line in lines and i'm now looking hopefully at a list.

    Of all the films at this point i could do with just reminding myself of what the bits of information are so let's quickly go and have a look if i go to any one of these files and have a look i'll right click and choose to open it in notepad and you can see that the headings are id title release date runtime and genre so i'll just copy that in and paste it as a reminder at the top of my comments so what i can now do is take this line and unpack it the line consists of a list of five.

    Different things so i'll create five variables which i might as well call exactly or pretty much exactly the same thing as the headings i'll set that to take the line and split it using a comma and so that will unpack it the line.split will create a list with five items in it and they'll be stored in these five variables so then what i can do is extract all the nice information from using my functions so let's start with getting get a nice runtime.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=QI0hWnT_dkw
Previous Post Next Post