DevTools¶
Description:
- Work with debugging tools
Resources:
Source and license:
- Some of these examples are adapted from the python-devtools documentation (https://python-devtools.helpmanual.io/usage/), Copyright (c) 2017 to present Samuel Colvin, licensed under MIT. That code stays under its own license; see THIRD_PARTY_NOTICES.md.
Example 1:¶
Debug a summation operation
In [ ]:
Copied!
from devtools import debug
v1 = {
'foo': {1: 'nested', 2: 'dict'},
'bar': ['apple', 'banana', 'carrot', 'grapefruit'],
}
debug(v1, sum(range(5)))
from devtools import debug
v1 = {
'foo': {1: 'nested', 2: 'dict'},
'bar': ['apple', 'banana', 'carrot', 'grapefruit'],
}
debug(v1, sum(range(5)))
Example 2:¶
Debug a numpy operation
In [ ]:
Copied!
import numpy as np
from devtools import debug
foo = {
'foo': np.array(range(20)),
'bar': [{'a': i, 'b': {j for j in range(1 + i * 2)}} for i in range(3)],
'spam': (i for i in ['i', 'am', 'a', 'generator']),
}
debug(foo)
# kwargs can be used as keys for what you are printing
debug(
long_string='long strings get wrapped ' * 10,
new_line_string='wraps also on newline\n' * 3,
)
bar = {1: 2, 11: 12}
# debug can also show the output of expressions
debug(
len(foo),
bar[1],
foo == bar
)
import numpy as np
from devtools import debug
foo = {
'foo': np.array(range(20)),
'bar': [{'a': i, 'b': {j for j in range(1 + i * 2)}} for i in range(3)],
'spam': (i for i in ['i', 'am', 'a', 'generator']),
}
debug(foo)
# kwargs can be used as keys for what you are printing
debug(
long_string='long strings get wrapped ' * 10,
new_line_string='wraps also on newline\n' * 3,
)
bar = {1: 2, 11: 12}
# debug can also show the output of expressions
debug(
len(foo),
bar[1],
foo == bar
)
Example 3:¶
Debug a numpy operation with highlighting
In [ ]:
Copied!
import numpy as np
from devtools import PrettyFormat, pformat, pprint
v = {
'foo': {'whatever': [3, 2, 1]},
'sentence': 'hello\nworld',
'generator': (i * 2 for i in [1, 2, 3]),
'matrix': np.matrix([[1, 2, 3, 4],
[50, 60, 70, 80],
[900, 1000, 1100, 1200],
[13000, 14000, 15000, 16000]])
}
# pretty print of v
pprint(v)
# as above without colours, the generator will also be empty as
# it's already been evaluated
s = pformat(v, highlight=False)
print(s)
pp = PrettyFormat(
indent_step=2, # default: 4
indent_char='.', # default: space
repr_strings=True, # default: False
# default: 10 (if line is below this length it'll be shown on one line)
simple_cutoff=2,
width=80, # default: 120
# default: True (whether to evaluate generators
yield_from_generators=False,
)
print(pp(v, highlight=True))
import numpy as np
from devtools import PrettyFormat, pformat, pprint
v = {
'foo': {'whatever': [3, 2, 1]},
'sentence': 'hello\nworld',
'generator': (i * 2 for i in [1, 2, 3]),
'matrix': np.matrix([[1, 2, 3, 4],
[50, 60, 70, 80],
[900, 1000, 1100, 1200],
[13000, 14000, 15000, 16000]])
}
# pretty print of v
pprint(v)
# as above without colours, the generator will also be empty as
# it's already been evaluated
s = pformat(v, highlight=False)
print(s)
pp = PrettyFormat(
indent_step=2, # default: 4
indent_char='.', # default: space
repr_strings=True, # default: False
# default: 10 (if line is below this length it'll be shown on one line)
simple_cutoff=2,
width=80, # default: 120
# default: True (whether to evaluate generators
yield_from_generators=False,
)
print(pp(v, highlight=True))