4.5. Advanced Debugging

4.5.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, "firstname": "José", "lastname": "Jiménez", "birthdate": "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", "birthdate": 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": "Иванович", "birthdate": 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,
            "firstname": "José",
            "lastname": "Jiménez",
            "birthdate": "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",
            "birthdate": 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": "Иванович",
            "birthdate": null,
            "email": null,
            "bio": "",
            "image": "",
            "status": null,
            "gender": null
        },
    ]
}

4.5.2. Using pdb

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

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

4.5.4. code.interact()

  • Halt code execution and open REPL with current state

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

4.5.5. Using debugger in IDE

4.5.6. Break Point

4.5.7. View Breakpoints

4.5.8. Mute Breakpoints

4.5.9. Poruszanie się

4.5.10. Step Over

4.5.11. Step Into My Code

4.5.12. Force Step Into

4.5.13. Show Execution Point

4.5.14. Step Out

4.5.15. Run to Cursor

4.5.16. Resume Program

4.5.17. New Watch

4.5.18. Frames

4.5.19. Previous Frame

4.5.20. Next Frame

4.5.21. Threads

4.5.22. Scope

4.5.23. Special Variables

  • __file__

  • __name__

  • __builtins__

  • __doc__

  • __loader__

  • __spec__

  • __package__

4.5.24. Moduły

4.5.25. Stałe

4.5.26. Zmienne

4.5.27. Wartości funkcji

4.5.28. Debugging i Wątki

4.5.29. Debugging i Procesy

4.5.30. Debugging aplikacji sieciowych

import logging

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

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

4.5.32. Assignments

Code 4.47. Solution
"""
* Assignment: Own `doctest`
* Complexity: easy
* Lines of code: 60 lines
* Time: 21 min

English:
    1. For code from listing
    2. Write your own simplified implementation of `doctest`
    3. For simplicity, assume that only one line will always be returned (directly below the test)
    4. Run doctests - all must succeed

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)
    4. Uruchom doctesty - wszystkie muszą się powieść
"""

class Astronaut:
    """
    New Astronaut
    """

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

    def say_hello(self, lang='en'):
        """
        prints greeting according to the language

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

        >>> Astronaut(name='Pan Twardowski').say_hello(lang='pl')
        'Witaj Pan Twardowski!'
        """
        if lang == 'en':
            return f'hello {self.name}'
        elif lang == 'es':
            return f'¡hola {self.name}!'
        elif lang == 'pl':
            return f'Witaj {self.name}!'
        else:
            return f'hello {self.name}!'