2003-03-12 07:26:59 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-03-12 07:26:59 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
2003-08-23 07:53:20 +02:00
|
|
|
"""
|
|
|
|
Fixes stuff that Python should have but doesn't.
|
|
|
|
"""
|
|
|
|
|
2004-04-17 18:35:31 +02:00
|
|
|
from __future__ import division
|
|
|
|
|
2003-11-25 18:33:58 +01:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2003-10-08 22:57:29 +02:00
|
|
|
__all__ = []
|
|
|
|
|
2004-04-20 12:36:53 +02:00
|
|
|
exported = ['ignore', 'reversed', 'window', 'group',
|
2004-04-20 11:44:58 +02:00
|
|
|
'partition', 'any', 'all', 'rsplit']
|
2003-10-04 16:58:04 +02:00
|
|
|
|
2004-08-30 22:06:05 +02:00
|
|
|
import sys
|
2003-11-04 09:26:54 +01:00
|
|
|
import new
|
2004-09-06 10:00:18 +02:00
|
|
|
import atexit
|
2003-09-04 22:42:37 +02:00
|
|
|
import string
|
2003-03-12 07:26:59 +01:00
|
|
|
string.ascii = string.maketrans('', '')
|
|
|
|
|
2004-04-17 18:35:31 +02:00
|
|
|
import random
|
|
|
|
_choice = random.choice
|
|
|
|
def choice(iterable):
|
2004-10-10 05:55:29 +02:00
|
|
|
if isinstance(iterable, (list, tuple)):
|
2004-04-17 18:35:31 +02:00
|
|
|
return _choice(iterable)
|
|
|
|
else:
|
|
|
|
n = 1
|
2004-10-10 05:39:59 +02:00
|
|
|
m = new.module('') # Guaranteed unique value.
|
|
|
|
ret = m
|
2004-04-17 18:35:31 +02:00
|
|
|
for x in iterable:
|
|
|
|
if random.random() < 1/n:
|
|
|
|
ret = x
|
|
|
|
n += 1
|
2004-10-10 05:39:59 +02:00
|
|
|
if ret is m:
|
|
|
|
raise IndexError
|
2004-04-17 18:35:31 +02:00
|
|
|
return ret
|
|
|
|
random.choice = choice
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def ignore(*args, **kwargs):
|
|
|
|
"""Simply ignore the arguments sent to it."""
|
|
|
|
pass
|
|
|
|
|
2004-04-20 11:51:20 +02:00
|
|
|
def reversed(L):
|
|
|
|
"""Iterates through a sequence in reverse."""
|
2003-03-12 07:26:59 +01:00
|
|
|
for i in xrange(len(L) - 1, -1, -1):
|
|
|
|
yield L[i]
|
|
|
|
|
|
|
|
def window(L, size):
|
2003-08-23 07:53:20 +02:00
|
|
|
"""Returns a sliding 'window' through the list L of size size."""
|
2004-09-12 22:28:30 +02:00
|
|
|
assert not isinstance(L, int), 'Argument order swapped: window(L, size)'
|
2003-04-03 10:17:57 +02:00
|
|
|
if size < 1:
|
2004-08-19 01:15:27 +02:00
|
|
|
raise ValueError, 'size <= 0 disallowed.'
|
2003-03-12 07:26:59 +01:00
|
|
|
for i in xrange(len(L) - (size-1)):
|
|
|
|
yield L[i:i+size]
|
2003-03-25 21:21:12 +01:00
|
|
|
|
2003-09-04 22:42:37 +02:00
|
|
|
import itertools
|
2004-03-30 10:32:55 +02:00
|
|
|
def ilen(iterable):
|
2003-08-23 07:53:20 +02:00
|
|
|
"""Returns the length of an iterator."""
|
2003-08-07 08:13:11 +02:00
|
|
|
i = 0
|
2004-03-30 10:32:55 +02:00
|
|
|
for _ in iterable:
|
2003-08-07 08:13:11 +02:00
|
|
|
i += 1
|
|
|
|
return i
|
2004-03-30 10:32:55 +02:00
|
|
|
|
|
|
|
def trueCycle(iterable):
|
|
|
|
while 1:
|
|
|
|
yielded = False
|
|
|
|
for x in iterable:
|
|
|
|
yield x
|
|
|
|
yielded = True
|
|
|
|
if not yielded:
|
|
|
|
raise StopIteration
|
|
|
|
|
|
|
|
itertools.trueCycle = trueCycle
|
2003-09-04 22:42:37 +02:00
|
|
|
itertools.ilen = ilen
|
2003-08-07 08:13:11 +02:00
|
|
|
|
2004-02-21 11:29:10 +01:00
|
|
|
def groupby(key, iterable):
|
|
|
|
if key is None:
|
|
|
|
key = lambda x: x
|
|
|
|
it = iter(iterable)
|
|
|
|
value = it.next() # If there are no items, this takes an early exit
|
|
|
|
oldkey = key(value)
|
|
|
|
group = [value]
|
|
|
|
for value in it:
|
|
|
|
newkey = key(value)
|
|
|
|
if newkey != oldkey:
|
|
|
|
yield group
|
|
|
|
group = []
|
|
|
|
oldkey = newkey
|
|
|
|
group.append(value)
|
|
|
|
yield group
|
|
|
|
itertools.groupby = groupby
|
|
|
|
|
2003-08-17 08:24:58 +02:00
|
|
|
def group(seq, groupSize, noneFill=True):
|
2003-08-23 07:53:20 +02:00
|
|
|
"""Groups a given sequence into sublists of length groupSize."""
|
2003-08-11 09:12:41 +02:00
|
|
|
ret = []
|
|
|
|
L = []
|
|
|
|
i = groupSize
|
|
|
|
for elt in seq:
|
|
|
|
if i > 0:
|
|
|
|
L.append(elt)
|
|
|
|
else:
|
|
|
|
ret.append(L)
|
|
|
|
i = groupSize
|
|
|
|
L = []
|
|
|
|
L.append(elt)
|
|
|
|
i -= 1
|
|
|
|
if L:
|
2003-08-17 08:24:58 +02:00
|
|
|
if noneFill:
|
|
|
|
while len(L) < groupSize:
|
|
|
|
L.append(None)
|
2003-08-11 09:12:41 +02:00
|
|
|
ret.append(L)
|
|
|
|
return ret
|
2003-07-31 08:20:58 +02:00
|
|
|
|
2003-04-20 19:17:50 +02:00
|
|
|
def partition(p, L):
|
2003-08-23 07:53:20 +02:00
|
|
|
"""Partitions a list L based on a predicate p. Returns a (yes,no) tuple"""
|
2003-04-20 19:17:50 +02:00
|
|
|
no = []
|
|
|
|
yes = []
|
|
|
|
for elt in L:
|
|
|
|
if p(elt):
|
|
|
|
yes.append(elt)
|
|
|
|
else:
|
|
|
|
no.append(elt)
|
|
|
|
return (yes, no)
|
2003-07-30 23:47:21 +02:00
|
|
|
|
2003-08-23 14:08:14 +02:00
|
|
|
def any(p, seq):
|
|
|
|
"""Returns true if any element in seq satisfies predicate p."""
|
2003-10-11 05:20:51 +02:00
|
|
|
for elt in itertools.ifilter(p, seq):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2003-08-23 14:08:14 +02:00
|
|
|
|
|
|
|
def all(p, seq):
|
|
|
|
"""Returns true if all elements in seq satisfy predicate p."""
|
2003-10-11 05:20:51 +02:00
|
|
|
for elt in itertools.ifilterfalse(p, seq):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2003-08-23 14:08:14 +02:00
|
|
|
|
2003-09-22 11:13:40 +02:00
|
|
|
def rsplit(s, sep=None, maxsplit=-1):
|
2003-10-03 02:08:30 +02:00
|
|
|
"""Equivalent to str.split, except splitting from the right."""
|
2003-09-22 15:04:15 +02:00
|
|
|
if sep is not None:
|
|
|
|
sep = sep[::-1]
|
2003-09-22 11:13:40 +02:00
|
|
|
L = s[::-1].split(sep, maxsplit)
|
|
|
|
L.reverse()
|
|
|
|
return [s[::-1] for s in L]
|
|
|
|
|
2004-03-01 19:15:21 +01:00
|
|
|
import operator
|
|
|
|
def itemgetter(i):
|
|
|
|
return lambda x: x[i]
|
|
|
|
|
|
|
|
def attrgetter(attr):
|
|
|
|
return lambda x: getattr(x, attr)
|
|
|
|
operator.itemgetter = itemgetter
|
|
|
|
operator.attrgetter = attrgetter
|
|
|
|
|
2004-08-05 05:59:40 +02:00
|
|
|
import csv
|
|
|
|
import cStringIO as StringIO
|
|
|
|
def join(L):
|
|
|
|
fd = StringIO.StringIO()
|
|
|
|
writer = csv.writer(fd)
|
|
|
|
writer.writerow(L)
|
|
|
|
return fd.getvalue().rstrip('\r\n')
|
|
|
|
|
|
|
|
def split(s):
|
|
|
|
fd = StringIO.StringIO(s)
|
|
|
|
reader = csv.reader(fd)
|
|
|
|
return reader.next()
|
|
|
|
csv.join = join
|
|
|
|
csv.split = split
|
|
|
|
|
2004-08-26 23:49:40 +02:00
|
|
|
import socket
|
|
|
|
# Some socket modules don't have sslerror, so we'll just make it an error.
|
|
|
|
if not hasattr(socket, 'sslerror'):
|
|
|
|
socket.sslerror = socket.error
|
2004-08-30 22:06:05 +02:00
|
|
|
|
|
|
|
# Trick to get an __revision__ attribute in all exceptions, if possible.
|
2004-09-01 14:33:22 +02:00
|
|
|
if not hasattr(Exception, '_original__init__'):
|
|
|
|
Exception._original__init__ = Exception.__init__
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# We had sys._getframe(1) here for awhile, but in the interactive
|
|
|
|
# interpreter, there isn't enough call stack. Thanks, Rafterman, for
|
|
|
|
# pointing that out.
|
|
|
|
me = sys._getframe(0)
|
|
|
|
caller = me.f_back
|
|
|
|
callerGlobals = getattr(caller, 'f_globals', {})
|
|
|
|
callerModule = sys.modules.get(callerGlobals.get('__name__'))
|
|
|
|
self.__revision__ = getattr(callerModule, '__revision__', None)
|
|
|
|
self._original__init__(*args, **kwargs)
|
|
|
|
Exception.__init__ = __init__
|
2004-09-01 14:56:19 +02:00
|
|
|
|
2004-09-06 10:00:18 +02:00
|
|
|
# Apparently, some things get dismantled a bit early, and we can get stuck in
|
|
|
|
# a non-exiting loop because our Exception.__init__ raises an exception. This
|
|
|
|
# makes sure that we replace our __init__ with the original before things like
|
|
|
|
# the sys module are dismantled.
|
2004-09-06 02:51:53 +02:00
|
|
|
def _replace_original__init__():
|
|
|
|
Exception.__init__ = Exception._original__init__
|
2004-09-06 02:54:27 +02:00
|
|
|
atexit.register(_replace_original__init__)
|
|
|
|
|
|
|
|
g = globals()
|
2004-09-01 14:56:19 +02:00
|
|
|
|
2003-10-08 22:57:29 +02:00
|
|
|
for name in exported:
|
2004-09-01 14:56:19 +02:00
|
|
|
__builtins__[name] = g[name]
|
2003-10-05 14:47:19 +02:00
|
|
|
|
2003-09-05 20:54:35 +02:00
|
|
|
|
2004-08-05 05:59:40 +02:00
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
2003-10-05 14:47:19 +02:00
|
|
|
|