Mass patch by Ka-Ping Yee:

1. Comments at the beginning of the module, before
       functions, and before classes have been turned
       into docstrings.

    2. Tabs are normalized to four spaces.

Also, removed the "remove" function from dircmp.py, which reimplements
list.remove() (it must have been very old).
This commit is contained in:
Guido van Rossum 2000-02-02 15:10:15 +00:00
parent 113e70efa2
commit 4acc25bd39
18 changed files with 2513 additions and 2515 deletions

View File

@ -1,4 +1,4 @@
# A multi-producer, multi-consumer queue. """A multi-producer, multi-consumer queue."""
# define this exception to be compatible with Python 1.5's class # define this exception to be compatible with Python 1.5's class
# exceptions, but also when -X option is used. # exceptions, but also when -X option is used.

View File

@ -1,30 +1,30 @@
# class StringIO implements file-like objects that read/write a """File-like objects that read from or write to a string buffer.
# string buffer (a.k.a. "memory files").
# This implements (nearly) all stdio methods.
# This implements (nearly) all stdio methods.
# f = StringIO() # ready for writing
# f = StringIO() # ready for writing f = StringIO(buf) # ready for reading
# f = StringIO(buf) # ready for reading f.close() # explicitly release resources held
# f.close() # explicitly release resources held flag = f.isatty() # always false
# flag = f.isatty() # always false pos = f.tell() # get current position
# pos = f.tell() # get current position f.seek(pos) # set current position
# f.seek(pos) # set current position f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
# f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF buf = f.read() # read until EOF
# buf = f.read() # read until EOF buf = f.read(n) # read up to n bytes
# buf = f.read(n) # read up to n bytes buf = f.readline() # read until end of line ('\n') or EOF
# buf = f.readline() # read until end of line ('\n') or EOF list = f.readlines()# list of f.readline() results until EOF
# list = f.readlines()# list of f.readline() results until EOF f.write(buf) # write at current position
# f.write(buf) # write at current position f.writelines(list) # for line in list: f.write(line)
# f.writelines(list) # for line in list: f.write(line) f.getvalue() # return whole file's contents as a string
# f.getvalue() # return whole file's contents as a string
# Notes:
# Notes: - Using a real file is often faster (but less convenient).
# - Using a real file is often faster (but less convenient). - fileno() is left unimplemented so that code which uses it triggers
# - fileno() is left unimplemented so that code which uses it triggers an exception early.
# an exception early. - Seeking far beyond EOF and then writing will insert real null
# - Seeking far beyond EOF and then writing will insert real null bytes that occupy space in the buffer.
# bytes that occupy space in the buffer. - There's a simple test set (see end of this file).
# - There's a simple test set (see end of this file). """
import string import string

View File

@ -1,4 +1,4 @@
# A more or less complete user-defined wrapper around dictionary objects """A more or less complete user-defined wrapper around dictionary objects."""
class UserDict: class UserDict:
def __init__(self, dict=None): def __init__(self, dict=None):

View File

@ -1,4 +1,4 @@
# A more or less complete user-defined wrapper around list objects """A more or less complete user-defined wrapper around list objects."""
class UserList: class UserList:
def __init__(self, list=None): def __init__(self, list=None):

View File

