Librarybook Tools and Utilities
Librarybook Tools and Utilities
11-1
11-2
SET_LINENO
LOAD_CONST
PRINT_ITEM
PRINT_NEWLINE
LOAD_CONST
RETURN_VALUE
0
1
0 ('hello again, and welcome to the show')
1 (None)
You can also use dis as a module. The dis function takes a class, method, function, or code object as its
single argument.
Example: Using the dis module
# File:dis-example-1.py
import dis
def procedure():
print 'hello'
dis.dis(procedure)
0 SET_LINENO
3
6
9
10
11
14
SET_LINENO
LOAD_CONST
PRINT_ITEM
PRINT_NEWLINE
LOAD_CONST
RETURN_VALUE
3
4
1 ('hello')
0 (None)
11-3
11-4
11-5
7 in spam
7 in spam
7 in spam
7 in spam
11-6
tottime
0.001
0.001
0.783
0.000
percall
0.001
0.001
0.783
cumtime
0.002
0.001
0.785
0.000
percall
0.002
0.001
0.785
filename:lineno(function)
<string>:1(?)
hello.py:1(?)
profile:0(execfile('hello.py'))
profile:0(profiler)
You can modify the report to suit your needs, via the pstats module (see next page).
11-7
percall
0.002
0.051
0.001
0.000
0.000
11-8
Since the Python interpreter interprets a tab as eight spaces, the script will run correctly. It will also
display correctly, in any editor that assumes that a tab is either eight or four spaces. That's not enough
to fool the tab nanny, of course...
You can also use tabnanny from within a program.
Example: Using the tabnanny module
# File:tabnanny-example-1.py
import tabnanny
FILE = "samples/badtabs.py"
file = open(FILE)
for line in file.readlines():
print repr(line)
# let tabnanny look at it
tabnanny.check(FILE)
'if 1:\012'
' \011print "hello"\012'
'
print "world"\012'
samples/badtabs.py 3 '
print "world"\012'
(To capture the output, you can redirect sys.stdout to a StringIO object.)