Skip to content

Latest commit

 

History

History
86 lines (66 loc) · 1.33 KB

debugging-with-icrecream.md

File metadata and controls

86 lines (66 loc) · 1.33 KB

Using icecream for Debugging

Install

python3 -m venv venv
source venv/bin/activate
pip install icecream

Getting Started

import sys
from icecream import ic

def yell(x: str) -> str:
    x = x.upper()
    ic(x)
    return x + '!!!'

if __name__ == '__main__':
    result = ic(yell(ic(sys.argv[1])))
    ic(result)

Output:

ic| sys.argv[1]: 'foo'
ic| x: 'FOO'
ic| yell(ic(sys.argv[1])): 'FOO!!!'
ic| result: 'FOO!!!'

Adding More Contextt

import sys
from icecream import ic
ic.configureOutput(includeContext=True)

def yell(x: str) -> str:
    x = x.upper()
    ic(x)
    return x + '!!!'

if __name__ == '__main__':
    result = ic(yell(ic(sys.argv[1])))
    ic(result)

Output:

ic| driver.py:14 in <module>- sys.argv[1]: 'foo'
ic| driver.py:10 in yell()- x: 'FOO'
ic| driver.py:14 in <module>- yell(ic(sys.argv[1])): 'FOO!!!'
ic| driver.py:15 in <module>- result: 'FOO!!!'

Disabling/Enabling icecream

import sys
from icecream import ic
ic.configureOutput(includeContext=True)

def yell(x: str) -> str:
    x = x.upper()
    ic(x)
    return x + '!!!'

if __name__ == '__main__':
    ic.disable()
    result = ic(yell(ic(sys.argv[1])))
    ic.enable()
    ic(result)

Output:

python driver.py foo
ic| driver.py:17 in <module>- result: 'FOO!!!'