@ -1,137 +1,138 @@
# Stuff to parse AIFF-C and AIFF files. """Stuff to parse AIFF-C and AIFF files.
#
# Unless explicitly stated otherwise, the description below is true Unless explicitly stated otherwise, the description below is true
# both for AIFF-C files and AIFF files. both for AIFF-C files and AIFF files.
#
# An AIFF-C file has the following structure. An AIFF-C file has the following structure.
#
# +-----------------+ +-----------------+
# | FORM | | FORM |
# +-----------------+ +-----------------+
# | <size> | | <size> |
# +----+------------+ +----+------------+
# | | AIFC | | | AIFC |
# | +------------+ | +------------+
# | | <chunks> | | | <chunks> |
# | | . | | | . |
# | | . | | | . |
# | | . | | | . |
# +----+------------+ +----+------------+
#
# An AIFF file has the string "AIFF" instead of "AIFC". An AIFF file has the string "AIFF" instead of "AIFC".
#
# A chunk consists of an identifier (4 bytes) followed by a size (4 bytes, A chunk consists of an identifier (4 bytes) followed by a size (4 bytes,
# big endian order), followed by the data. The size field does not include big endian order), followed by the data. The size field does not include
# the size of the 8 byte header. the size of the 8 byte header.
#
# The following chunk types are recognized. The following chunk types are recognized.
#
# FVER FVER
# <version number of AIFF-C defining document> (AIFF-C only). <version number of AIFF-C defining document> (AIFF-C only).
# MARK MARK
# <# of markers> (2 bytes) <# of markers> (2 bytes)
# list of markers: list of markers:
# <marker ID> (2 bytes, must be > 0) <marker ID> (2 bytes, must be > 0)
# <position> (4 bytes) <position> (4 bytes)
# <marker name> ("pstring") <marker name> ("pstring")
# COMM COMM
# <# of channels> (2 bytes) <# of channels> (2 bytes)
# <# of sound frames> (4 bytes) <# of sound frames> (4 bytes)
# <size of the samples> (2 bytes) <size of the samples> (2 bytes)
# <sampling frequency> (10 bytes, IEEE 80-bit extended <sampling frequency> (10 bytes, IEEE 80-bit extended
# floating point) floating point)
# in AIFF-C files only: in AIFF-C files only:
# <compression type> (4 bytes) <compression type> (4 bytes)
# <human-readable version of compression type> ("pstring") <human-readable version of compression type> ("pstring")
# SSND SSND
# <offset> (4 bytes, not used by this program) <offset> (4 bytes, not used by this program)
# <blocksize> (4 bytes, not used by this program) <blocksize> (4 bytes, not used by this program)
# <sound data> <sound data>
#
# A pstring consists of 1 byte length, a string of characters, and 0 or 1 A pstring consists of 1 byte length, a string of characters, and 0 or 1
# byte pad to make the total length even. byte pad to make the total length even.
#
# Usage. Usage.
#
# Reading AIFF files: Reading AIFF files:
# f = aifc.open(file, 'r') f = aifc.open(file, 'r')
# where file is either the name of a file or an open file pointer. where file is either the name of a file or an open file pointer.
# The open file pointer must have methods read(), seek(), and close(). The open file pointer must have methods read(), seek(), and close().
# In some types of audio files, if the setpos() method is not used, In some types of audio files, if the setpos() method is not used,
# the seek() method is not necessary. the seek() method is not necessary.
#
# This returns an instance of a class with the following public methods: This returns an instance of a class with the following public methods:
# getnchannels() -- returns number of audio channels (1 for getnchannels() -- returns number of audio channels (1 for
# mono, 2 for stereo) mono, 2 for stereo)
# getsampwidth() -- returns sample width in bytes getsampwidth() -- returns sample width in bytes
# getframerate() -- returns sampling frequency getframerate() -- returns sampling frequency
# getnframes() -- returns number of audio frames getnframes() -- returns number of audio frames
# getcomptype() -- returns compression type ('NONE' for AIFF files) getcomptype() -- returns compression type ('NONE' for AIFF files)
# getcompname() -- returns human-readable version of getcompname() -- returns human-readable version of
# compression type ('not compressed' for AIFF files) compression type ('not compressed' for AIFF files)
# getparams() -- returns a tuple consisting of all of the getparams() -- returns a tuple consisting of all of the
# above in the above order above in the above order
# getmarkers() -- get the list of marks in the audio file or None getmarkers() -- get the list of marks in the audio file or None
# if there are no marks if there are no marks
# getmark(id) -- get mark with the specified id (raises an error getmark(id) -- get mark with the specified id (raises an error
# if the mark does not exist) if the mark does not exist)
# readframes(n) -- returns at most n frames of audio readframes(n) -- returns at most n frames of audio
# rewind() -- rewind to the beginning of the audio stream rewind() -- rewind to the beginning of the audio stream
# setpos(pos) -- seek to the specified position setpos(pos) -- seek to the specified position
# tell() -- return the current position tell() -- return the current position
# close() -- close the instance (make it unusable) close() -- close the instance (make it unusable)
# The position returned by tell(), the position given to setpos() and The position returned by tell(), the position given to setpos() and
# the position of marks are all compatible and have nothing to do with the position of marks are all compatible and have nothing to do with
# the actual postion in the file. the actual postion in the file.
# The close() method is called automatically when the class instance The close() method is called automatically when the class instance
# is destroyed. is destroyed.
#
# Writing AIFF files: Writing AIFF files:
# f = aifc.open(file, 'w') f = aifc.open(file, 'w')
# where file is either the name of a file or an open file pointer. where file is either the name of a file or an open file pointer.
# The open file pointer must have methods write(), tell(), seek(), and The open file pointer must have methods write(), tell(), seek(), and
# close(). close().
#
# This returns an instance of a class with the following public methods: This returns an instance of a class with the following public methods:
# aiff() -- create an AIFF file (AIFF-C default) aiff() -- create an AIFF file (AIFF-C default)
# aifc() -- create an AIFF-C file aifc() -- create an AIFF-C file
# setnchannels(n) -- set the number of channels setnchannels(n) -- set the number of channels
# setsampwidth(n) -- set the sample width setsampwidth(n) -- set the sample width
# setframerate(n) -- set the frame rate setframerate(n) -- set the frame rate
# setnframes(n) -- set the number of frames setnframes(n) -- set the number of frames
# setcomptype(type, name) setcomptype(type, name)
# -- set the compression type and the -- set the compression type and the
# human-readable compression type human-readable compression type
# setparams(tuple) setparams(tuple)
# -- set all parameters at once -- set all parameters at once
# setmark(id, pos, name) setmark(id, pos, name)
# -- add specified mark to the list of marks -- add specified mark to the list of marks
# tell() -- return current position in output file (useful tell() -- return current position in output file (useful
# in combination with setmark()) in combination with setmark())
# writeframesraw(data) writeframesraw(data)
# -- write audio frames without pathing up the -- write audio frames without pathing up the
# file header file header
# writeframes(data) writeframes(data)
# -- write audio frames and patch up the file header -- write audio frames and patch up the file header
# close() -- patch up the file header and close the close() -- patch up the file header and close the
# output file output file
# You should set the parameters before the first writeframesraw or You should set the parameters before the first writeframesraw or
# writeframes. The total number of frames does not need to be set, writeframes. The total number of frames does not need to be set,
# but when it is set to the correct value, the header does not have to but when it is set to the correct value, the header does not have to
# be patched up. be patched up.
# It is best to first set all parameters, perhaps possibly the It is best to first set all parameters, perhaps possibly the
# compression type, and then write audio frames using writeframesraw. compression type, and then write audio frames using writeframesraw.
# When all frames have been written, either call writeframes('') or When all frames have been written, either call writeframes('') or
# close() to patch up the sizes in the header. close() to patch up the sizes in the header.
# Marks can be added anytime. If there are any marks, ypu must call Marks can be added anytime. If there are any marks, ypu must call
# close() after all frames have been written. close() after all frames have been written.
# The close() method is called automatically when the class instance The close() method is called automatically when the class instance
# is destroyed. is destroyed.
#
# When a file is opened with the extension '.aiff', an AIFF file is When a file is opened with the extension '.aiff', an AIFF file is
# written, otherwise an AIFF-C file is written. This default can be written, otherwise an AIFF-C file is written. This default can be
# changed by calling aiff() or aifc() before the first writeframes or changed by calling aiff() or aifc() before the first writeframes or
# writeframesraw. writeframesraw.
"""
import struct import struct
import __builtin__ import __builtin__

View File

@ -1,3 +1,5 @@
"""Classes for manipulating audio devices (currently only for Sun and SGI)"""
error = 'audiodev.error' error = 'audiodev.error'
class Play_Audio_sgi: class Play_Audio_sgi:

View File

@ -1,7 +1,7 @@
#! /usr/bin/env python #! /usr/bin/env python
# Conversions to/from base64 transport encoding as per RFC-1521. """Conversions to/from base64 transport encoding as per RFC-1521."""
#
# Modified 04-Oct-95 by Jack to use binascii module # Modified 04-Oct-95 by Jack to use binascii module
import binascii import binascii
@ -9,8 +9,8 @@ import binascii
MAXLINESIZE = 76 # Excluding the CRLF MAXLINESIZE = 76 # Excluding the CRLF
MAXBINSIZE = (MAXLINESIZE/4)*3 MAXBINSIZE = (MAXLINESIZE/4)*3
# Encode a file.
def encode(input, output): def encode(input, output):
"""Encode a file."""
while 1: while 1:
s = input.read(MAXBINSIZE) s = input.read(MAXBINSIZE)
if not s: break if not s: break
@ -21,8 +21,8 @@ def encode(input, output):
line = binascii.b2a_base64(s) line = binascii.b2a_base64(s)
output.write(line) output.write(line)
# Decode a file.
def decode(input, output): def decode(input, output):
"""Decode a file."""
while 1: while 1:
line = input.readline() line = input.readline()
if not line: break if not line: break
@ -30,6 +30,7 @@ def decode(input, output):
output.write(s) output.write(s)
def encodestring(s): def encodestring(s):
"""Encode a string."""
import StringIO import StringIO
f = StringIO.StringIO(s) f = StringIO.StringIO(s)
g = StringIO.StringIO() g = StringIO.StringIO()
@ -37,14 +38,15 @@ def encodestring(s):
return g.getvalue() return g.getvalue()
def decodestring(s): def decodestring(s):
"""Decode a string."""
import StringIO import StringIO
f = StringIO.StringIO(s) f = StringIO.StringIO(s)
g = StringIO.StringIO() g = StringIO.StringIO()
decode(f, g) decode(f, g)
return g.getvalue() return g.getvalue()
# Small test program
def test(): def test():
"""Small test program"""
import sys, getopt import sys, getopt
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'deut') opts, args = getopt.getopt(sys.argv[1:], 'deut')

View File

@ -1,4 +1,4 @@
# Debugger basics """Debugger basics"""
import sys import sys
import os import os
@ -119,46 +119,46 @@ class Bdb:
# to gain control. # to gain control.
def user_call(self, frame, argument_list): def user_call(self, frame, argument_list):
# This method is called when there is the remote possibility """This method is called when there is the remote possibility
# that we ever need to stop in this function that we ever need to stop in this function."""
pass pass
def user_line(self, frame): def user_line(self, frame):
# This method is called when we stop or break at this line """This method is called when we stop or break at this line."""
pass pass
def user_return(self, frame, return_value): def user_return(self, frame, return_value):
# This method is called when a return trap is set here """This method is called when a return trap is set here."""
pass pass
def user_exception(self, frame, (exc_type, exc_value, exc_traceback)): def user_exception(self, frame, (exc_type, exc_value, exc_traceback)):
# This method is called if an exception occurs, """This method is called if an exception occurs,
# but only if we are to stop at or just below this level but only if we are to stop at or just below this level."""
pass pass
# Derived classes and clients can call the following methods # Derived classes and clients can call the following methods
# to affect the stepping state. # to affect the stepping state.
def set_step(self): def set_step(self):
# Stop after one line of code """Stop after one line of code."""
self.stopframe = None self.stopframe = None
self.returnframe = None self.returnframe = None
self.quitting = 0 self.quitting = 0
def set_next(self, frame): def set_next(self, frame):
# Stop on the next line in or below the given frame """Stop on the next line in or below the given frame."""
self.stopframe = frame self.stopframe = frame
self.returnframe = None self.returnframe = None
self.quitting = 0 self.quitting = 0
def set_return(self, frame): def set_return(self, frame):
# Stop when returning from the given frame """Stop when returning from the given frame."""
self.stopframe = frame.f_back self.stopframe = frame.f_back
self.returnframe = frame self.returnframe = frame
self.quitting = 0 self.quitting = 0
def set_trace(self): def set_trace(self):
# Start debugging from here """Start debugging from here."""
try: try:
1 + '' 1 + ''
except: except:

