8. Advanced Debugging

8.1. json.tool

$ curl -s http://localhost:8000/contact/api/
{"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": "Mark", "last_name": "Watney", "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}]}
$ curl -s http://localhost:8000/contact/api/ |python -m json.tool
{
    "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": "Mark",
            "last_name": "Watney",
            "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
        },
    ]
}

8.2. Using pdb

print('José Jiménez')
import pdb; pdb.set_trace()
print('Mark Watney')

8.3. breakpoint()

print('José Jiménez')
breakpoint()
print('Mark Watney')
  • sys.breakpointhook()
  • sys.__breakpointhook__
  • By default, sys.breakpointhook() implements the actual importing and entry into pdb.set_trace().
  • It can be set to a different function to change the debugger that breakpoint() enters.
os.environ['PYTHONBREAKPOINT'] = 'foo.bar.baz'
breakpoint()    # Imports foo.bar and calls foo.bar.baz()

8.4. Using debugger in IDE

8.4.1. Break Point

8.4.1.1. View Breakpoints

8.4.1.2. Mute Breakpoints

8.4.2. Poruszanie się

8.4.2.1. Step Over

8.4.2.2. Step Into My Code

8.4.2.3. Force Step Into

8.4.2.4. Show Execution Point

8.4.2.5. Step Out

8.4.2.6. Run to Cursor

8.4.2.7. Resume Program

8.4.2.8. New Watch

8.4.3. Frames

8.4.3.1. Previous Frame

8.4.3.2. Next Frame

8.4.3.3. Threads

8.4.4. Scope

8.4.4.1. Special Variables

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

8.4.4.2. Moduły

8.4.4.3. Stałe

8.4.4.4. Zmienne

8.4.4.5. Wartości funkcji

8.5. Debugging i Wątki

8.6. Debugging i Procesy

8.7. Debugging aplikacji sieciowych

import logging

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

8.7.1. 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')

8.8. Assignment

8.8.1. Own doctest

  1. Dla kodu z listingu Code Listing 8.4.
  2. Napisz własną uproszczoną implementację doctest
  3. Dla uproszczenia przyjmij, że zwracana zawsze będzie tylko jedna linia (bezpośrednio poniżej testu)
Code Listing 8.4. 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')