10. Debugging

10.1. Run in the console

  • Execute cell
  • Run File in the console

10.2. print

>>> lista = ['a', 'b', 'c', [1, 2, 3]]
>>> for element in lista:
...     print(element)
a
b
c
[1, 2, 3]
>>> imiona = ('Matt')

>>> for imie in imiona:
...     print(imie)
M
a
t
t

10.3. pprint

from pprint import pprint

pprint(...)

10.4. help and docstring __doc__

help(str)
def say_hello(name='José Jiménez'):
    print(f'hello {name}')

help(say_hello)
Code Listing 10.1. Debugging with docstring
class Astronaut:
    """
    Nowy astronauta
    """

    def __init__(self, name):
        self.name = name

    def say_hello(self, lang='en'):
        """
        wyświetla przywitanie w zalezności od języka

        >>> Astronaut(name='José Jiménez').say_hello(lang='es')
        ¡hola José Jiménez!

        >>> Astronaut(name='Иван Иванович').say_hello(lang='ru')
        здраствуйте Иван Иванович!
        """
        if lang == 'en':
            print(f'hello {self.first_name}')
        elif lang == 'es':
            print(f'¡hola {self.first_name}!')
        elif lang == 'ru':
            print(f'здраствуйте {self.first_name}!')
        else:
            print(f'witaj {self.first_name}!')


astronaut = Astronaut(name='José Jiménez')

help(astronaut)
print(astronaut.say_hello.__doc__)

10.5. dir() i object.__dict__

>>> class Astronaut():
...    def __init__(self):
...        self.first_name = 'José'
...        self.last_name = 'Jiménez'

>>> jose = Astronaut()

>>> jose.__dict__
{'first_name': 'José', 'last_name': 'Jiménez'}

>>> dir(jose)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'first_name', 'last_name']

10.6. Wykorzystanie debuggera w IDE

10.6.1. Break Point

10.6.1.1. View Breakpoints

10.6.1.2. Mute Breakpoints

10.6.2. Poruszanie się

10.6.2.1. Step Over

10.6.2.2. Step Into My Code

10.6.2.3. Force Step Into

10.6.2.4. Show Execution Point

10.6.2.5. Step Out

10.6.2.6. Run to Cursor

10.6.2.7. Resume Program

10.6.2.8. New Watch

10.6.3. Frames

10.6.3.1. Previous Frame

10.6.3.2. Next Frame

10.6.3.3. Threads

10.6.4. Scope

10.6.4.1. Special Variables

  • __file__
  • __name__
  • __builtins__
  • __doc__
  • __loader__
  • __spec__
  • __package__

10.6.4.2. Moduły

10.6.4.3. Stałe

10.6.4.4. Zmienne

10.6.4.5. Wartości funkcji

10.6.5. Debugging i Wątki

10.6.6. Debugging i Procesy

10.6.7. Debugging aplikacji sieciowych

import logging

logging.getLogger('requests').setLevel(logging.DEBUG)

10.6.8. Wyciszanie logowania

import logging

logging.basicConfig(
    level=logging.DEBUG,
    format='[%(asctime).19s] [%(levelname)s] %(message)s')

logging.getLogger('requests').setLevel(logging.WARNING)
log = logging.getLogger(__name__)

log.debug('to jest moja debugowa wiadomosc')