10. Basic Debugging

10.1. Run in the console

  • Execute cell

  • Run File in the console

10.2. print

DATA = ['a', 'b', 'c', [1, 2, 3]]

for element in DATA:
    print(element)
    # a
    # b
    # c
    # [1, 2, 3]

10.2.1. Use cases

Listing 448. One element tuple (ADMINISTRATORS) has comma at the end
## Content of the ``settings.py`` file
ADMINISTRATORS = '[email protected]',


## Content of the ``script.py`` file
from settings import ADMINISTRATORS

for admin in ADMINISTRATORS:
    print(admin)

# [email protected]
Listing 449. Problem with missing coma for ADMINISTRATORS tuple
## Content of the ``settings.py`` file
ADMINISTRATORS = '[email protected]'


## Content of the ``script.py`` file
from settings import ADMINISTRATORS

for admin in ADMINISTRATORS:
    print(admin)

# j
# a
# n
# .
# t
# w
#[...]
# .
# p
# l

10.3. pprint

from pprint import pprint

pprint(...)
import json
from pprint import pprint

DANE = '{"contacts": [{"id": 1, "created": "2018-06-13T09:57:55.405Z", "modified": "2018-06-13T10:16:13.975Z", "reporter_id": 1, "is_deleted": false, "first_name": "José", "last_name": "Jiménez", "date_of_birth": "1969-07-24", "email": "[email protected]", "bio": "", "image": "33950257662_d7561fb140_o.jpg", "status": null, "gender": null}, {"id": 2, "created": "2018-06-13T10:26:46.948Z", "modified": "2018-06-13T10:26:46.948Z", "reporter_id": 1, "is_deleted": false, "first_name": "Jan", "last_name": "Twardowski", "date_of_birth": null, "email": null, "bio": "", "image": "", "status": null, "gender": null}, {"id": 3, "created": "2018-06-13T10:26:55.820Z", "modified": "2018-06-13T10:26:55.820Z", "reporter_id": 1, "is_deleted": false, "first_name": "Иван", "last_name": "Иванович", "date_of_birth": null, "email": null, "bio": "", "image": "", "status": null, "gender": null}, {"id": 15, "created": "2018-06-13T14:34:42.353Z", "modified": "2018-06-13T14:34:43.638Z", "reporter_id": null, "is_deleted": false, "first_name": "Mark", "last_name": "Watney", "date_of_birth": null, "email": null, "bio": null, "image": "", "status": null, "gender": null}]}'

dane = json.loads(DANE)
pprint(dane)
pprint(globals())
from pprint import pprint

print(globals())
pprint(globals())

def hello(a, b, text='My name...'):
    first_name = 'José'
    last_name = 'Jiménez'
    pprint(locals())
    return locals()


hello(1, 2)

10.3.1. pformat

from pprint import pformat


class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    def __str__(self):
        return pformat(self.__dict__, indent=1, width=120, compact=False)

p = Point(1, 2)

repr(p)
# <__main__.Point object at 0x10378a470>

str(p)
# {'x': 1, 'y': 2}

print(p)
# {'x': 1, 'y': 2}

10.4. locals()

def hello(a, b, text='My name...'):
    first_name = 'José'
    last_name = 'Jiménez'
    my_vars = locals()
    del my_vars['text']
    return my_vars

10.5. Using debugger in IDE

10.5.1. Setting Break Points

10.5.2. Inspecting variable values

10.5.3. Resume Program