Removed comments; removed zipiter (no one used it, and itertools.izip is available now). Added docstrings to all remaining non-docstringed functions.

This commit is contained in:
Jeremy Fincher 2003-08-23 05:53:20 +00:00
parent b86205ae8b
commit bdd66dbd5e

View File

@ -31,6 +31,10 @@
## from __future__ import generators ## from __future__ import generators
"""
Fixes stuff that Python should have but doesn't.
"""
import sys import sys
import string import string
@ -49,110 +53,6 @@ def catch(f, *args, **kwargs):
except: except:
return None return None
## class bool(int):
## """2.3 came out."""
## def __new__(cls, val=0):
## # This constructor always returns an existing instance
## if val:
## return True
## else:
## return False
## def __repr__(self):
## if self:
## return "True"
## else:
## return "False"
## __str__ = __repr__
## def __and__(self, other):
## if isinstance(other, bool):
## return bool(int(self) & int(other))
## else:
## return int.__and__(self, other)
## __rand__ = __and__
## def __or__(self, other):
## if isinstance(other, bool):
## return bool(int(self) | int(other))
## else:
## return int.__or__(self, other)
## __ror__ = __or__
## def __xor__(self, other):
## if isinstance(other, bool):
## return bool(int(self) ^ int(other))
## else:
## return int.__xor__(self, other)
## __rxor__ = __xor__
## False = int.__new__(bool, 0)
## True = int.__new__(bool, 1)
## class set(object):
## """2.3 came out."""
## __slots__ = ('d',)
## def __init__(self, seq=()):
## self.d = {}
## for x in seq:
## self.d[x] = None
## def __contains__(self, x):
## return x in self.d
## def __iter__(self):
## return self.d.iterkeys()
## def __repr__(self):
## return '%s([%s])' % (self.__class__.__name__,
## ', '.join(map(repr, self.d.iterkeys())))
## def __nonzero__(self):
## if self.d:
## return True
## else:
## return False
## def __getstate__(self):
## return (self.d.keys(),)
## def __setstate__(self, (L,)):
## self.d = {}
## for x in L:
## self.d[x] = None
## def __len__(self):
## return len(self.d)
## def add(self, x):
## self.d[x] = None
## def remove(self, x):
## del self.d[x]
## def discard(self, x):
## try:
## del self.d[x]
## except KeyError:
## pass
## def __eq__(self, other):
## return self.d == other.d
## def __ne__(self, other):
## return not self.d == other.d
## ## def __getstate__(self):
## ## return self.d
## ## def __setstate__(self, d):
## ## self.d = d
class IterableMap(object): class IterableMap(object):
"""Define .iteritems() in a class and subclass this to get the other iters. """Define .iteritems() in a class and subclass this to get the other iters.
@ -205,31 +105,27 @@ def mktemp(suffix=''):
s = m.hexdigest() s = m.hexdigest()
return sha.sha(s + str(time.time())).hexdigest() + suffix return sha.sha(s + str(time.time())).hexdigest() + suffix
def zipiter(*args):
args = map(iter, args)
while 1:
L = []
for arg in args:
L.append(arg.next())
yield tuple(L)
def reviter(L): def reviter(L):
"""Iterates through a list in reverse."""
for i in xrange(len(L) - 1, -1, -1): for i in xrange(len(L) - 1, -1, -1):
yield L[i] yield L[i]
def window(L, size): def window(L, size):
"""Returns a sliding 'window' through the list L of size size."""
if size < 1: if size < 1:
raise ValueError, 'size <= 0 unallowed.' raise ValueError, 'size <= 0 unallowed.'
for i in xrange(len(L) - (size-1)): for i in xrange(len(L) - (size-1)):
yield L[i:i+size] yield L[i:i+size]
def ilen(iterator): def ilen(iterator):
"""Returns the length of an iterator."""
i = 0 i = 0
for _ in iterator: for _ in iterator:
i += 1 i += 1
return i return i
def group(seq, groupSize, noneFill=True): def group(seq, groupSize, noneFill=True):
"""Groups a given sequence into sublists of length groupSize."""
ret = [] ret = []
L = [] L = []
i = groupSize i = groupSize
@ -250,6 +146,7 @@ def group(seq, groupSize, noneFill=True):
return ret return ret
def itersplit(iterable, isSeparator, yieldEmpty=False): def itersplit(iterable, isSeparator, yieldEmpty=False):
"""Splits an iterator based on a predicate isSeparator."""
acc = [] acc = []
for element in iterable: for element in iterable:
if isSeparator(element): if isSeparator(element):
@ -262,6 +159,8 @@ def itersplit(iterable, isSeparator, yieldEmpty=False):
yield acc yield acc
def flatten(seq, strings=False): def flatten(seq, strings=False):
"""Flattens a list of lists into a single list. See the test for examples.
"""
for elt in seq: for elt in seq:
if not strings and type(elt) == str or type(elt) == unicode: if not strings and type(elt) == str or type(elt) == unicode:
yield elt yield elt
@ -273,6 +172,7 @@ def flatten(seq, strings=False):
yield elt yield elt
def partition(p, L): def partition(p, L):
"""Partitions a list L based on a predicate p. Returns a (yes,no) tuple"""
no = [] no = []
yes = [] yes = []
for elt in L: for elt in L:
@ -283,6 +183,7 @@ def partition(p, L):
return (yes, no) return (yes, no)
def flip((x, y)): def flip((x, y)):
"""Flips a two-tuple around. (x, y) becomes (y, x)."""
return (y, x) return (y, x)
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: