Stop using Print or Logger to Debug your Python Code | by Satyam Kumar | Towards Data Science

Debugging code is an important but tiresome task for every developer. It is essential to perform code debugging when your code output is not as expected or throws an error. Debugging is the process of finding and fixing the errors in the program.
Syntactic errors and Semantic errors are two types of errors a developer faces in their program. Syntactic errors are caused due to mistyping of commands or code, indentation error, and are easily fixed by following the Python traceback instructions. Semantic errors result when the code output is not as expected which may be caused due to the wrong implementation of the algorithm.
Syntactic errors are easy to handle but sometimes it's a difficult and time-taking task to handle the semantic errors. The developer needs to go through the code snippet and fix the bug.
Usually, developers use print statements or add logs in between the code snippets for debugging. There are various open-sourced Python libraries for debugging. In this article, we will discuss one of such library Icecream, which can help in debugging your Python code without inserting a lot of print and log statements.
Icecream? Yes, it’s the name of a Python package that helps the developers to debug their Python code. Icecream helps the developers to get rid of writing multiple prints and log statements to fix the bug while debugging. Instead, they can use ic class from the Icecream package.
Icecream package can be installed from PyPl using
pip install icecream
and import the ic class for debugging from icecream import ic
Icecream can print both expressions/variable names and their values. The output format can also be formatted and can also include program contexts such as filename, line number, and parent function.
ic() function from the Icecream package inspects itself and prints both its own arguments and the values of those arguments.
Just give ic() a variable, expression, or function, and you're done.
Often developers use loggers or print statements to know the parts of code is being executed and to know the function flow. Using ic() function in every function call or loop can be used to track the execution of the code while debugging.
To inspect the execution of the Python function, pass the custom function to ic() as a parameter. ic() returns the arguments of the custom function, hence can be easily implemented in the pre-existing code.
One can easily track the execution of the function by observing the print function of the ic() function.
One can configure and customize the output of the ic() function. Using ic.configureOutput() the function, one can add a prefix, change the output function, customize the arguments of the output statement, and include or exclude program contexts such as filename, line number, and parent function.
def unixTimestamp():return '%i |> ' % int(time.time())# add unix time stamp as prefixic.configureOutput(prefix=unixTimestamp)
After you are completed with code debugging, you can either remove all the ic() function calls or just disable the output using ic.disable() function. It can be later re-enabled using ic.enable() function.
Icecream package can be used instead of writing multiple prints or log statements for code debugging. The data structures of the output format are pretty printed including expressions/variable names and their values and other program contexts. Writing print statements or loggers to output all this information while debugging is a tedious task, but ice cream provides all these in just a few lines of code implementation.
Along with Python, the implementation of the Icecream package is available in 12 programming languages along with C++, Java, Ruby, R, Go, etc.
[1] Icecream GitHub: https://github.com/gruns/icecream
Thank You for Reading