View File

@ -1,4 +1,5 @@
"""binhex - Macintosh binhex compression/decompression """binhex - Macintosh binhex compression/decompression
easy interface: easy interface:
binhex(inputfilename, outputfilename) binhex(inputfilename, outputfilename)
hexbin(inputfilename, outputfilename) hexbin(inputfilename, outputfilename)

View File

@ -1,9 +1,8 @@
# Bisection algorithms """Bisection algorithms."""
# Insert item x in list a, and keep it sorted assuming a is sorted
def insort(a, x, lo=0, hi=None): def insort(a, x, lo=0, hi=None):
"""Insert item x in list a, and keep it sorted assuming a is sorted."""
if hi is None: if hi is None:
hi = len(a) hi = len(a)
while lo < hi: while lo < hi:
@ -13,9 +12,8 @@ def insort(a, x, lo=0, hi=None):
a.insert(lo, x) a.insert(lo, x)
# Find the index where to insert item x in list a, assuming a is sorted
def bisect(a, x, lo=0, hi=None): def bisect(a, x, lo=0, hi=None):
"""Find the index where to insert item x in list a, assuming a is sorted."""
if hi is None: if hi is None:
hi = len(a) hi = len(a)
while lo < hi: while lo < hi:

View File

@ -1,6 +1,4 @@
############################### """Calendar printing functions"""
# Calendar printing functions #
###############################
# Revision 2: uses funtions from built-in time module # Revision 2: uses funtions from built-in time module
@ -22,42 +20,42 @@ February = 2
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Full and abbreviated names of weekdays # Full and abbreviated names of weekdays
day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', \ day_name = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday'] 'Friday', 'Saturday', 'Sunday']
day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] day_abbr = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Full and abbreviated names of months (1-based arrays!!!) # Full and abbreviated names of months (1-based arrays!!!)
month_name = ['', 'January', 'February', 'March', 'April', \ month_name = ['', 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August', \ 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'] 'September', 'October', 'November', 'December']
month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \ month_abbr = [' ', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
# Return 1 for leap years, 0 for non-leap years
def isleap(year): def isleap(year):
"""Return 1 for leap years, 0 for non-leap years."""
return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0) return year % 4 == 0 and (year % 100 <> 0 or year % 400 == 0)
# Return number of leap years in range [y1, y2)
# Assume y1 <= y2 and no funny (non-leap century) years
def leapdays(y1, y2): def leapdays(y1, y2):
"""Return number of leap years in range [y1, y2).
Assume y1 <= y2 and no funny (non-leap century) years."""
return (y2+3)/4 - (y1+3)/4 return (y2+3)/4 - (y1+3)/4
# Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)
def weekday(year, month, day): def weekday(year, month, day):
"""Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12), day (1-31)."""
secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0)) secs = mktime((year, month, day, 0, 0, 0, 0, 0, 0))
tuple = localtime(secs) tuple = localtime(secs)
return tuple[6] return tuple[6]
# Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month
def monthrange(year, month): def monthrange(year, month):
"""Return weekday (0-6 ~ Mon-Sun) and number of days (28-31) for year, month."""
if not 1 <= month <= 12: raise ValueError, 'bad month number' if not 1 <= month <= 12: raise ValueError, 'bad month number'
day1 = weekday(year, month, 1) day1 = weekday(year, month, 1)
ndays = mdays[month] + (month == February and isleap(year)) ndays = mdays[month] + (month == February and isleap(year))
return day1, ndays return day1, ndays
# Return a matrix representing a month's calendar
# Each row represents a week; days outside this month are zero
def _monthcalendar(year, month): def _monthcalendar(year, month):
"""Return a matrix representing a month's calendar.
Each row represents a week; days outside this month are zero."""
day1, ndays = monthrange(year, month) day1, ndays = monthrange(year, month)
rows = [] rows = []
r7 = range(7) r7 = range(7)
@ -70,9 +68,9 @@ def _monthcalendar(year, month):
rows.append(row) rows.append(row)
return rows return rows
# Caching interface to _monthcalendar
_mc_cache = {} _mc_cache = {}
def monthcalendar(year, month): def monthcalendar(year, month):
"""Caching interface to _monthcalendar."""
key = (year, month) key = (year, month)
if _mc_cache.has_key(key): if _mc_cache.has_key(key):
return _mc_cache[key] return _mc_cache[key]
@ -80,23 +78,23 @@ def monthcalendar(year, month):
_mc_cache[key] = ret = _monthcalendar(year, month) _mc_cache[key] = ret = _monthcalendar(year, month)
return ret return ret
# Center a string in a field
def _center(str, width): def _center(str, width):
"""Center a string in a field."""
n = width - len(str) n = width - len(str)
if n <= 0: return str if n <= 0: return str
return ' '*((n+1)/2) + str + ' '*((n)/2) return ' '*((n+1)/2) + str + ' '*((n)/2)
# XXX The following code knows that print separates items with space! # XXX The following code knows that print separates items with space!
# Print a single week (no newline)
def prweek(week, width): def prweek(week, width):
"""Print a single week (no newline)."""
for day in week: for day in week:
if day == 0: s = '' if day == 0: s = ''
else: s = `day` else: s = `day`
print _center(s, width), print _center(s, width),
# Return a header for a week
def weekheader(width): def weekheader(width):
"""Return a header for a week."""
str = '' str = ''
if width >= 9: names = day_name if width >= 9: names = day_name
else: names = day_abbr else: names = day_abbr
@ -105,8 +103,8 @@ def weekheader(width):
str = str + _center(names[i%7][:width], width) str = str + _center(names[i%7][:width], width)
return str return str
# Print a month's calendar
def prmonth(year, month, w = 0, l = 0): def prmonth(year, month, w = 0, l = 0):
"""Print a month's calendar."""
w = max(2, w) w = max(2, w)
l = max(1, l) l = max(1, l)
print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1), print _center(month_name[month] + ' ' + `year`, 7*(w+1) - 1),
@ -121,16 +119,16 @@ def prmonth(year, month, w = 0, l = 0):
_colwidth = 7*3 - 1 # Amount printed by prweek() _colwidth = 7*3 - 1 # Amount printed by prweek()
_spacing = ' '*4 # Spaces between columns _spacing = ' '*4 # Spaces between columns
# 3-column formatting for year calendars
def format3c(a, b, c): def format3c(a, b, c):
"""3-column formatting for year calendars"""
print _center(a, _colwidth), print _center(a, _colwidth),
print _spacing, print _spacing,
print _center(b, _colwidth), print _center(b, _colwidth),
print _spacing, print _spacing,
print _center(c, _colwidth) print _center(c, _colwidth)
# Print a year's calendar
def prcal(year): def prcal(year):
"""Print a year's calendar."""
header = weekheader(2) header = weekheader(2)
format3c('', `year`, '') format3c('', `year`, '')
for q in range(January, January+12, 3): for q in range(January, January+12, 3):
@ -152,9 +150,9 @@ def prcal(year):
print _spacing, print _spacing,
print print
# Unrelated but handy function to calculate Unix timestamp from GMT
EPOCH = 1970 EPOCH = 1970
def timegm(tuple): def timegm(tuple):
"""Unrelated but handy function to calculate Unix timestamp from GMT."""
year, month, day, hour, minute, second = tuple[:6] year, month, day, hour, minute, second = tuple[:6]
assert year >= EPOCH assert year >= EPOCH
assert 1 <= month <= 12 assert 1 <= month <= 12

