Debugging 101: Replace print() with icecream ic()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back in this video today we're going to learn about a python package called ice cream which allows us to do easy and professional debugging and logging so let us get right into it [Music] alright so oftentimes when we program in Python things don't work the way we expect them to work we Implement a function and the function does not deliver the results that we expect from it at least not every time and we don't really know why this is the case so for example we might have some function here my function take some parameters ABC and then we have for example some Loop in here right in the beginning then we have some if statements some condition making stuff up right now then something happens here otherwise we have maybe another condition and then we have here's something that we do and then maybe we have an else Branch with another loop or something whatever so we have some complicated function with conditions with loops and things that can happen in this function that are not too predictable and of course the proper way to debug this function is to use your debugger in the integrated development environment or in your code Editor to set breakpoints for example here and here and to check what the values are to see okay a is this right now B is this right now but oftentimes this is too tedious and we don't really want to go into debugging mode we don't really want to start a debugger we just want to execute the code as it is right now but we want to get some more information so what we do is we just use print statements we do stuff like print a at this point print B at this point and then print if a equals B to to see the behavior here what happens here so maybe when we print A and B they look the same but this doesn't return true and then we can examine this a little bit more by printing something like a type of a and then we can print type of B and we can examine it like that so debugging with print statements is a real thing even though it's less professional than using proper debugging people do it and I do it myself as well all the time now a good compromise between using print statements and using the proper debugger is to use a package called ice cream ice cream is basically like a print statement for debugging but with Superior features and we're going to look at this in this video today so what we're going to do first is we're going to open up terminal and we're going to install ice cream and then we're going to get rid of this sample code here we're going to import from ice cream IC the IC function from ice cream and now I'm going to show you a couple of examples to so that you can see why this is the quote-unquote better print for debugging so let's say as a simple example here I have some add function takes X and Y as parameters and returns just basic X Plus y now what I can do here is if I call for example at 10 and 20 I can get 30 here as a print statement but maybe I have multiple of those statements and maybe I have here 40 and I have here 60 and 70 and 30 and 10 and 10. and 20 and 20 and I want to know okay what is the output here but when I just look at the output I have to especially when the list is longer I have to see okay 100 belongs to which one it's the third so the third is this and of course in the case of addition it's simple I can just look at the values and add them manually but imagine this is a more complicated function and I cannot predict immediately by looking at the function call what the result is going to be then of course it would be nicer to have the function call to see okay what is actually being called and what is the result of this function call and this is exactly and of course I can do this with print by just adding by just using an F string and saying stuff like okay I'm calling add with a parameters 10 and 20 and this is the result and stuff like that but I can also just replace print by IC and then you will see there's way more convenient output format where I see IC here as a keyword and then add with the parameters that I called or that I put into the add function and the result of the function call and the great thing about this is and this doesn't work with a print statement I can also use the result to assign it to a variable so uh for example let's just use one of these lines here if I use a print statement and I say result equals and then I print the results I will get as a result none because the print function does not return anything the print function doesn't have a return value the add function however has a return value so what I would want to do here if I want to do it with print only is I would have to store the result of the addition in result then I could print the result or what I could do is I could just use IC I would do the addition get the output and also I would be able to get the result in the result variable so you can see I get the logging message but I also get the results stored into the result variable so whatever returns whatever is returned in here is also returned by IC it passes the return value so to say um all right so that's a very simple example another nice thing is this also works with dictionary access so let's say I have a simple dictionary here let's call it data and in data I have some maybe another data keyword here uh with one two three four five and then we have maybe labels in here and we have something like a b c d and e and then I want to print something like data data two or something like that now this is the output I get from print if I replace print by IC I get the code that I executed so I get data and then which key I accessed and which index and what the result is so this is just again giving me more information about what I did now it's not only about the axis it's also printing the data structure itself now I'm going to copy here something because it's just a larger dictionary it isn't something that is uh of any relevance here so that you have to understand it it's just a big dictionary and this thing here if you print it with an ordinary print statement what you get here is this doesn't look very very good and of course if it's larger it's going to look even more confusing if I here replace print by IC I will get a better view of the data first of all I have some colors I have gray for the brackets and I have uh what is this teal or something for the actual keys and values and you can see that this is just a better overview I have metadata users I have notation it looks just better so this is also something if you print a dictionary if you print a Json object it's better to print it with IC than with print also I can just print an IC statement anywhere in the code so let's say again I have some function and this function has Maybe a value that it takes and if the value is let's say if the value is uh even we're going to print something we're going to return true and else we're going to return false and I want to know okay where am I in the function so if I print here my function and 10 which is even I would just get the result and what I can do now in the function here is I can call let's see here an IC without any parameters without any content inside and you will see that this returns to me the line that was executed in the function and at which time point which means that now by running this I know this function call here produced or resulted in ic being called in line five so we entered this else Branch if branch so that is also a nice feature uh this this is just nice to see okay which condition uh or or are we entering the if Branch the LF Branch the else Branch or where am I in the code um yeah so what we can also do and this is nice we can also disable IC for a certain section of the code so let's say for example I call this function here with IC here and with IC here and then I change this to 11 for example and this is 12 then you will see okay I get 585 because this is even odd even um at the different time points here and what I can do now is I can say I see disable to no longer get the IC messages then I can copy this paste it down here and you will see I don't get any more messages even though I run the functions uh three more time or the function calls three more time times and if I say now enable and I copy this again and paste it down here then again I get more output so this is just nice if you have a section that you don't want to log anything for because you know that everything there is fine you don't need information about this section just disable IC for this part and then enable it again because you don't want to necessarily remove the IC values here you can just say Okay I want to keep them in a function but if I call it in this section here don't execute them so that's also a nice feature and then also in general we can just configure how ice cream works so we can say for example um let's do something more complicated let's say I have a function output to file and I get a text parameter here and what this function does is it says I open up a debug log.txt file in writing mode SF and then I say F right um or maybe let's do this in a pending mode uh write the text and also add the backslash n so this would be a function that just takes text and outputs it to uh to the file and what we can do now is we can say I see configure output and we can say first of all let's add a prefix I want to have not IC but debug for example debug and then pipe and a space and then I also want to have an output function and the output function is output to file so basically instead of just using the text and printing it onto the screen I'm taking the text of the IC function output and I'm feeding it into this function here which writes it into a file so I'm logging the output and I can also say something like include context equals true to get some more information and then I can call again this function here I see uh or actually let's let's add let's define the add function up here again like this and then I can just say I see at 10 and 20. and we're going to run this now you can see I don't get any output but I have this log file and here you see I have some context so which line and which part and then what happened and here you can also see the prefix so yeah this is how you do professional or sort of professional debugging better than just using the print statement in Python using ice cream so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you next video and bye
Info
Channel: NeuralNine
Views: 227,773
Rating: undefined out of 5
Keywords: python, debugging, icecream, print, python debugging, python icecream package, ic(), ic function, python ic(), icecream debugging
Id: JJ9zZ8cyaEk
Channel Id: undefined
Length: 12min 36sec (756 seconds)
Published: Thu Oct 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.