How to use configureOutput parameter in icecream module In Python
IceCream module also enables you to configure a custom prefix. In the below example, we are going to learn how to configure the output prefix as time using a function.
Python3
# Python program to configure # output prefix as time from datetime import datetime from icecream import ic import time ic( "gfg" ) ic.configureOutput(prefix = "GFG " ) ic( "gfg" ) def time_format(): now = datetime.now() return f '{now.strftime("%H:%M:%S")} --> ' ic.configureOutput(prefix = time_format) for i in range ( 1 , 6 ): time.sleep( 1 ) ic(i) |
Output:
$ python gfg.py ic| 'gfg' GFG 'gfg' 02:02:41 --> i: 1 02:02:42 --> i: 2 02:02:43 --> i: 3 02:02:44 --> i: 4 02:02:45 --> i: 5
Explanation: In the output, line 1 shows the default output. Line 2 has a prefix ‘GFG’ which was configured in the code. From the 3rd line onwards, we were able to configure output with prefix time. The time shows a delay of 1 second as time.sleep() delays it as shown in the code. Finally, remove all the ic() after the debugging is complete.
Debugging with ice cream in Python
Do you often use Python print() to debug your code? However, if you want to make a significant improvement, you could use IceCream which would make debugging faster, cleaner, and easier to read. ic(), which is short for IceCream prints both expressions/variable names and their values. ic() is faster to type than print(). The output is highlighted and printed in a structured format. If you want, you can include program context (filename, line number, and parent function) easily. You could effortlessly debug while solving competitive code problems and save a lot of time while debugging.