4.1. Debugging Aboutï
4.1.1. Run in the consoleï
Execute cell
Run File in the console
4.1.2. printï
DATA = ['a', 'b', 'c', [1, 2, 3]]
for element in DATA:
print(element)
# a
# b
# c
# [1, 2, 3]
4.1.3. Use casesï
One element tuple (ADMINISTRATORS) has comma at the end:
## Content of the ``settings.py`` file
ADMINISTRATORS = 'mwatney@nasa.gov',
## Content of the ``script.py`` file
from settings import ADMINISTRATORS
for admin in ADMINISTRATORS:
print(admin)
# mwatney@nasa.gov
Problem with missing coma for ADMINISTRATORS tuple:
## Content of the ``settings.py`` file
ADMINISTRATORS = 'mwatney@nasa.gov'
## Content of the ``script.py`` file
from settings import ADMINISTRATORS
for admin in ADMINISTRATORS:
print(admin)
# m
# w
# a
# t
# n
# e
# y
# @
# n
# a
# s
# a
# .
# g
# o
# v
4.1.4. pprintï
from pprint import pprint
pprint(...)
import json
from pprint import pprint
DATA = '{"contacts": [{"id": 1, "created": "2018-06-13T09:57:55.405Z", "modified": "2018-06-13T10:16:13.975Z", "reporter_id": 1, "is_deleted": false, "firstname": "Bob", "lastname": "Blackthorn", "birthdate": "1969-07-24", "email": "mwatney@nasa.gov", "bio": "", "image": "mwatney.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": "Melissa", "lastname": "Lewis", "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": "Rick", "lastname": "Martinez", "birthdate": null, "email": null, "bio": "", "image": "", "status": null, "gender": null}, {"id": 15, "created": "2018-06-13T14:34:42.353Z", "modified": "2018-06-13T14:34:43.638Z", "reporter_id": null, "is_deleted": false, "firstname": "Alex", "lastname": "Vogel", "birthdate": null, "email": null, "bio": null, "image": "", "status": null, "gender": null}]}'
data = json.loads(DATA)
pprint(data)
pprint(globals())
from pprint import pprint
print(globals())
pprint(globals())
def hello(a, b, text='Hello'):
firstname = 'Alice'
lastname = 'Apricot'
pprint(locals())
return locals()
hello(1, 2)
4.1.5. pformatï
from pprint import pformat
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __str__(self):
return pformat(self.__dict__, indent=1, width=120, compact=False)
p = Point(1, 2)
repr(p)
# <__main__.Point object at 0x10378a470>
str(p)
# {'x': 1, 'y': 2}
print(p)
# {'x': 1, 'y': 2}
4.1.6. locals()ï
def hello(a, b, text='Hello'):
firstname = 'Alice'
lastname = 'Apricot'
my_vars = locals()
del my_vars['text']
return my_vars
4.1.7. Using debugger in IDEï
4.1.8. Setting Break Pointsï
4.1.9. Inspecting variable valuesï
4.1.10. Resume Programï
4.1.11. 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": "Alice", "lastname": "Apricot", "birthdate": "2000-01-01", "email": "alice@example.com, "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": "Bob", "lastname": "Blackthorn", "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": "Carol", "lastname": "Corn", "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": "Alice",
"lastname": "Apricot",
"birthdate": "2000-01-01",
"email": "alice@example.com,
"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": "Bob",
"lastname": "Blackthorn",
"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": "Carol",
"lastname": "Corn",
"birthdate": null,
"email": null,
"bio": "",
"image": "",
"status": null,
"gender": null
},
]
}
4.1.12. Using pdbï
print('Alice Apricot')
import pdb; pdb.set_trace()
print('Bob Blackthorn')
4.1.13. breakpoint()ï
print('Alice Apricot')
breakpoint()
print('Bob Blackthorn')
sys.breakpointhook()sys.__breakpointhook__By default,
sys.breakpointhook()implements the actual importing and entry intopdb.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.1.14. code.interact()ï
Halt code execution and open REPL with current state
import code
code.interact(local=locals())
4.1.15. Using debugger in IDEï
4.1.16. Break Pointï
4.1.17. View Breakpointsï
4.1.18. Mute Breakpointsï
4.1.19. Poruszanie siÄï
4.1.20. Step Overï
4.1.21. Step Into My Codeï
4.1.22. Force Step Intoï
4.1.23. Show Execution Pointï
4.1.24. Step Outï
4.1.25. Run to Cursorï
4.1.26. Resume Programï
4.1.27. New Watchï
4.1.28. Framesï
4.1.29. Previous Frameï
4.1.30. Next Frameï
4.1.31. Threadsï
4.1.32. Scopeï
4.1.33. Special Variablesï
__file____name____builtins____doc____loader____spec____package__
4.1.34. ModuÅyï
4.1.35. StaÅeï
4.1.36. Zmienneï
4.1.37. WartoÅci funkcjiï
4.1.38. Debugging i WÄ tkiï
4.1.39. Debugging i Procesyï
4.1.40. Debugging aplikacji sieciowychï
import logging
logging.getLogger('requests').setLevel(logging.DEBUG)
4.1.41. 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.1.42. Assignmentsï
# FIXME: Write tests
# %% About
# - Name: Own `doctest`
# - Difficulty: easy
# - Lines: 60
# - Minutes: 21
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% 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ÅÄ
# %% Doctests
"""
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
from typing import Callable
User: type
doctest: Callable[[str], None]
# %% Data
class User:
"""
New User
"""
def __init__(self, name):
self.name = name
def say_hello(self, lang='en'):
"""
prints greeting according to the language
>>> User(name='Alice').say_hello(lang='es')
'¡hola Alice!'
>>> User(name='Bob').say_hello(lang='pl')
'Witaj Bob!'
"""
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}!'
# %% Result