DESCRIPTION
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.
Dynamic objects:
argv -- command line arguments; argv[0] is the script pathname if known
path -- module search path; path[0] is the script directory, else ''
modules -- dictionary of loaded modules
...lots of lines deleted here...
The help function is one interface provided by the PyDoc system—standard library
code that ships with Python and renders documentation (documentation strings, as
well as structural details) related to an object in a formatted way. The format is either
like a Unix manpage, which we get for help, or an HTML page, which is more grandiose.
It’s a handy way to get basic information when working interactively, and it’s a last
resort before falling back on manuals and books.
A Custom Paging Script
The help function we just met is also fairly fixed in the way it displays information;
although it attempts to page the display in some contexts, its page size isn’t quite right
on some of the machines I use. Moreover, it doesn’t page at all in the IDLE GUI, instead
relying on manual use if the scrollbar—potentially painful for large displays. When I
want more control over the way help text is printed, I usually use a utility script of my
own, like the one in Example 2-1.
Example 2-1. PP4E\System\more.py
"""
split and interactively page a string or file of text
"""
def more(text, numlines=15):
lines = text.splitlines() # like split('\n') but no '' at end
while lines:
chunk = lines[:numlines]
lines = lines[numlines:]
for line in chunk: print(line)
if lines and input('More?') not in ['y', 'Y']: break
if name == 'main':
import sys # when run, not imported
more(open(sys.argv[1]).read(), 10) # page contents of file on cmdline
The meat of this file is its more function, and if you know enough Python to be qualified
to read this book, it should be fairly straightforward. It simply splits up a string around
end-line characters, and then slices off and displays a few lines at a time (15 by default)
to avoid scrolling off the screen. A slice expression, lines[:15], gets the first 15 items
in a list, and lines[15:] gets the rest; to show a different number of lines each time,
System Scripting Overview | 79