Kaydet (Commit) e7b146fb authored tarafından Guido van Rossum's avatar Guido van Rossum

The third and final doc-string sweep by Ka-Ping Yee.

The attached patches update the standard library so that all modules
have docstrings beginning with one-line summaries.

A new docstring was added to formatter.  The docstring for os.py
was updated to mention nt, os2, ce in addition to posix, dos, mac.
üst 54f22ed3
#! /usr/bin/env python
# Conversions to/from quoted-printable transport encoding as per RFC-1521
"""Conversions to/from quoted-printable transport encoding as per RFC-1521."""
# (Dec 1991 version).
ESCAPE = '='
......@@ -8,11 +9,15 @@ MAXLINESIZE = 76
HEX = '0123456789ABCDEF'
def needsquoting(c, quotetabs):
"""Decide whether a particular character needs to be quoted.
The 'quotetabs' flag indicates whether tabs should be quoted."""
if c == '\t':
return not quotetabs
return c == ESCAPE or not(' ' <= c <= '~')
def quote(c):
"""Quote a single character."""
if c == ESCAPE:
return ESCAPE * 2
else:
......@@ -20,6 +25,10 @@ def quote(c):
return ESCAPE + HEX[i/16] + HEX[i%16]
def encode(input, output, quotetabs):
"""Read 'input', apply quoted-printable encoding, and write to 'output'.
'input' and 'output' are files with readline() and write() methods.
The 'quotetabs' flag indicates whether tabs should be quoted."""
while 1:
line = input.readline()
if not line: break
......@@ -42,6 +51,9 @@ def encode(input, output, quotetabs):
output.write(new + '\n')
def decode(input, output):
"""Read 'input', apply quoted-printable decoding, and write to 'output'.
'input' and 'output' are files with readline() and write() methods."""
new = ''
while 1:
line = input.readline()
......@@ -73,9 +85,11 @@ def decode(input, output):
output.write(new)
def ishex(c):
"""Return true if the character 'c' is a hexadecimal digit."""
return '0' <= c <= '9' or 'a' <= c <= 'f' or 'A' <= c <= 'F'
def unhex(s):
"""Get the integer value of a hexadecimal number."""
bits = 0
for c in s:
if '0' <= c <= '9':
......
# R A N D O M V A R I A B L E G E N E R A T O R S
#
# distributions on the real line:
# ------------------------------
# normal (Gaussian)
# lognormal
# negative exponential
# gamma
# beta
#
# distributions on the circle (angles 0 to 2pi)
# ---------------------------------------------
# circular uniform
# von Mises
# Translated from anonymously contributed C/C++ source.
# Multi-threading note: the random number generator used here is not
# thread-safe; it is possible that two calls return the same random
# value. See whrandom.py for more info.
"""Random variable generators.
distributions on the real line:
------------------------------
normal (Gaussian)
lognormal
negative exponential
gamma
beta
distributions on the circle (angles 0 to 2pi)
---------------------------------------------
circular uniform
von Mises
Translated from anonymously contributed C/C++ source.
Multi-threading note: the random number generator used here is not
thread-safe; it is possible that two calls return the same random
value. See whrandom.py for more info.
"""
import whrandom
from whrandom import random, uniform, randint, choice, randrange # For export!
......
# These bits are passed to regex.set_syntax() to choose among
# alternative regexp syntaxes.
"""Constants for selecting regexp syntaxes for the obsolete regex module.
This module is only for backward compatibility. "regex" has now
been replaced by the new regular expression module, "re".
These bits are passed to regex.set_syntax() to choose among
alternative regexp syntaxes.
"""
# 1 means plain parentheses serve as grouping, and backslash
# parentheses are needed for literal searching.
......
# Regular expression subroutines:
# sub(pat, repl, str): replace first occurrence of pattern in string
# gsub(pat, repl, str): replace all occurrences of pattern in string
# split(str, pat, maxsplit): split string using pattern as delimiter
# splitx(str, pat, maxsplit): split string using pattern as delimiter plus
# return delimiters
"""Regexp-based split and replace using the obsolete regex module.
This module is only for backward compatibility. These operations
are now provided by the new regular expression module, "re".
sub(pat, repl, str): replace first occurrence of pattern in string
gsub(pat, repl, str): replace all occurrences of pattern in string
split(str, pat, maxsplit): split string using pattern as delimiter
splitx(str, pat, maxsplit): split string using pattern as delimiter plus
return delimiters
"""
import regex
......
# Redo the `...` (representation) but with limits on most sizes.
"""Redo the `...` (representation) but with limits on most sizes."""
import string
......
# A parser for SGML, using the derived class as static DTD.
"""A parser for SGML, using the derived class as a static DTD."""
# XXX This only supports those SGML features used by HTML.
......
"""A lexical analyzer class for simple shell-like syntaxes."""
# Module and documentation by Eric S. Raymond, 21 Dec 1998
import sys
......
"""Utility functions for copying files.
"""Utility functions for copying files and directory trees.
XXX The functions here don't copy the resource fork or other metadata on Mac.
......
# Module 'stat'
#
# Defines constants and functions for interpreting stat/lstat struct
# as returned by os.stat() and os.lstat() (if it exists).
#
# Suggested usage: from stat import *
#
"""Constants/functions for interpreting results of os.stat() and os.lstat().
Suggested usage: from stat import *
"""
# XXX Strictly spoken, this module may have to be adapted for each POSIX
# implementation; in practice, however, the numeric constants used by
# stat() are almost universal (even for stat() emulations on non-UNIX
......
# Module 'statcache'
#
# Maintain a cache of file stats.
# There are functions to reset the cache or to selectively remove items.
"""Maintain a cache of file stats.
There are functions to reset the cache or to selectively remove items.
"""
import os
from stat import *
......@@ -12,42 +11,37 @@ from stat import *
cache = {}
# Stat a file, possibly out of the cache.
#
def stat(path):
"""Stat a file, possibly out of the cache."""
if cache.has_key(path):
return cache[path]
cache[path] = ret = os.stat(path)
return ret
# Reset the cache completely.
#
def reset():
"""Reset the cache completely."""
global cache
cache = {}
# Remove a given item from the cache, if it exists.
#
def forget(path):
"""Remove a given item from the cache, if it exists."""
if cache.has_key(path):
del cache[path]
# Remove all pathnames with a given prefix.
#
def forget_prefix(prefix):
"""Remove all pathnames with a given prefix."""
n = len(prefix)
for path in cache.keys():
if path[:n] == prefix:
del cache[path]
# Forget about a directory and all entries in it, but not about
# entries in subdirectories.
#
def forget_dir(prefix):
"""Forget about a directory and all entries in it, but not about
entries in subdirectories."""
if prefix[-1:] == '/' and prefix <> '/':
prefix = prefix[:-1]
forget(prefix)
......@@ -62,19 +56,17 @@ def forget_dir(prefix):
del cache[path]
# Remove all pathnames except with a given prefix.
# Normally used with prefix = '/' after a chdir().
#
def forget_except_prefix(prefix):
"""Remove all pathnames except with a given prefix.
Normally used with prefix = '/' after a chdir()."""
n = len(prefix)
for path in cache.keys():
if path[:n] <> prefix:
del cache[path]
# Check for directory.
#
def isdir(path):
"""Check for directory."""
try:
st = stat(path)
except os.error:
......
# Module 'statvfs'
#
# Defines constants for interpreting statvfs struct as returned
# by os.statvfs() and os.fstatvfs() (if they exist).
#
"""Constants for interpreting the results of os.statvfs() and os.fstatvfs()."""
# Indices for statvfs struct members in the tuple returned by
# os.statvfs() and os.fstatvfs().
......
# module 'string' -- A collection of string operations
"""A collection of string operations (most are no longer used in Python 1.6).
# Warning: most of the code you see here isn't normally used nowadays. With
# Python 1.6, many of these functions are implemented as methods on the
# standard string object. They used to be implemented by a built-in module
# called strop, but strop is now obsolete itself.
"""Common string manipulations.
Warning: most of the code you see here isn't normally used nowadays. With
Python 1.6, many of these functions are implemented as methods on the
standard string object. They used to be implemented by a built-in module
called strop, but strop is now obsolete itself.
Public module variables:
......
# Stuff to parse Sun and NeXT audio files.
#
# An audio consists of a header followed by the data. The structure
# of the header is as follows.
#
# +---------------+
# | magic word |
# +---------------+
# | header size |
# +---------------+
# | data size |
# +---------------+
# | encoding |
# +---------------+
# | sample rate |
# +---------------+
# | # of channels |
# +---------------+
# | info |
# | |
# +---------------+
#
# The magic word consists of the 4 characters '.snd'. Apart from the
# info field, all header fields are 4 bytes in size. They are all
# 32-bit unsigned integers encoded in big-endian byte order.
#
# The header size really gives the start of the data.
# The data size is the physical size of the data. From the other
# parameter the number of frames can be calculated.
# The encoding gives the way in which audio samples are encoded.
# Possible values are listed below.
# The info field currently consists of an ASCII string giving a
# human-readable description of the audio file. The info field is
# padded with NUL bytes to the header size.
#
# Usage.
#
# Reading audio files:
# f = sunau.open(file, 'r')
# 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().
# When the setpos() and rewind() methods are not used, the seek()
# method is not necessary.
#
# This returns an instance of a class with the following public methods:
# getnchannels() -- returns number of audio channels (1 for
# mono, 2 for stereo)
# getsampwidth() -- returns sample width in bytes
# getframerate() -- returns sampling frequency
# getnframes() -- returns number of audio frames
# getcomptype() -- returns compression type ('NONE' or 'ULAW')
# getcompname() -- returns human-readable version of
# compression type ('not compressed' matches 'NONE')
# getparams() -- returns a tuple consisting of all of the
# above in the above order
# getmarkers() -- returns None (for compatibility with the
# aifc module)
# getmark(id) -- raises an error since the mark does not
# exist (for compatibility with the aifc module)
# readframes(n) -- returns at most n frames of audio
# rewind() -- rewind to the beginning of the audio stream
# setpos(pos) -- seek to the specified position
# tell() -- return the current position
# close() -- close the instance (make it unusable)
# The position returned by tell() and the position given to setpos()
# are compatible and have nothing to do with the actual postion in the
# file.
# The close() method is called automatically when the class instance
# is destroyed.
#
# Writing audio files:
# f = sunau.open(file, 'w')
# 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
# close().
#
# This returns an instance of a class with the following public methods:
# setnchannels(n) -- set the number of channels
# setsampwidth(n) -- set the sample width
# setframerate(n) -- set the frame rate
# setnframes(n) -- set the number of frames
# setcomptype(type, name)
# -- set the compression type and the
# human-readable compression type
# setparams(tuple)-- set all parameters at once
# tell() -- return current position in output file
# writeframesraw(data)
# -- write audio frames without pathing up the
# file header
# writeframes(data)
# -- write audio frames and patch up the file header
# close() -- patch up the file header and close the
# output file
# You should set the parameters before the first writeframesraw or
# 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
# be patched up.
# It is best to first set all parameters, perhaps possibly the
# compression type, and then write audio frames using writeframesraw.
# When all frames have been written, either call writeframes('') or
# close() to patch up the sizes in the header.
# The close() method is called automatically when the class instance
# is destroyed.
"""Stuff to parse Sun and NeXT audio files.
An audio consists of a header followed by the data. The structure
of the header is as follows.
+---------------+
| magic word |
+---------------+
| header size |
+---------------+
| data size |
+---------------+
| encoding |
+---------------+
| sample rate |
+---------------+
| # of channels |
+---------------+
| info |
| |
+---------------+
The magic word consists of the 4 characters '.snd'. Apart from the
info field, all header fields are 4 bytes in size. They are all
32-bit unsigned integers encoded in big-endian byte order.
The header size really gives the start of the data.
The data size is the physical size of the data. From the other
parameter the number of frames can be calculated.
The encoding gives the way in which audio samples are encoded.
Possible values are listed below.
The info field currently consists of an ASCII string giving a
human-readable description of the audio file. The info field is
padded with NUL bytes to the header size.
Usage.
Reading audio files:
f = sunau.open(file, 'r')
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().
When the setpos() and rewind() methods are not used, the seek()
method is not necessary.
This returns an instance of a class with the following public methods:
getnchannels() -- returns number of audio channels (1 for
mono, 2 for stereo)
getsampwidth() -- returns sample width in bytes
getframerate() -- returns sampling frequency
getnframes() -- returns number of audio frames
getcomptype() -- returns compression type ('NONE' or 'ULAW')
getcompname() -- returns human-readable version of
compression type ('not compressed' matches 'NONE')
getparams() -- returns a tuple consisting of all of the
above in the above order
getmarkers() -- returns None (for compatibility with the
aifc module)
getmark(id) -- raises an error since the mark does not
exist (for compatibility with the aifc module)
readframes(n) -- returns at most n frames of audio
rewind() -- rewind to the beginning of the audio stream
setpos(pos) -- seek to the specified position
tell() -- return the current position
close() -- close the instance (make it unusable)
The position returned by tell() and the position given to setpos()
are compatible and have nothing to do with the actual postion in the
file.
The close() method is called automatically when the class instance
is destroyed.
Writing audio files:
f = sunau.open(file, 'w')
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
close().
This returns an instance of a class with the following public methods:
setnchannels(n) -- set the number of channels
setsampwidth(n) -- set the sample width
setframerate(n) -- set the frame rate
setnframes(n) -- set the number of frames
setcomptype(type, name)
-- set the compression type and the
human-readable compression type
setparams(tuple)-- set all parameters at once
tell() -- return current position in output file
writeframesraw(data)
-- write audio frames without pathing up the
file header
writeframes(data)
-- write audio frames and patch up the file header
close() -- patch up the file header and close the
output file
You should set the parameters before the first writeframesraw or
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
be patched up.
It is best to first set all parameters, perhaps possibly the
compression type, and then write audio frames using writeframesraw.
When all frames have been written, either call writeframes('') or
close() to patch up the sizes in the header.
The close() method is called automatically when the class instance
is destroyed.
"""
# from <multimedia/audio_filehdr.h>
AUDIO_FILE_MAGIC = 0x2e736e64
......
# Module 'sunaudio' -- interpret sun audio headers
"""Interpret sun audio headers."""
MAGIC = '.snd'
error = 'sunaudio sound header conversion error'
# convert a 4-char value to integer
def get_long_be(s):
"""Convert a 4-char value to integer."""
return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
# read a sound header from an open file
def gethdr(fp):
"""Read a sound header from an open file."""
if fp.read(4) <> MAGIC:
raise error, 'gethdr: bad magic word'
hdr_size = get_long_be(fp.read(4))
......@@ -31,9 +29,8 @@ def gethdr(fp):
return (data_size, encoding, sample_rate, channels, info)
# read and print the sound header of a named file
def printhdr(file):
"""Read and print the sound header of a named file."""
hdr = gethdr(open(file, 'r'))
data_size, encoding, sample_rate, channels, info = hdr
while info[-1:] == '\0':
......
#! /usr/bin/env python
#
# Non-terminal symbols of Python grammar (from "graminit.h")
#
"""Non-terminal symbols of Python grammar (from "graminit.h")."""
# This file is automatically generated; please don't muck it up!
#
# To update the symbols in this file, 'cd' to the top directory of
......
# Temporary file name allocation
#
"""Temporary files and filenames."""
# XXX This tries to be not UNIX specific, but I don't know beans about
# how to choose a temp directory or filename on MS-DOS or other
# systems so it may have to be changed...
......@@ -14,9 +14,8 @@ tempdir = None
template = None
# Function to calculate the directory to use
def gettempdir():
"""Function to calculate the directory to use."""
global tempdir
if tempdir is not None:
return tempdir
......@@ -58,11 +57,10 @@ def gettempdir():
return tempdir
# Function to calculate a prefix of the filename to use
_pid = None
def gettempprefix():
"""Function to calculate a prefix of the filename to use."""
global template, _pid
if os.name == 'posix' and _pid and _pid != os.getpid():
# Our pid changed; we must have forked -- zap the template
......@@ -85,9 +83,8 @@ def gettempprefix():
counter = 0
# User-callable function to return a unique temporary file name
def mktemp(suffix=""):
"""User-callable function to return a unique temporary file name."""
global counter
dir = gettempdir()
pre = gettempprefix()
......@@ -126,6 +123,7 @@ class TemporaryFileWrapper:
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
"""Create and return a temporary file (opened read-write by default)."""
name = mktemp(suffix)
if os.name == 'posix':
# Unix -- be very careful
......
# threading.py:
# Proposed new threading module, emulating a subset of Java's threading model
"""Proposed new threading module, emulating a subset of Java's threading model."""
import sys
import time
......
# Convert "arbitrary" sound files to AIFF files (Apple and SGI's audio format).
# Input may be compressed.
# Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
# An exception is raised if the file is not of a recognized type.
# Returned filename is either the input filename or a temporary filename;
# in the latter case the caller must ensure that it is removed.
# Other temporary files used are removed by the function.
"""Convert "arbitrary" sound files to AIFF (Apple and SGI's audio format).
Input may be compressed.
Uncompressed file type may be AIFF, WAV, VOC, 8SVX, NeXT/Sun, and others.
An exception is raised if the file is not of a recognized type.
Returned filename is either the input filename or a temporary filename;
in the latter case the caller must ensure that it is removed.
Other temporary files used are removed by the function.
"""
import os
import tempfile
......
#! /usr/bin/env python
#
# Tokens (from "token.h")
#
"""Token constants (from "token.h")."""
# This file is automatically generated; please don't muck it up!
#
# To update the symbols in this file, 'cd' to the top directory of
......
# Format and print Python stack traces
"""Extract, format and print information about Python stack traces."""
import linecache
import string
......@@ -10,6 +10,8 @@ def _print(file, str='', terminator='\n'):
def print_list(extracted_list, file=None):
"""Print the list of tuples as returned by extract_tb() or
extract_stack() as a formatted stack trace to the given file."""
if not file:
file = sys.stderr
for filename, lineno, name, line in extracted_list:
......@@ -19,6 +21,12 @@ def print_list(extracted_list, file=None):
_print(file, ' %s' % string.strip(line))
def format_list(extracted_list):
"""Given a list of tuples as returned by extract_tb() or
extract_stack(), return a list of strings ready for printing.
Each string in the resulting list corresponds to the item with
the same index in the argument list. Each string ends in a
newline; the strings may contain internal newlines as well, for
those items whose source text line is not None."""
list = []
for filename, lineno, name, line in extracted_list:
item = ' File "%s", line %d, in %s\n' % (filename,lineno,name)
......@@ -29,6 +37,10 @@ def format_list(extracted_list):
def print_tb(tb, limit=None, file=None):
"""Print up to 'limit' stack trace entries from the traceback 'tb'.
If 'limit' is omitted or None, all entries are printed. If 'file' is
omitted or None, the output goes to sys.stderr; otherwise 'file'
should be an open file or file-like object with a write() method."""
if not file:
file = sys.stderr
if limit is None:
......@@ -49,9 +61,18 @@ def print_tb(tb, limit=None, file=None):
n = n+1
def format_tb(tb, limit = None):
"""A shorthand for 'format_list(extract_stack(f, limit))."""
return format_list(extract_tb(tb, limit))
def extract_tb(tb, limit = None):
"""Return a list of up to 'limit' pre-processed stack trace entries
extracted from the traceback object 'traceback'. This is useful for
alternate formatting of stack traces. If 'limit' is omitted or None,
all entries are extracted. A pre-processed stack trace entry is a
quadruple (filename, line number, function name, text) representing
the information that is usually printed for a stack trace. The text
is a string with leading and trailing whitespace stripped; if the
source is not available it is None."""
if limit is None:
if hasattr(sys, 'tracebacklimit'):
limit = sys.tracebacklimit
......@@ -73,6 +94,14 @@ def extract_tb(tb, limit = None):
def print_exception(etype, value, tb, limit=None, file=None):
"""Print exception information and up to 'limit' stack trace entries
from the traceback 'tb' to 'file'. This differs from print_tb() in
the following ways: (1) if traceback is not None, it prints a header
"Traceback (innermost last):"; (2) it prints the exception type and
value after the stack trace; (3) if type is SyntaxError and value has
the appropriate format, it prints the line where the syntax error
occurred with a caret on the next line indicating the approximate
position of the error."""
if not file:
file = sys.stderr
if tb:
......@@ -84,6 +113,12 @@ def print_exception(etype, value, tb, limit=None, file=None):
_print(file, lines[-1], '')
def format_exception(etype, value, tb, limit = None):
"""Format a stack trace and the exception information. The arguments
have the same meaning as the corresponding arguments to
print_exception(). The return value is a list of strings, each
ending in a newline and some containing internal newlines. When
these lines are contatenated and printed, exactly the same text is
printed as does print_exception()."""
if tb:
list = ['Traceback (innermost last):\n']
list = list + format_tb(tb, limit)
......@@ -93,6 +128,14 @@ def format_exception(etype, value, tb, limit = None):
return list
def format_exception_only(etype, value):
"""Format the exception part of a traceback. The arguments are the
exception type and value such as given by sys.last_type and
sys.last_value. The return value is a list of strings, each ending
in a newline. Normally, the list contains a single string;
however, for SyntaxError exceptions, it contains several lines that
(when printed) display detailed information about where the syntax
error occurred. The message indicating which exception occurred is
the always last string in the list."""
list = []
if type(etype) == types.ClassType:
stype = etype.__name__
......@@ -128,6 +171,10 @@ def format_exception_only(etype, value):
def print_exc(limit=None, file=None):
"""This is a shorthand for 'print_exception(sys.exc_type,
sys.exc_value, sys.exc_traceback, limit, file)'.
(In fact, it uses sys.exc_info() to retrieve the same information
in a thread-safe way.)"""
if not file:
file = sys.stderr
try:
......@@ -137,6 +184,8 @@ def print_exc(limit=None, file=None):
etype = value = tb = None
def print_last(limit=None, file=None):
"""This is a shorthand for 'print_exception(sys.last_type,
sys.last_value, sys.last_traceback, limit, file)'."""
if not file:
file = sys.stderr
print_exception(sys.last_type, sys.last_value, sys.last_traceback,
......@@ -144,6 +193,10 @@ def print_last(limit=None, file=None):
def print_stack(f=None, limit=None, file=None):
"""This function prints a stack trace from its invocation point.
The optional 'f' argument can be used to specify an alternate stack
frame at which to start. The optional 'limit' and 'file' arguments
have the same meaning as for print_exception()."""
if f is None:
try:
raise ZeroDivisionError
......@@ -152,6 +205,7 @@ def print_stack(f=None, limit=None, file=None):
print_list(extract_stack(f, limit), file)
def format_stack(f=None, limit=None):
"""A shorthand for 'format_list(extract_stack(f, limit))'."""
if f is None:
try:
raise ZeroDivisionError
......@@ -160,6 +214,12 @@ def format_stack(f=None, limit=None):
return format_list(extract_stack(f, limit))
def extract_stack(f=None, limit = None):
"""Extract the raw traceback from the current stack frame. The
return value has the same format as for extract_tb(). The optional
'f' and 'limit' arguments have the same meaning as for print_stack().
Each item in the list is a quadruple (filename, line number,
function name, text), and the entries are in order from outermost
to innermost stack frame."""
if f is None:
try:
raise ZeroDivisionError
......@@ -184,13 +244,14 @@ def extract_stack(f=None, limit = None):
list.reverse()
return list
# Calculate the correct line number of the traceback given in tb (even
# with -O on).
# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
# in compile.c.
# Revised version by Jim Hugunin to work with JPython too.
def tb_lineno(tb):
"""Calculate the correct line number of the traceback given in tb
(even with -O on)."""
# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
# in compile.c.
# Revised version by Jim Hugunin to work with JPython too.
c = tb.tb_frame.f_code
if not hasattr(c, 'co_lnotab'):
return tb.tb_lineno
......
# tty.py -- Terminal utilities.
"""Terminal utilities."""
# Author: Steen Lumholt.
from TERMIOS import *
......@@ -13,8 +14,8 @@ ISPEED = 4
OSPEED = 5
CC = 6
# Put terminal into a raw mode.
def setraw(fd, when=TCSAFLUSH):
"""Put terminal into a raw mode."""
mode = tcgetattr(fd)
mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON)
mode[OFLAG] = mode[OFLAG] & ~(OPOST)
......@@ -25,8 +26,8 @@ def setraw(fd, when=TCSAFLUSH):
mode[CC][VTIME] = 0
tcsetattr(fd, when, mode)
# Put terminal into a cbreak mode.
def setcbreak(fd, when=TCSAFLUSH):
"""Put terminal into a cbreak mode."""
mode = tcgetattr(fd)
mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON)
mode[CC][VMIN] = 1
......
# Define names for all type symbols known in the standard interpreter.
# Types that are part of optional modules (e.g. array) are not listed.
"""Define names for all type symbols known in the standard interpreter.
Types that are part of optional modules (e.g. array) are not listed.
"""
import sys
......
# Parse a timezone specification.
"""Parse a timezone specification."""
# XXX Unfinished.
# XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.
......@@ -8,6 +9,12 @@ tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
tzprog = None
def tzparse(tzstr):
"""Given a timezone spec, return a tuple of information
(tzname, delta, dstname, daystart, hourstart, dayend, hourend),
where 'tzname' is the name of the timezone, 'delta' is the offset
in hours from GMT, 'dstname' is the name of the daylight-saving
timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
specify the starting and ending points for daylight saving time."""
global tzprog
if tzprog == None:
import re
......@@ -24,6 +31,9 @@ def tzparse(tzstr):
return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
def tzlocaltime(secs, params):
"""Given a Unix time in seconds and a tuple of information about
a timezone as returned by tzparse(), return the local time in the
form (year, month, day, hour, min, sec, yday, wday, tzname)."""
import time
(tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
year, month, days, hours, mins, secs, yday, wday, isdst = \
......@@ -34,6 +44,7 @@ def tzlocaltime(secs, params):
return year, month, days, hours, mins, secs, yday, wday, tzname
def tzset():
"""Determine the current timezone from the "TZ" environment variable."""
global tzparams, timezone, altzone, daylight, tzname
import os
tzstr = os.environ['TZ']
......@@ -44,6 +55,8 @@ def tzset():
tzname = tzparams[0], tzparams[2]
def isdst(secs):
"""Return true if daylight-saving time is in effect for the given
Unix time in the current timezone."""
import time
(tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
tzparams
......@@ -54,6 +67,7 @@ def isdst(secs):
tzset()
def localtime(secs):
"""Get the local time in the current timezone."""
return tzlocaltime(secs, tzparams)
def test():
......
This diff is collapsed.
"""An extensible library for opening URLs using a variety protocols
"""An extensible library for opening URLs using a variety of protocols
The simplest way to use this module is to call the urlopen function,
which accepts a string containing a URL or a Request object (described
......
# Parse (absolute and relative) URLs. See RFC 1808: "Relative Uniform
# Resource Locators", by R. Fielding, UC Irvine, June 1995.
"""Parse (absolute and relative) URLs.
See RFC 1808: "Relative Uniform Resource Locators", by R. Fielding,
UC Irvine, June 1995.
"""
# Standard/builtin Python modules
import string
......@@ -39,12 +42,12 @@ def clear_cache():
_parse_cache = {}
# Parse a URL into 6 components:
# <scheme>://<netloc>/<path>;<params>?<query>#<fragment>
# Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
# Note that we don't break the components up in smaller bits
# (e.g. netloc is a single string) and we don't expand % escapes.
def urlparse(url, scheme = '', allow_fragments = 1):
"""Parse a URL into 6 components:
<scheme>://<netloc>/<path>;<params>?<query>#<fragment>
Return a 6-tuple: (scheme, netloc, path, params, query, fragment).
Note that we don't break the components up in smaller bits
(e.g. netloc is a single string) and we don't expand % escapes."""
key = url, scheme, allow_fragments
cached = _parse_cache.get(key, None)
if cached:
......@@ -107,11 +110,11 @@ def urlparse(url, scheme = '', allow_fragments = 1):
_parse_cache[key] = tuple
return tuple
# Put a parsed URL back together again. This may result in a slightly
# different, but equivalent URL, if the URL that was parsed originally
# had redundant delimiters, e.g. a ? with an empty query (the draft
# states that these are equivalent).
def urlunparse((scheme, netloc, url, params, query, fragment)):
"""Put a parsed URL back together again. This may result in a
slightly different, but equivalent URL, if the URL that was parsed
originally had redundant delimiters, e.g. a ? with an empty query
(the draft states that these are equivalent)."""
if netloc or (scheme in uses_netloc and url[:2] == '//'):
if url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url
......@@ -125,9 +128,9 @@ def urlunparse((scheme, netloc, url, params, query, fragment)):
url = url + '#' + fragment
return url
# Join a base URL and a possibly relative URL to form an absolute
# interpretation of the latter.
def urljoin(base, url, allow_fragments = 1):
"""Join a base URL and a possibly relative URL to form an absolute
interpretation of the latter."""
if not base:
return url
bscheme, bnetloc, bpath, bparams, bquery, bfragment = \
......
......@@ -23,11 +23,12 @@
# between ascii and binary. This results in a 1000-fold speedup. The C
# version is still 5 times faster, though.
# - Arguments more compliant with python standard
#
# This file implements the UUencode and UUdecode functions.
# encode(in_file, out_file [,name, mode])
# decode(in_file [, out_file, mode])
"""Implementation of the UUencode and UUdecode functions.
encode(in_file, out_file [,name, mode])
decode(in_file [, out_file, mode])
"""
import binascii
import os
......
This diff is collapsed.
# WICHMANN-HILL RANDOM NUMBER GENERATOR
#
# Wichmann, B. A. & Hill, I. D. (1982)
# Algorithm AS 183:
# An efficient and portable pseudo-random number generator
# Applied Statistics 31 (1982) 188-190
#
# see also:
# Correction to Algorithm AS 183
# Applied Statistics 33 (1984) 123
#
# McLeod, A. I. (1985)
# A remark on Algorithm AS 183
# Applied Statistics 34 (1985),198-200
#
#
# USE:
# whrandom.random() yields double precision random numbers
# uniformly distributed between 0 and 1.
#
# whrandom.seed(x, y, z) must be called before whrandom.random()
# to seed the generator
#
# There is also an interface to create multiple independent
# random generators, and to choose from other ranges.
# Translated by Guido van Rossum from C source provided by
# Adrian Baddeley.
# Multi-threading note: the random number generator used here is not
# thread-safe; it is possible that nearly simultaneous calls in
# different theads return the same random value. To avoid this, you
# have to use a lock around all calls. (I didn't want to slow this
# down in the serial case by using a lock here.)
"""Wichman-Hill random number generator.
Wichmann, B. A. & Hill, I. D. (1982)
Algorithm AS 183:
An efficient and portable pseudo-random number generator
Applied Statistics 31 (1982) 188-190
see also:
Correction to Algorithm AS 183
Applied Statistics 33 (1984) 123
McLeod, A. I. (1985)
A remark on Algorithm AS 183
Applied Statistics 34 (1985),198-200
USE:
whrandom.random() yields double precision random numbers
uniformly distributed between 0 and 1.
whrandom.seed(x, y, z) must be called before whrandom.random()
to seed the generator
There is also an interface to create multiple independent
random generators, and to choose from other ranges.
Translated by Guido van Rossum from C source provided by
Adrian Baddeley.
Multi-threading note: the random number generator used here is not
thread-safe; it is possible that nearly simultaneous calls in
different theads return the same random value. To avoid this, you
have to use a lock around all calls. (I didn't want to slow this
down in the serial case by using a lock here.)
"""
class whrandom:
#
# Initialize an instance.
# Without arguments, initialize from current time.
# With arguments (x, y, z), initialize from them.
#
def __init__(self, x = 0, y = 0, z = 0):
"""Initialize an instance.
Without arguments, initialize from current time.
With arguments (x, y, z), initialize from them."""
self.seed(x, y, z)
#
# Set the seed from (x, y, z).
# These must be integers in the range [0, 256).
#
def seed(self, x = 0, y = 0, z = 0):
"""Set the seed from (x, y, z).
These must be integers in the range [0, 256)."""
if not type(x) == type(y) == type(z) == type(0):
raise TypeError, 'seeds must be integers'
if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
......@@ -63,10 +61,9 @@ class whrandom:
t, z = divmod(t, 256)
# Zero is a poor seed, so substitute 1
self._seed = (x or 1, y or 1, z or 1)
#
# Get the next random number in the range [0.0, 1.0).
#
def random(self):
"""Get the next random number in the range [0.0, 1.0)."""
# This part is thread-unsafe:
# BEGIN CRITICAL SECTION
x, y, z = self._seed
......@@ -79,30 +76,25 @@ class whrandom:
# END CRITICAL SECTION
#
return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
#
# Get a random number in the range [a, b).
#
def uniform(self, a, b):
"""Get a random number in the range [a, b)."""
return a + (b-a) * self.random()
#
# Get a random integer in the range [a, b] including both end points.
# (Deprecated; use randrange below.)
#
def randint(self, a, b):
"""Get a random integer in the range [a, b] including both end points.
(Deprecated; use randrange below.)"""
return self.randrange(a, b+1)
#
# Choose a random element from a non-empty sequence.
#
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
return seq[int(self.random() * len(seq))]
#
# Choose a random item from range([start,] step[, stop]).
# This fixes the problem with randint() which includes the
# endpoint; in Python this is usually not what you want.
#
def randrange(self, start, stop=None, step=1,
# Do not supply the following arguments
int=int, default=None):
def randrange(self, start, stop=None, step=1, int=int, default=None):
"""Choose a random item from range([start,] step[, stop]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int' and 'default' arguments."""
# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking
istart = int(start)
......@@ -136,7 +128,6 @@ class whrandom:
# Initialize from the current time
#
_inst = whrandom()
seed = _inst.seed
random = _inst.random
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment