3.5. Advanced Debugging

3.5.1. json.tool

$ curl -s http://example.com: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, "firstname": "José", "lastname": "Jiménez", "date_of_birth": "1969-07-24", "email": "jose.jimenez@nasa.gov", "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, "firstname": "Mark", "lastname": "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, "firstname": "Иван", "lastname": "Иванович", "date_of_birth": null, "email": null, "bio": "", "image": "", "status": null, "gender": null}]}
$ curl -s http://example.com: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,
            "firstname": "José",
            "lastname": "Jiménez",
            "date_of_birth": "1969-07-24",
            "email": "jose.jimenez@nasa.gov",
            "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,
            "firstname": "Mark",
            "lastname": "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,
            "firstname": "Иван",
            "lastname": "Иванович",
            "date_of_birth": null,
            "email": null,
            "bio": "",
            "image": "",
            "status": null,
            "gender": null
        },
    ]
}

3.5.2. Using pdb

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

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

3.5.4. code.interact()

  • Halt code execution and open REPL with current state

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

3.5.5. Using debugger in IDE

3.5.5.1. Break Point

3.5.5.1.1. View Breakpoints

3.5.5.1.2. Mute Breakpoints

3.5.5.2. Poruszanie się

3.5.5.2.1. Step Over

3.5.5.2.2. Step Into My Code

3.5.5.2.3. Force Step Into

3.5.5.2.4. Show Execution Point

3.5.5.2.5. Step Out

3.5.5.2.6. Run to Cursor

3.5.5.2.7. Resume Program

3.5.5.2.8. New Watch

3.5.5.3. Frames

3.5.5.3.1. Previous Frame

3.5.5.3.2. Next Frame

3.5.5.3.3. Threads

3.5.5.4. Scope

3.5.5.4.1. Special Variables

  • __file__

  • __name__

  • __builtins__

  • __doc__

  • __loader__

  • __spec__

  • __package__

3.5.5.4.2. Moduły

3.5.5.4.3. Stałe

3.5.5.4.4. Zmienne

3.5.5.4.5. Wartości funkcji

3.5.6. Debugging i Wątki

3.5.7. Debugging i Procesy

3.5.8. Debugging aplikacji sieciowych

import logging

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

3.5.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('This is my debug message')

3.5.9. Assignments

3.5.9.1. Own doctest

English:

Todo

English Translation

Polish:
  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)

Given:
Code 3.200. 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 zależnoś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.firstname}')
        elif lang == 'es':
            print(f'¡hola {self.firstname}!')
        elif lang == 'ru':
            print(f'здраствуйте {self.firstname}!')
        else:
            print(f'witaj {self.firstname}!')


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