How to analyze part of your program with Kolo
This guide shows you how you can use kolo.enable
to mark a section of code to be analyzed.
Usage
In your python code you can use kolo.enable
as a context manager:
import kolo
def fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == "__main__":
with kolo.enable():
print(fibonacci(6))
Warning
Kolo doesn’t trace individual lines of code, so the generated trace will only show detail about functions called within the block.
For example, kolo will only trace the fibonacci
call and return in the with
block:
def foo():
with kolo.enable():
b = 1 + 2
f = fibonacci(5)
return b + f
You can also use kolo.enable
as a decorator:
import kolo
@kolo.enable
def fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n - 1) + fibonacci(n - 2)
Kolo will now trace the fibonacci
function whenever it is called in your program.
Note
kolo.enable
can be nested arbitrarily. The outermost call will be used to generate the trace.
If you want to customise Kolo’s behaviour while using kolo.enable
you can pass a config
dictionary:
config = {
"filters": {
"include_frames": ["include_path", "/more/data/to/include"],
"ignore_frames": ["ignore_path"],
},
}
with kolo.enable(config):
...
@kolo.enable(config=config)
def example():
...
For more information about including and ignoring frames, see how to configure Kolo for Python.