4.4. Basic Debugging¶
4.4.1. Run in the console¶
Execute cell
Run File in the console
4.4.2. print
¶
DATA = ['a', 'b', 'c', [1, 2, 3]]
for element in DATA:
print(element)
# a
# b
# c
# [1, 2, 3]
4.4.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.4.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": "Mark", "lastname": "Watney", "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='My name...'):
firstname = 'José'
lastname = 'Jiménez'
pprint(locals())
return locals()
hello(1, 2)
4.4.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.4.6. locals()
¶
def hello(a, b, text='My name...'):
firstname = 'José'
lastname = 'Jiménez'
my_vars = locals()
del my_vars['text']
return my_vars