Python Tools – 4 Major Utilities of Python

1. Python Tools

After reading Python Flask we move towards Python Tools. Python provides us with many utilities that we can also use as command-line utilities. In this Python Tools tutorial, we will focus on – Python Dis modules, Python PDB module, Python Profile module, and Python Tabnanny module with an example.

So, let’s start with Python Tools.

Python Tools - Python Utilities

Python Tools – 4 Major Utilities of Python

Python Interview Questions

2. Which Python Tools are Commonly Used?

Here, we will discuss 4 types of Python Utilities.

a. Python Dis Module

To convert bytecode into a more human-readable format, Python has the ‘dis’ module. You can say that it compiles a script, disassembles the bytecode, and prints the output to the STDOUT. We can use it as a module or through the command line. As an argument, we can pass a function, a method, a class, or a code object to the dis() function.

Let’s take an example.

>>> def add():
     a=3
     b=4
     c=a+b
     print(f"{a}+{b}={c}")
>>> add()
3+4=7

Now, we import the Python dis module and call the dis() function on it.

>>> dis.dis(add)

2   0 LOAD_CONST   1 (3)

2 STORE_FAST 0 (a)

3    4 LOAD_CONST   2 (4)

6 STORE_FAST  1 (b)

4    8 LOAD_FAST    0 (a)

10 LOAD_FAST  1 (b)

12 BINARY_ADD

14 STORE_FAST       2 (c)

5    16 LOAD_GLOBAL 0 (print)

18 LOAD_FAST 0 (a)

20 FORMAT_VALUE 0

22 LOAD_CONST 3 (‘+’)

24 LOAD_FAST 1 (b)

26 FORMAT_VALUE 0

28 LOAD_CONST 4 (‘=’)

30 LOAD_FAST 2 (c)

32 FORMAT_VALUE 0

34 BUILD_STRING 5

36 CALL_FUNCTION 1

38 POP_TOP

40 LOAD_CONST 0 (None)

42 RETURN_VALUE

Do you know the difference between Python Modules vs Packages

b. Python Tabnanny Module

Python tabnanny checks code for ambiguous indentation. It tells us about any weird combinations of tabs and spaces in the code. In Python, whitespace shouldn’t be ambiguous.

Like dis, we can run tabnanny from the command line or using the function check().

>>> import os
>>> import tabnanny
>>> os.chdir('C:\\Users\\lifei\\Desktop')
>>> tabnanny.check('nannydemo.py')
>>>

Python Tabnanny module has the following methods:

Python Tools - 4 Major Utilities of Python

Python Tools Tutorial – Python Tabnanny Module

i. tabnanny.check(file_or_dir)

This checks the file or directory we pass to it for examining whitespace-related issues. Then, it prints the diagnostic messages to the standard output.
Let’s define Python Datetime Module with Quick Examples

ii. tabnanny.verbose

This is a flag that depicts whether Python will print verbose messages.

iii. tabnanny.filename_only

This is a flag that depicts whether Python should print only the filenames for those files that contain issues involving whitespace.
It also has the following function:

I. tabnanny.process_tokens(tokens)

The tokenize module generates some tokens. check() uses process_tokens() to process these tokens.
Finally, this module may raise the following exception:

II. exception tabnanny.NannyNag

When process_tokens() detects an ambiguous indent, it raises this exception. check() captures and handles this.

c. Python Profile Module

We may sometimes like to know which parts of our code take the longest. So in a way, through profiling, we try to find bottlenecks in our code. Other profilers in Python are cProfile and hotshot.

Let’s explore List of 63 Python os Modules in 2018

Let’s try this module with the cProfile module.

>>> import hashlib
>>> import cProfile
>>> cProfile.run("hashlib.md5('abcdefghijkl').digest()")

4 function calls in 0.000 seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)

1 0.000 0.000 0.000 0.000 <string>:1(<module>)

1 0.000 0.000 0.000 0.000 {built-in method _hashlib.openssl_md5}

1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}

1 0.000 0.000 0.000 0.000 {method ‘disable’ of ‘_lsprof.Profiler’ objects}

We profile the creation of an MD5 hash here. The first output we get tells us that four function calls were made. These are ordered by standard name. Here’s what all the other columns mean:

  • ncalls- The number of calls made.
  • tottime- Total time spent in a function.
  • percall- The quotient of tottime divided by ncalls.
  • cumtime- Cumulative time spent in this function and all subfunctions.
  • percall- The quotient of cumtime divided by primitive calls.
  • filename: lineno(function)- Data of each function.

Read about Python Collections Module in detail

d. Python PDB Module

Pdb is the standard Python debugger. It helps us debug our code line by line and we have talked about this at length in our article on Python Debugger.

>>> import pdb
>>> n=4
>>> def raised(a):
      return a**a
pdb.set_trace()
eight=raised(8)
>>> print(eight)
>>> two=raised(2)
>>> print(two)

In the command prompt:

C:\Users\lifei\Desktop>python nannydemo.py

> c:\users\lifei\desktop\pdbdemo.py(6)<module>()

-> seven=raised(7)

(Pdb)

Now, we can use the commands of the pdb module to debug our code.

For a detailed demonstration, please refer to Debugging in Python.

So, this was all about Python Tools Tutorial. Hope you like our explanation.

3. Conclusion

Hence, these are few basic tools and utilities of Python: Dis modules, PDB module, the Profile module, and Tabnanny module with an example. Furthermore, if have a doubt regarding Python Tools, feel free to ask in the comment box.
See Also – Recursion in Python with Examples
For reference – Python Dis Module

1 Response

  1. Rajkamal Mishra says:

    In this blog, the spelling of tabnanny is written as tbnanny in diagrams. Is there any specific reason for that or just a typo error.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.