View File

@ -1,39 +1,39 @@
# A generic class to build line-oriented command interpreters """A generic class to build line-oriented command interpreters.
#
# Interpreters constructed with this class obey the following conventions: Interpreters constructed with this class obey the following conventions:
#
# 1. End of file on input is processed as the command 'EOF'. 1. End of file on input is processed as the command 'EOF'.
# 2. A command is parsed out of each line by collecting the prefix composed 2. A command is parsed out of each line by collecting the prefix composed
# of characters in the identchars member. of characters in the identchars member.
# 3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method 3. A command `foo' is dispatched to a method 'do_foo()'; the do_ method
# is passed a single argument consisting of the remainder of the line. is passed a single argument consisting of the remainder of the line.
# 4. Typing an empty line repeats the last command. (Actually, it calls the 4. Typing an empty line repeats the last command. (Actually, it calls the
# method `emptyline', which may be overridden in a subclass.) method `emptyline', which may be overridden in a subclass.)
# 5. There is a predefined `help' method. Given an argument `topic', it 5. There is a predefined `help' method. Given an argument `topic', it
# calls the command `help_topic'. With no arguments, it lists all topics calls the command `help_topic'. With no arguments, it lists all topics
# with defined help_ functions, broken into up to three topics; documented with defined help_ functions, broken into up to three topics; documented
# commands, miscellaneous help topics, and undocumented commands. commands, miscellaneous help topics, and undocumented commands.
# 6. The command '?' is a synonym for `help'. The command '!' is a synonym 6. The command '?' is a synonym for `help'. The command '!' is a synonym
# for `shell', if a do_shell method exists. for `shell', if a do_shell method exists.
#
# The `default' method may be overridden to intercept commands for which there The `default' method may be overridden to intercept commands for which there
# is no do_ method. is no do_ method.
#
# The data member `self.ruler' sets the character used to draw separator lines The data member `self.ruler' sets the character used to draw separator lines
# in the help messages. If empty, no ruler line is drawn. It defaults to "=". in the help messages. If empty, no ruler line is drawn. It defaults to "=".
#
# If the value of `self.intro' is nonempty when the cmdloop method is called, If the value of `self.intro' is nonempty when the cmdloop method is called,
# it is printed out on interpreter startup. This value may be overridden it is printed out on interpreter startup. This value may be overridden
# via an optional argument to the cmdloop() method. via an optional argument to the cmdloop() method.
#
# The data members `self.doc_header', `self.misc_header', and The data members `self.doc_header', `self.misc_header', and
# `self.undoc_header' set the headers used for the help function's `self.undoc_header' set the headers used for the help function's
# listings of documented functions, miscellaneous topics, and undocumented listings of documented functions, miscellaneous topics, and undocumented
# functions respectively. functions respectively.
#
# These interpreters use raw_input; thus, if the readline module is loaded, These interpreters use raw_input; thus, if the readline module is loaded,
# they automatically support Emacs-like command history and editing features. they automatically support Emacs-like command history and editing features.
# """
import string import string

