11. Advanced Debugging

11.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
        },
    ]
}

11.2. Using pdb

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

11.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()

11.4. code.interact()

  • Halt code execution and open REPL with current state

import code
code.interact(local=locals())

11.5. Using debugger in IDE

11.5.1. Break Point

11.5.1.1. View Breakpoints

11.5.1.2. Mute Breakpoints

11.5.2. Poruszanie się

11.5.2.1. Step Over

11.5.2.2. Step Into My Code

11.5.2.3. Force Step Into

11.5.2.4. Show Execution Point

11.5.2.5. Step Out

11.5.2.6. Run to Cursor

11.5.2.7. Resume Program

11.5.2.8. New Watch

11.5.3. Frames

11.5.3.1. Previous Frame

11.5.3.2. Next Frame

11.5.3.3. Threads

11.5.4. Scope

11.5.4.1. Special Variables

  • __file__

  • __name__

  • __builtins__

  • __doc__

  • __loader__

  • __spec__

  • __package__

11.5.4.2. Moduły

11.5.4.3. Stałe

11.5.4.4. Zmienne

11.5.4.5. Wartości funkcji

11.6. Debugging i Wątki

11.7. Debugging i Procesy

11.8. Debugging aplikacji sieciowych

import logging

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

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

11.9. Assignments

11.9.1. Own doctest

  1. Dla kodu z listingu

  2. Napisz własną uproszczoną implementację doctest

  3. Dla uproszczenia przyjmij, że zwracana zawsze będzie tylko jedna linia (bezpośrednio poniżej testu)

Listing 11.17. Debugging with docstring
class Astronaut:
    """
    Nowy astronauta

    >>> astro = Astronaut(name='Jan Twardowski')
    >>> astro.say_hello()
    'hello Jan Twardowski'
    """

    def __init__(self, name):
        """
        Initial metod
        """
        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')