Jupyter Notebook [http://jupyter.org/] is a open source application for writing and sharing code and text.It is one of the popular data science tool in python.
It has replaced IPython notebook, which initially supported only python but now later started supporting many languages. IPython notebooks was started to make working with python easy and interactive. IPython provides python backend (also known as kernel) for Jupyter
If you installed Python using Anaconda, then you have the Jupyter Notebook installed. To start the notebook
After starting Jupyter notebook, well notice that a tab will open in a web browser open. It will run the Jupyter Notebook on a local port, such as http://localhost:8888. It will list out the contents of the system in a directory format. To create new notebooks just click New and then selecting the python version.
Using Jupyter Notebook we can create shareable files that can support live code, charts, graphs, math, different forms of markup (Markdown, etc.), and much more.
pdb and ipdb
pdb
pdb [https://docs.python.org/3/library/pdb.html] is a debugging tool that is part of pythons standard library. It is an interactive source code debugger for Python programs.
Using pdb, we can set breakpoints [https://en.wikipedia.org/wiki/Breakpoint] at any point of our program to stop it and check for errors or the status of our running program.
pbd help speed up the debugging process a lot faster than using simple print() statements everywhere.
The easiest way to use pdb is to call it in the code youre working on.
import pdb ; pdb . set_trace ()
As soon as the interpreter reaches this line, well receive a command prompt on the terminal where were running the program. This is a general Python prompt, but with some new commands.
l (list) - Display 11 lines around the current line.
r (return) - Continue execution until the current function returns.
b (break) - Set a breakpoint (depending on the argument provided).
n (next) - Continue execution until the next line in the current function is reached.
s (step) - Execute the current line, stop at the first possible occasion.
j (jump) - Jump to the next line to be executed.
c (continue) - Creates a breakpoint in the program execution.
for more commands check [https://docs.python.org/3/library/pdb.html#debugger-commands]
If we want to run the application from the debugger and set breakpoints without any changes in the source code, then we need to execute the application with the debugger, use the command
$ python -m pdb hello.py
ipdb
ipdb, the Ipython-enabled python debugger, with all pdbs features and adds ipython support for the interactive shell, like tab completion, color support, magic functions and more. We can use ipdb just as we use pdb.
Linters and formatters
PEP8
PEP8 [https://www.python.org/dev/peps/pep-0008/?] is the official style guide for python. It is important to know the style-guide if you want to be a part of the python-community.
PEP8 coding conventions are:
Spaces are the preferred indentation method.
Use 4 spaces per indentation level.
Limit all lines to a maximum of 79 characters.
Separate top-level function and class definitions with two blank lines.
Method definitions inside a class are surrounded by a single blank line.
Imports should be grouped in the following order:
Standard library imports.
Related third party imports.
Local application/library specific imports.
A blank line between each group of imports.
pycodestyle
Pycodestyle [https://github.com/PyCQA/pycodestyle] (Formerly PEP8) is the official linter tool to check the python code against the style conventions of PEP8 python.
To install it: pip install pycodestyle
.
Let us take a small example script to test pycodestyle
We will create a test script file test_script.py
and use it as an example for all the linters.
from __future__ import print_function import os , sys import logging from .. import views class DoSomething ( SomeCommand ) : def __init__ ( self ): for i in range ( , ): if self . number == i : print ( "matched" ) else : print ( 'not matched' ) def check_user ( self ): if self . user : return True else : return False
If we run pycodestyle: $ pycodestyle {source_file_or_directory}
$ pycodestyle test_script.pytest_script.py:2:10: E401 multiple imports on one linetest_script.py:6:1: E302 expected blank lines, found test_script.py:6:31: E203 whitespace before ':' test_script.py:9:25: E231 missing whitespace after ',' test_script.py:13:37: W291 trailing whitespacetest_script.py:16:21: E701 multiple statements on one line ( colon ) test_script.py:16:34: W291 trailing whitespacetest_script.py:17:13: E271 multiple spaces after keywordtest_script.py:17:14: E203 whitespace before ':' test_script.py:17:15: E701 multiple statements on one line ( colon ) test_script.py:17:29: W291 trailing whitespace
To see the summary, use --statistics -qq
$ pycodestyle --statistics -qq {source_file_or_directory}
$ pycodestyle --statistics -qq test_script.py E203 whitespace before ':' E231 missing whitespace after ',' E271 multiple spaces after keyword E302 expected blank lines, found E401 multiple imports on one line E701 multiple statements on one line ( colon ) W291 trailing whitespace