View File

@ -1,20 +1,20 @@
# Module 'cmp' """Efficiently compare files, boolean outcome only (equal / not equal).
# Efficiently compare files, boolean outcome only (equal / not equal). Tricks (used in this order):
- Files with identical type, size & mtime are assumed to be clones
# Tricks (used in this order): - Files with different type or size cannot be identical
# - Files with identical type, size & mtime are assumed to be clones - We keep a cache of outcomes of earlier comparisons
# - Files with different type or size cannot be identical - We don't fork a process to run 'cmp' but read the files ourselves
# - We keep a cache of outcomes of earlier comparisons """
# - We don't fork a process to run 'cmp' but read the files ourselves
import os import os
cache = {} cache = {}
def cmp(f1, f2, shallow=1): # Compare two files, use the cache if possible. def cmp(f1, f2, shallow=1):
# Return 1 for identical files, 0 for different. """Compare two files, use the cache if possible.
# Raise exceptions if either file could not be statted, read, etc. Return 1 for identical files, 0 for different.
Raise exceptions if either file could not be statted, read, etc."""
s1, s2 = sig(os.stat(f1)), sig(os.stat(f2)) s1, s2 = sig(os.stat(f1)), sig(os.stat(f2))
if s1[0] <> 8 or s2[0] <> 8: if s1[0] <> 8 or s2[0] <> 8:
# Either is a not a plain file -- always report as different # Either is a not a plain file -- always report as different
@ -42,15 +42,17 @@ def cmp(f1, f2, shallow=1): # Compare two files, use the cache if possible.
cache[key] = s1, s2, outcome cache[key] = s1, s2, outcome
return outcome return outcome
def sig(st): # Return signature (i.e., type, size, mtime) from raw stat data def sig(st):
# 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid """Return signature (i.e., type, size, mtime) from raw stat data
# 6-9: st_size, st_atime, st_mtime, st_ctime 0-5: st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid
6-9: st_size, st_atime, st_mtime, st_ctime"""
type = st[0] / 4096 type = st[0] / 4096
size = st[6] size = st[6]
mtime = st[8] mtime = st[8]
return type, size, mtime return type, size, mtime
def do_cmp(f1, f2): # Compare two files, really def do_cmp(f1, f2):
"""Compare two files, really."""
bufsize = 8*1024 # Could be tuned bufsize = 8*1024 # Could be tuned
fp1 = open(f1, 'rb') fp1 = open(f1, 'rb')
fp2 = open(f2, 'rb') fp2 = open(f2, 'rb')

View File

@ -1,13 +1,12 @@
# Module 'cmpcache' """Efficiently compare files, boolean outcome only (equal / not equal).
#
# Efficiently compare files, boolean outcome only (equal / not equal). Tricks (used in this order):
# - Use the statcache module to avoid statting files more than once
# Tricks (used in this order): - Files with identical type, size & mtime are assumed to be clones
# - Use the statcache module to avoid statting files more than once - Files with different type or size cannot be identical
# - Files with identical type, size & mtime are assumed to be clones - We keep a cache of outcomes of earlier comparisons
# - Files with different type or size cannot be identical - We don't fork a process to run 'cmp' but read the files ourselves
# - We keep a cache of outcomes of earlier comparisons """
# - We don't fork a process to run 'cmp' but read the files ourselves
import os import os
from stat import * from stat import *
@ -19,12 +18,11 @@ import statcache
cache = {} cache = {}
# Compare two files, use the cache if possible.
# May raise os.error if a stat or open of either fails.
#
def cmp(f1, f2, shallow=1): def cmp(f1, f2, shallow=1):
# Return 1 for identical files, 0 for different. """Compare two files, use the cache if possible.
# Raise exceptions if either file could not be statted, read, etc. May raise os.error if a stat or open of either fails.
Return 1 for identical files, 0 for different.
Raise exceptions if either file could not be statted, read, etc."""
s1, s2 = sig(statcache.stat(f1)), sig(statcache.stat(f2)) s1, s2 = sig(statcache.stat(f1)), sig(statcache.stat(f2))
if not S_ISREG(s1[0]) or not S_ISREG(s2[0]): if not S_ISREG(s1[0]) or not S_ISREG(s2[0]):
# Either is a not a plain file -- always report as different # Either is a not a plain file -- always report as different
@ -49,14 +47,12 @@ def cmp(f1, f2, shallow=1):
cache[key] = s1, s2, outcome cache[key] = s1, s2, outcome
return outcome return outcome
# Return signature (i.e., type, size, mtime) from raw stat data.
#
def sig(st): def sig(st):
"""Return signature (i.e., type, size, mtime) from raw stat data."""
return S_IFMT(st[ST_MODE]), st[ST_SIZE], st[ST_MTIME] return S_IFMT(st[ST_MODE]), st[ST_SIZE], st[ST_MTIME]
# Compare two files, really.
#
def do_cmp(f1, f2): def do_cmp(f1, f2):
"""Compare two files, really."""
#print ' cmp', f1, f2 # XXX remove when debugged #print ' cmp', f1, f2 # XXX remove when debugged
bufsize = 8*1024 # Could be tuned bufsize = 8*1024 # Could be tuned
fp1 = open(f1, 'rb') fp1 = open(f1, 'rb')

View File

@ -1,4 +1,4 @@
# Helper to provide extensibility for pickle/cPickle. """Helper to provide extensibility for pickle/cPickle."""
dispatch_table = {} dispatch_table = {}
safe_constructors = {} safe_constructors = {}

View File

@ -1,14 +1,13 @@
# Module 'dircache' """Return a sorted list of the files in a directory, using a cache
# to avoid reading the directory more often than necessary.
# Return a sorted list of the files in a directory, using a cache Also contains a subroutine to append slashes to directories."""
# to avoid reading the directory more often than necessary.
# Also contains a subroutine to append slashes to directories.
import os import os
cache = {} cache = {}
def listdir(path): # List directory contents, using cache def listdir(path):
"""List directory contents, using cache."""
try: try:
cached_mtime, list = cache[path] cached_mtime, list = cache[path]
del cache[path] del cache[path]
@ -29,7 +28,8 @@ def listdir(path): # List directory contents, using cache
opendir = listdir # XXX backward compatibility opendir = listdir # XXX backward compatibility
def annotate(head, list): # Add '/' suffixes to directories def annotate(head, list):
"""Add '/' suffixes to directories."""
for i in range(len(list)): for i in range(len(list)):
if os.path.isdir(os.path.join(head, list[i])): if os.path.isdir(os.path.join(head, list[i])):
list[i] = list[i] + '/' list[i] = list[i] + '/'

