Debug and Profile your Python code

Lecturer

Rositsa Kotseva

  • Full Stack Developer at HedgeServ Bulgaria
  • Python Lecturer at the HackBulgaria Academy

Content

  • Debug

    • What? Why? How?

    • Python Debug tools

  • Code Profiling

    • What? Why? How?

    • Python Profiling Tools

  • Profile visualization

Why did I choose this topic?

What is debugging?

The process of finding/detecting and fixing existing and potential bugs is called DEBUGGING

Why do we debug our code?

How to debug?

print()

def sum_number_digits(number):
    print(number)
    digits = []
    for ch in str(number):
        print(ch)
        print(type(ch))
        digits.append(ch)
    print(digits)
    return sum(digits)


print(sum_number_digits(45))
print(sum_number_digits('4O'))

Logging

import logging


logging.basicConfig(
    filename='example.log',
    level=logging.DEBUG,
    format='%(asctime)s:%(levelname)s:%(message)s'
    )


def sum_number_digits(number):
    logging.debug(number)
    digits = []
    for ch in str(number):
        logging.debug(ch)
        logging.debug(type(ch))
        digits.append(ch)
    logging.debug(digits)
    return sum(digits)


print(sum_number_digits(45))
print(sum_number_digits('4O'))

Log result

2021-03-14 22:47:12,091:DEBUG:45
2021-03-14 22:47:12,091:DEBUG:4
2021-03-14 22:47:12,091:DEBUG:<class 'str'>
2021-03-14 22:47:12,091:DEBUG:5
2021-03-14 22:47:12,091:DEBUG:<class 'str'>
2021-03-14 22:47:12,091:DEBUG:['4', '5']

Log & print based libraries

logger.add("out.log", backtrace=True, diagnose=True)

def func(a, b):
    return a / b

def nested(c):
    try:
        func(5, c)
    except ZeroDivisionError:
        logger.exception("What?!")

nested(0)

Loguru  example

2018-07-17 01:38:43.975 | ERROR    | __main__:nested:10 - What?!
Traceback (most recent call last):

  File "test.py", line 12, in <module>
    nested(0)
    └ <function nested at 0x7f5c755322f0>

> File "test.py", line 8, in nested
    func(5, c)
    │       └ 0
    └ <function func at 0x7f5c79fc2e18>

  File "test.py", line 4, in func
    return a / b
           │   └ 0
           └ 5

ZeroDivisionError: division by zero

Debuggers

Debugger is a program that assists in the detection and correction of errors in computer programs.

Python Debuggers

pdb commands

  • python -m pdb.py <file_name>.py [args] 
  • import pdb; pdb.set_trace()
  • breakpoint() 
  • help [command] 

What is Profiling?

 A set of statistics that describes how often and for how long various parts of the program executed

Why do we profile our code?

How to profile?

Python Profiling tools

Profiling Statistics

  • Total number of calls
  • primitive calls
  • ncalls
  • tottime
  • percall
  • cumtime
  • percall #2
  • filename:lineno

Profiling example

Profile Visualization

  • SnakeViz

  • pyprof2calltree

  • gprof2dot

SnakeViz example

Memory Profiler

Debug and profile Django

  • Django Debug ToolBar

  • django-cprofile-middleware

  • django-profiler

  • Django Silk

Tips & Tricks

& useful links

Thank you!

Contacts

Serverless WebSockets with AWS Lambda and API Gateway

Next event

debug-and-profile

By Rositsa Zlateva

debug-and-profile

  • 472