icecream
2023-12-23 python dump icecream debuggingWhen working with python I always missed data structures dumping like those for perl shown in Data::Dump and Data::Printer posts. However recently I discovered nice module that does something similar and more.
The module is icecream and it can no only print the structure nicely, it can also show the source for the print. For example
from icecream import ic
json = {
'metadata': {
'version': 1.0,
'generated_at': '2023-12-21'
},
'posts': [
{
'id': 1,
'author': 'Roman',
'posts': [
{
'title': 'A title',
'contents': 'Contents of the file'
}
]
}
]
}
ic(json)
Gives you
ic| json: {'metadata': {'generated_at': '2023-12-21', 'version': 1.0},
'posts': [{'author': 'Roman',
'id': 1,
'posts': [{'contents': 'Contents of the file',
'title': 'A title'}]}]}
Note the name of printed variable. It can also report function calls
from icecream import ic
def plus333(i):
return i + 333
def multiply5(i):
return i * 5
ic(multiply5(plus333(123)))
d = {'key': {1: 'one'}}
ic(d['key'][1])
Prints
ic| multiply5(plus333(123)): 2280
ic| d['key'][1]: 'one'
This is very useful for the debugging, printing into logs and understanding of what is going on.