View File

@ -1,6 +1,4 @@
# Module 'dirmp' """A class to build directory diff tools on."""
#
# Defines a class to build directory diff tools on.
import os import os
@ -9,20 +7,21 @@ import cmpcache
import statcache import statcache
from stat import * from stat import *
# Directory comparison class.
#
class dircmp: class dircmp:
# """Directory comparison class."""
def new(self, a, b): # Initialize
def new(self, a, b):
"""Initialize."""
self.a = a self.a = a
self.b = b self.b = b
# Properties that caller may change before calling self.run(): # Properties that caller may change before calling self.run():
self.hide = [os.curdir, os.pardir] # Names never to be shown self.hide = [os.curdir, os.pardir] # Names never to be shown
self.ignore = ['RCS', 'tags'] # Names ignored in comparison self.ignore = ['RCS', 'tags'] # Names ignored in comparison
#
return self return self
#
def run(self): # Compare everything except common subdirectories def run(self):
"""Compare everything except common subdirectories."""
self.a_list = filter(dircache.listdir(self.a), self.hide) self.a_list = filter(dircache.listdir(self.a), self.hide)
self.b_list = filter(dircache.listdir(self.b), self.hide) self.b_list = filter(dircache.listdir(self.b), self.hide)
self.a_list.sort() self.a_list.sort()
@ -30,8 +29,9 @@ class dircmp:
self.phase1() self.phase1()
self.phase2() self.phase2()
self.phase3() self.phase3()
#
def phase1(self): # Compute common names def phase1(self):
"""Compute common names."""
self.a_only = [] self.a_only = []
self.common = [] self.common = []
for x in self.a_list: for x in self.a_list:
@ -39,21 +39,22 @@ class dircmp:
self.common.append(x) self.common.append(x)
else: else:
self.a_only.append(x) self.a_only.append(x)
#
self.b_only = [] self.b_only = []
for x in self.b_list: for x in self.b_list:
if x not in self.common: if x not in self.common:
self.b_only.append(x) self.b_only.append(x)
#
def phase2(self): # Distinguish files, directories, funnies def phase2(self):
"""Distinguish files, directories, funnies."""
self.common_dirs = [] self.common_dirs = []
self.common_files = [] self.common_files = []
self.common_funny = [] self.common_funny = []
#
for x in self.common: for x in self.common:
a_path = os.path.join(self.a, x) a_path = os.path.join(self.a, x)
b_path = os.path.join(self.b, x) b_path = os.path.join(self.b, x)
#
ok = 1 ok = 1
try: try:
a_stat = statcache.stat(a_path) a_stat = statcache.stat(a_path)
@ -65,7 +66,7 @@ class dircmp:
except os.error, why: except os.error, why:
# print 'Can\'t stat', b_path, ':', why[1] # print 'Can\'t stat', b_path, ':', why[1]
ok = 0 ok = 0
#
if ok: if ok:
a_type = S_IFMT(a_stat[ST_MODE]) a_type = S_IFMT(a_stat[ST_MODE])
b_type = S_IFMT(b_stat[ST_MODE]) b_type = S_IFMT(b_stat[ST_MODE])
@ -79,15 +80,17 @@ class dircmp:
self.common_funny.append(x) self.common_funny.append(x)
else: else:
self.common_funny.append(x) self.common_funny.append(x)
#
def phase3(self): # Find out differences between common files def phase3(self):
"""Find out differences between common files."""
xx = cmpfiles(self.a, self.b, self.common_files) xx = cmpfiles(self.a, self.b, self.common_files)
self.same_files, self.diff_files, self.funny_files = xx self.same_files, self.diff_files, self.funny_files = xx
#
def phase4(self): # Find out differences between common subdirectories def phase4(self):
# A new dircmp object is created for each common subdirectory, """Find out differences between common subdirectories.
# these are stored in a dictionary indexed by filename. A new dircmp object is created for each common subdirectory,
# The hide and ignore properties are inherited from the parent these are stored in a dictionary indexed by filename.
The hide and ignore properties are inherited from the parent."""
self.subdirs = {} self.subdirs = {}
for x in self.common_dirs: for x in self.common_dirs:
a_x = os.path.join(self.a, x) a_x = os.path.join(self.a, x)
@ -96,13 +99,15 @@ class dircmp:
newdd.hide = self.hide newdd.hide = self.hide
newdd.ignore = self.ignore newdd.ignore = self.ignore
newdd.run() newdd.run()
#
def phase4_closure(self): # Recursively call phase4() on subdirectories def phase4_closure(self):
"""Recursively call phase4() on subdirectories."""
self.phase4() self.phase4()
for x in self.subdirs.keys(): for x in self.subdirs.keys():
self.subdirs[x].phase4_closure() self.subdirs[x].phase4_closure()
#
def report(self): # Print a report on the differences between a and b def report(self):
"""Print a report on the differences between a and b."""
# Assume that phases 1 to 3 have been executed # Assume that phases 1 to 3 have been executed
# Output format is purposely lousy # Output format is purposely lousy
print 'diff', self.a, self.b print 'diff', self.a, self.b
@ -120,9 +125,10 @@ class dircmp:
print 'Common subdirectories :', self.common_dirs print 'Common subdirectories :', self.common_dirs
if self.common_funny: if self.common_funny:
print 'Common funny cases :', self.common_funny print 'Common funny cases :', self.common_funny
#
def report_closure(self): # Print reports on self and on subdirs def report_closure(self):
# If phase 4 hasn't been done, no subdir reports are printed """Print reports on self and on subdirs.
If phase 4 hasn't been done, no subdir reports are printed."""
self.report() self.report()
try: try:
x = self.subdirs x = self.subdirs
@ -131,8 +137,9 @@ class dircmp:
for x in self.subdirs.keys(): for x in self.subdirs.keys():
print print
self.subdirs[x].report_closure() self.subdirs[x].report_closure()
#
def report_phase4_closure(self): # Report and do phase 4 recursively def report_phase4_closure(self):
"""Report and do phase 4 recursively."""
self.report() self.report()
self.phase4() self.phase4()
for x in self.subdirs.keys(): for x in self.subdirs.keys():
@ -140,26 +147,26 @@ class dircmp:
self.subdirs[x].report_phase4_closure() self.subdirs[x].report_phase4_closure()
# Compare common files in two directories.
# Return:
# - files that compare equal
# - files that compare different
# - funny cases (can't stat etc.)
#
def cmpfiles(a, b, common): def cmpfiles(a, b, common):
"""Compare common files in two directories.
Return:
- files that compare equal
- files that compare different
- funny cases (can't stat etc.)"""
res = ([], [], []) res = ([], [], [])
for x in common: for x in common:
res[cmp(os.path.join(a, x), os.path.join(b, x))].append(x) res[cmp(os.path.join(a, x), os.path.join(b, x))].append(x)
return res return res
# Compare two files.
# Return:
# 0 for equal
# 1 for different
# 2 for funny cases (can't stat, etc.)
#
def cmp(a, b): def cmp(a, b):
"""Compare two files.
Return:
0 for equal
1 for different
2 for funny cases (can't stat, etc.)"""
try: try:
if cmpcache.cmp(a, b): return 0 if cmpcache.cmp(a, b): return 0
return 1 return 1
@ -167,28 +174,18 @@ def cmp(a, b):
return 2 return 2
# Remove a list item.
# NB: This modifies the list argument.
#
def remove(list, item):
for i in range(len(list)):
if list[i] == item:
del list[i]
break
# Return a copy with items that occur in skip removed.
#
def filter(list, skip): def filter(list, skip):
"""Return a copy with items that occur in skip removed."""
result = [] result = []
for item in list: for item in list:
if item not in skip: result.append(item) if item not in skip: result.append(item)
return result return result
# Demonstration and testing.
#
def demo(): def demo():
"""Demonstration and testing."""
import sys import sys
import getopt import getopt
options, args = getopt.getopt(sys.argv[1:], 'r') options, args = getopt.getopt(sys.argv[1:], 'r')
@ -200,4 +197,5 @@ def demo():
else: else:
dd.report() dd.report()
# demo() if __name__ == "__main__":
demo()

View File

@ -1,15 +1,15 @@
# General floating point formatting functions. """General floating point formatting functions.
# Functions: Functions:
# fix(x, digits_behind) fix(x, digits_behind)
# sci(x, digits_behind) sci(x, digits_behind)
# Each takes a number or a string and a number of digits as arguments. Each takes a number or a string and a number of digits as arguments.
# Parameters:
# x: number to be formatted; or a string resembling a number
# digits_behind: number of digits behind the decimal point
Parameters:
x: number to be formatted; or a string resembling a number
digits_behind: number of digits behind the decimal point
"""
import re import re