 again hi everyone welcome to the LPS session in this lecture we are going to continue to look at Python just as recap we covered many things with Python as you may recall we started with the basic data structures we went through some of the control structures we started talking about some of the advanced data structures like tools lists and then we went into the dictionary essentially which is the associated associative arrays then in the last lecture we covered new items like lambda functions it is like a function generator and then we went over some of the default functions that are provided with as a part of Python so I hope like I mean all those things are clear today we will we will start with actually another section basically this is we are going to talk about the exception handling so this is like when you have some exception generated inside code how do how does the program handle it how do we handle it things like that that is what we talk about so the exception in general is the recognized type of error and handling is what you do when that error happens so the general syntax of exception handling is this call called try very similar to a function but it is not a function it is a predefined function and then basically what we say is we say try the code you want to run except what are the exceptions so exception one and then if it is exception one so for that what is the block of the code then we do exception two and then what we do and then all the way up to the last exception is exception n and then we specify that as variable one so essentially like and this is the general syntax of the exception handling so now what this means is this code means is that it is an error occurs and if it is of exception type one which is here then we set the variable one it becomes an alias to the exception object so that is specified here and then this block of code starts executing exception one block executes otherwise python tries the exception types to all the way up to n until the exception is caught or else the program stops with an unhandled exception the as variable is an optional one and this will not work with the older Python so this is like three and above so let us look at a quick example so value error dot this should be py so here we say try I equal to int of snakes and then print the integer is something except value errors here we omitted that as variable print oops invalid value now there can be other exceptions one of the exception is this end of file exception end of file error is raised at the end of the file the index error happens if you use an invalid index for a string or a collection example if you try to get our V1 there is only one command line argument which is basically like our V0 then it generates an index error and then there is also another one is called type error which is if you are trying to compare two different types incompatible types like string versus float or something again that is the type error so essentially like I mean so general like I mean a value error end of file error and then index error and then the type error so these are the general exceptions so that you want to keep in mind now let us look at some of the Python modules so in this section we are going to talk about the basics of modules import and from import statements and then how do we change data inside the module we will also talk about reloading the modules then we will talk about the module packages then name and the main then finally how do we import as a statement so now let us look at the Python modules so couple of small things regarding the before we be going to the modules I want to also highlight some of the other things about the exceptions so we talked about some of the things basically so the other ones essentially you can also have standard error which is basically the which can be like any all the built-in exceptions except the top iteration generator exit keyboard interrupt things like then our system exit they are all the standard error and then we can also get an arithmetic error which is essentially like I mean various types of arithmetic operations which which can be generated from the arithmetic operation like overflow error zero-division error or floating point error and then there is also buffer error which is essentially like I mean and the buffer related operations cannot be performed you can get a buffer error and then there is also lookup error which is the index error is one of them essentially or even the key error is also the other one which is only classified under lookup error and then the things like Ior error even this end-of-file error these are all under the environment error which is concerning with outside Python system so Ior OS error can also be with an environmental and there are some assertion errors basically then an assert statement attribute errors then an attribute reference or attribute assignment we also have talked about the floating point error basically when the floating point operation fails and then there is the generator exit engine so all these things are comes under the exception class of the in Python so they they are basically like basically it is a base class that is for all the exceptions so now let us look at the modules Python modules what is a module first of all each file in a in Python is considered a module so this module actually like I mean we saw it in Perl as well as actually in particular we did not see that much but in Perl we saw the modules essentially so everything within the file is encapsulated within a namespace which is basically the name of the file okay so to access code in another module another file we need to import that file and then access the functions of the data of that module we can do it by prefixing with the name of the module followed by a period but to import a module we just use the the function called import and with argument as so one thing you notice basically there is no fine suffix for this particular you can also import use a defined module or some standard modules like sys and random so in this case like sys like a standard module but you can also like to use a different modules any Python program needs one top level file which imports any other needed modules so the way it is organized as a top level file which imports other files so in the Python standard library there are over 200 modules and so essentially like I mean you can go to Python org and then you can consider the console the Python library reference manual and for all these different modules in the standard library so now what does the import do essentially so the import statement does three things it finds the file for the given module it compiles into a bytecode and runs the module code to build any objects basically the top level code and then variable initialization the module name is only a simple name Python also uses a module search path to find it it will search the directory of the top-level module or the top level file directory seems the inside the environmental variable called the Python path and then the standard directories and then finally the directories list listed in any of the path files that is in your directory and then here it is basically in one directory per line in a plain text file essentially that's how we want to specify the .pth file the path can be listed by printing sys.path so if you say print sys.path it will list the path so here is an example for printing the command line arguments and argv.par so here we import the sys and then the command options are sys.argv and then basically like for I initialize the variable I and then for command in command options essentially which is all these phones we just print the argument I and then the command and then we just increment so it basically prints once we specify this essentially like arg0 is print argv.par and then test 1 and test 2 now there is another module called random so random also can be imported so we import random and then we say basically random.rand integer between 1 and 100 so this is essentially it is a random number generator between 1 and 0 I mean sorry 1 and 100 and then we can print that and then you can also like specify like a random or choice and then we can pass a list and then we can make it to print the dinner and then it randomly fix one and so here we notice basically like this random function and we also say like ran in an int to make sure that this output of this one is an integer from these values and here we just specify simply like choice and then basically like the one such strings and then essentially like I mean that is printed out so now let us look at the difference between import and from import basically so from this whole thing when we say import import brings in a whole new module essentially the whole module essentially and we need to qualify the names by module name so that is using this is argue but import from copies the names from the module into a current into the current module so no need to qualify then basically we do not need to actually qualify with these additional names so it is actually brought into the current module itself so and notice that actually these are full copies basically not links so essentially you can change and basically like it is not going to override anything changes to the original ones so you have a your own copy and then your copy will be different from the one inside the module itself so here an example from module X we import junk and then we directly call junk we do not call as module X dot junk anymore so now when we specify this command the junk actually now gets transferred and then basically it is copied and kept it as part of your module and not part of the module X in and then you can say basically like from module X import star that gets all the top level mod names from modules now the change changing the data inside module so you want to reassign a new value to fetch name from module from a module which does not change the module but changing the mutable variable from the module so essentially like I mean the module X import XY and we say X is equal to 30 it doesn't change X in the module so this is the something that we talked about earlier but if you put as Y 0 equal to 1 that actually changes Y 0 in module X because now it has no bearing of this 0 inside this particular your own module and it needs to go back to the module X to find out what is Y 0 and then change that so this is similar to like more exactly like the functions so to actually change a global name in another file we could use import without the from and qualify the variable but again this breaks the data encapsulation because now we say like module X dot X is 30 and then any kind of object encapsulation that the module provides is compelling now how do we reload modules the model use top level code is only run the first time when the model gets imported subsequent imports do nothing so the reload function force the cell to reload and rerun of that module so you can use if there is a module changes while the program Python is run so essentially like I mean if you change the variable and you have to re-import the module then at the point you can use the reload and reload is passed an existing module object for example reload module X and then the module X must be must have been previously imported so it won't you cannot do an import with a reload command and then the reload changes module object in place basically like whatever that was imported it changes that essentially and it does not affect prior from import statement so once they are copied into your own particular module those will not change even with the reload only the import the regular import will be the only one that will change and they still point to the old objects if you do another from import of course that will change basically after so now let's talk about the module packages so when using the import we can give the directory path instead of a simple name so the directory of Python code is known as the package so for example here import directory 1 directory 2 and then module so or we can also say like from directory 1 directory 2 module import X so essentially what that means is basically look for the file directory 1 slash directory 2 then module dot path and one thing is directory 1 must be within one of the directories in the Python path so the Python path is now important similar to what we saw in regular import modules and the other thing is also like directly one and directly two they should be simple names not the platform specific syntax that is C column slash that flash now underscore underscore init underscore underscore dot file these are some of the package files so when we use the Python packages basically the directory path syntax for the imports each directory in the path needs to have this underscore underscore init underscore underscore dot file this files could be just blank basically it is nothing basically or if it is not blank the file contains the Python code the first time Python imports through the directory it will run the code in the underscore underscore init underscore underscore dot Python so in the directory one directory 2 module examples the namespace directory 1 directory 2 now exists which contains all the names assigned by the directory 2 underscore underscore init underscore underscore Python the file can contain an underscore all underscore underscore list which specifies what it exported by default when the directories imported bit the from statement so now this actually uses another concept of the data encapsulation so by default names beginning with an underscore will not be copied in an import statement they can still be changed if access directly so that is no issues alternatively one can still one can list the names to be copied on import by assigning them to a list called underscore underscore all underscore underscore so for example here we specify like underscore underscore all underscore underscore and this list x1 y1 and v1 and to mean that basically import only these this list is read only only like we need to import these these x1 y1 and v1 and this list is a read only and when it is using the from star syntax so that is the one the from import import from syntax that we saw so this is this list is read only when you use this from star so that when you use the import from and then star it only imports these even though it may have many other so now the same thing basically like underscore name underscore and underscore main underscore so when a file is run as a top-level program its underscore name underscore is set to underscore main underscore when it starts so this is how so this is the concept which is very similar in like C language in C always the execution starts with this function called main and it only executes that and then any other functions are under that similarly in Python basically the underscore underscore name underscore is set to main when it starts so that it runs that as the starting point but if a file is imported the underscore name underscore is set to the name of the module as the importer sees it and can use this to package a module as a library but allow it to run as a standalone off by checking if the name the underscore name underscore is the same as underscore main underscore and do whatever basically that will run in the standalone mode so now the the next one is this import as we can rename a module by using import long module name as short module where the short name is just an alias for the original model we saw this thing in the very first slide when we did the example for importing where we said the import or actually like in the exception handling so try and then we say basically exception as variable one so there that long name is assigned to the short name essentially as using the app so the same thing is applicable even to the import statement so import long module name as short name and then the short name is the alias for the moment so when we start using the syntax basically like I mean there is no chart name thereafter and we don't have to specify the long name we can also use the same as syntax for the from basically so from module import long name as the short name that's another thing that we can do yeah basically here X or whatever which module and we saw this thing basically on the reload the reload may not affect the from imports essentially so when we do from copies so basically the from actually copies names the and does not retain the link back those so that's why if you do a reload you won't get the new one for the those modules that are imported as from so when the reload is run it changes the module but not any copies that were made on the original from the module import from module import X statement if this is this becomes a problem then we need to import the entire module and then just use the name qualification just like module dot xxx xxx that kind of funny instead of this from statement so now we come to the file form section here we are going to just talk about some basic file operations so quick commands how do we open a file the command is open and then we just specify the file name or more and whether the file name is just a Python string the mode is also a string basically and then it is string R for reading the W for writing and a for happening so the basic operation basically out file open out dot that with write option in file open input dot there and then with read option simple stuff we saw this kind of thing both in tickle and pulse already and then some of the basic operations so once you open the file we specify input dot read and then that reads the whole file into a string this also we saw like in the previous examples and then we can also read by number of bytes essentially like so that is again read and the end specifies the number of bytes so it reads so many bytes from the file and keeps it and read line is to read the next line and we can also specify read lines to read the files into a list of strings and then similarly for writing some output basically we use the output dot write and then if you specify the string a and then the string a is written into the form and then we can say like write lines a to write a list of strings into the a and then we use the close function output or close to close a file now how do we redirect the standard out so the print statement normally go to the standard out the standard output is basically the screen and then if we can actually redirect the STD out into file so here we basically the import the sys module and then we specify sys.standard out is this particular file called output dot text and we open it with the right option now when we specify the print message it will not show up in your screen but it directly goes into this output or text alternatively we can also print something basically to a file using the print double arrow and then we specify the file and then the message and then for this the only condition is that the log file needs to be open so here are some examples of file parsing this file parsing like I mean we saw this curl a lot of times so here how do we do it in Python we specify the in file basically like what is the in file with what kind of option and then we start reading the lines this is so the lines contain and list of the strings of all the lines in the in the file and then we can just there after we read it we can just close it now to print the line essentially like we use the for statement and then basically print line that prints one line at a time and then there is also like a shortcut syntax to avoid the read line calls basically the line by line so we open the file and then basically like maybe so here once we specify the line by line essentially like just read the lines one by one so here we can directly read this file and then print those lines so just to recap today we saw three major things basically the first one is on the exception handling how do we handle exceptions and we cover many different types of exception and in general the syntax is like this try and catch essentially the try syntax is for the exception handling then we also covered the the various modules module commands so we went through the basics of modules how to import modules how to use the from import commands then we also talked about the various types of modules like the sys module and the random module and then we went through like how do we reload modules what are the effects basically and then how do we define packages and then the what are the some rules that govern the package specification and then how do we actually change what gets imported and what gets run and then we went to the file operations basically how do we open the file how do we close the file how do we open it as read only how to write and then how do we actually do operations essentially how do we read from a file and how do we write out into a file and then also like how do we redirect some of the standard outs files and then we also went through the file parsing essentially so that's pretty much it for today we will pick up this stuff from this point in the next class thank you very much