100% found this document useful (1 vote)
124 views3 pages

Python Tools Utilities

The document discusses several Python modules that can be used as both modules and command line utilities including dis, pdb, profile, and tabnanny. It provides examples of using each module from the command line.

Uploaded by

XXX
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
124 views3 pages

Python Tools Utilities

The document discusses several Python modules that can be used as both modules and command line utilities including dis, pdb, profile, and tabnanny. It provides examples of using each module from the command line.

Uploaded by

XXX
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

PYTHON TOOLS/UTILITIES

http://www.tuto rialspo int.co m/pytho n/pytho n_to o ls_utilitie s.htm


Co pyrig ht tuto rials po int.co m

T he standard library comes with a number of modules that can be used both as modules and as command-line utilities.

The dis Module:


T he dis module is the Python disassembler. It converts byte codes to a format that is slig htly more appropriate for human consumption. You can run the disassembler from the command line. It compiles the g iven script and prints the disassembled byte codes to the ST DOUT . You can also use dis as a module. T he dis function takes a class, method, function or code object as its sing le arg ument.

Example:
#!/usr/bin/python import dis def sum(): vara = 10 varb = 20 sum = vara + varb print "vara + varb = %d" % sum # Call dis function for the function. dis.dis(sum)

T his would produce the following result:


6 7 9 0 LOAD_CONST 3 STORE_FAST 6 LOAD_CONST 9 STORE_FAST 12 15 18 19 22 25 28 29 30 31 34 LOAD_FAST LOAD_FAST BINARY_ADD STORE_FAST LOAD_CONST LOAD_FAST BINARY_MODULO PRINT_ITEM PRINT_NEWLINE LOAD_CONST RETURN_VALUE 1 (10) 0 (vara) 2 (20) 1 (varb) 0 (vara) 1 (varb) 2 (sum) 3 ('vara + varb = %d') 2 (sum)

10

0 (None)

The pdb Module


T he pdb module is the standard Python debug g er. It is based on the bdb debug g er framework. You can run the debug g er from the command line (type n [or next] to g o to the next line and help to g et a list of available commands):

Example:
Before you try to run pdb.py, set your path properly to Python lib directory. So let us try with above example sum.py:

$pdb.py sum.py > /test/sum.py(3)<module>() -> import dis (Pdb) n > /test/sum.py(5)<module>() -> def sum(): (Pdb) n >/test/sum.py(14)<module>() -> dis.dis(sum) (Pdb) n 6 0 LOAD_CONST 3 STORE_FAST 7 9 6 LOAD_CONST 9 STORE_FAST 12 15 18 19 22 25 28 29 30 31 34 LOAD_FAST LOAD_FAST BINARY_ADD STORE_FAST LOAD_CONST LOAD_FAST BINARY_MODULO PRINT_ITEM PRINT_NEWLINE LOAD_CONST RETURN_VALUE

1 (10) 0 (vara) 2 (20) 1 (varb) 0 (vara) 1 (varb) 2 (sum) 3 ('vara + varb = %d') 2 (sum)

10

0 (None)

--Return-> /test/sum.py(14)<module>()->None -v dis.dis(sum) (Pdb) n --Return-> <string>(1)<module>()->None (Pdb)

The profile Module:


T he profile module is the standard Python profiler. You can run the profiler from the command line:

Example:
Let us try to profile the following prog ram:
#!/usr/bin/python vara = 10 varb = 20 sum = vara + varb print "vara + varb = %d" % sum

Now, try running c Profile.py over this file sum.py as follows:


$cProfile.py sum.py vara + varb = 30 4 function calls in 0.000 CPU seconds Ordered by: standard name ncalls 1 1 1 1 tottime 0.000 0.000 0.000 0.000 percall 0.000 0.000 0.000 0.000 cumtime 0.000 0.000 0.000 0.000 percall filename:lineno 0.000 <string>:1(<module>) 0.000 sum.py:3(<module>) 0.000 {execfile} 0.000 {method ......}

The tabnanny Module


T he tabnanny module checks Python source files for ambig uous indentation. If a file mixes tabs and spaces in a

way that throws off indentation, no matter what tab size you're using , the nanny complains:

Example:
Let us try to profile the following prog ram:
#!/usr/bin/python vara = 10 varb = 20 sum = vara + varb print "vara + varb = %d" % sum

If you would try a correct file with tabnanny.py, then it won't complain as follows:
$tabnanny.py -v sum.py 'sum.py': Clean bill of health.

You might also like