2003-03-12 07:26:59 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
from __future__ import generators
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
|
|
|
|
sys.path.insert(0, 'others')
|
|
|
|
|
|
|
|
string.ascii = string.maketrans('', '')
|
|
|
|
|
|
|
|
def ignore(*args, **kwargs):
|
|
|
|
"""Simply ignore the arguments sent to it."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def catch(f, *args, **kwargs):
|
2003-04-09 20:12:38 +02:00
|
|
|
"""Catches all exceptions raises by f."""
|
2003-03-12 07:26:59 +01:00
|
|
|
try:
|
2003-04-03 08:57:01 +02:00
|
|
|
return f(*args, **kwargs)
|
2003-03-12 07:26:59 +01:00
|
|
|
except:
|
2003-04-03 09:00:51 +02:00
|
|
|
return None
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
class bool(int):
|
2003-04-09 20:12:38 +02:00
|
|
|
"""Just a holdover until 2.3 comes out with its wonderful new bool type."""
|
|
|
|
def __new__(cls, val=0):
|
2003-03-12 07:26:59 +01:00
|
|
|
# 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):
|
2003-04-09 20:12:38 +02:00
|
|
|
"""Just a holdover until 2.3 comes out with its wonderful new set type."""
|
2003-04-05 12:26:26 +02:00
|
|
|
__slots__ = ('d',)
|
|
|
|
def __init__(self, seq=()):
|
2003-03-12 07:26:59 +01:00
|
|
|
self.d = {}
|
2003-04-05 12:26:26 +02:00
|
|
|
for x in seq:
|
2003-03-12 07:26:59 +01:00
|
|
|
self.d[x] = None
|
|
|
|
|
|
|
|
def __contains__(self, x):
|
|
|
|
return x in self.d
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self.d.iterkeys()
|
|
|
|
|
|
|
|
def __repr__(self):
|
2003-04-05 12:26:26 +02:00
|
|
|
return '%s([%s])' % (self.__class__.__name__,
|
|
|
|
', '.join(map(repr, self.d.iterkeys())))
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
if self.d:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
return self.d.keys()
|
|
|
|
|
|
|
|
def __setstate__(self, t):
|
|
|
|
for x in t:
|
|
|
|
self.d[x] = None
|
|
|
|
|
2003-04-04 08:15:34 +02:00
|
|
|
def __len__(self):
|
|
|
|
return len(self.d)
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def add(self, x):
|
|
|
|
self.d[x] = None
|
|
|
|
|
|
|
|
def remove(self, x):
|
2003-04-03 10:17:57 +02:00
|
|
|
del self.d[x]
|
|
|
|
|
|
|
|
def discard(self, x):
|
|
|
|
try:
|
2003-03-12 07:26:59 +01:00
|
|
|
del self.d[x]
|
2003-04-03 10:17:57 +02:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-09 20:57:25 +02:00
|
|
|
def __getstate__(self):
|
|
|
|
return self.d
|
|
|
|
|
|
|
|
def __setstate__(self, d):
|
|
|
|
self.d = d
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
|
2003-04-02 09:01:36 +02:00
|
|
|
class queue(dict):
|
2003-04-09 20:12:38 +02:00
|
|
|
"""An FIFO Queue, O(1) for all operations."""
|
2003-04-02 09:01:36 +02:00
|
|
|
__slots__ = ('first', 'last')
|
2003-04-05 12:26:26 +02:00
|
|
|
def __init__(self, seq=()):
|
2003-04-02 09:01:36 +02:00
|
|
|
self.first = 0
|
|
|
|
self.last = 0
|
2003-04-05 12:26:26 +02:00
|
|
|
for elt in seq:
|
2003-04-02 11:19:26 +02:00
|
|
|
self.enqueue(elt)
|
2003-04-02 09:01:36 +02:00
|
|
|
|
|
|
|
def enqueue(self, elt):
|
2003-04-05 12:26:26 +02:00
|
|
|
dict.__setitem__(self, self.last, elt)
|
2003-04-02 09:01:36 +02:00
|
|
|
self.last += 1
|
|
|
|
|
|
|
|
def peek(self):
|
|
|
|
try:
|
2003-04-05 12:26:26 +02:00
|
|
|
return dict.__getitem__(self, self.first)
|
2003-04-02 09:01:36 +02:00
|
|
|
except KeyError:
|
|
|
|
raise IndexError, 'peek into empty queue'
|
|
|
|
|
|
|
|
def dequeue(self):
|
|
|
|
try:
|
2003-04-05 12:26:26 +02:00
|
|
|
ret = dict.__getitem__(self, self.first)
|
|
|
|
dict.__delitem__(self, self.first)
|
2003-04-02 09:01:36 +02:00
|
|
|
self.first += 1
|
|
|
|
return ret
|
|
|
|
except KeyError:
|
|
|
|
raise IndexError, 'dequeue from empty queue'
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.last - self.first
|
|
|
|
|
|
|
|
def __contains__(self, elt):
|
|
|
|
return elt in self.itervalues()
|
2003-04-02 11:19:26 +02:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for i in xrange(self.first, self.last):
|
2003-04-05 12:26:26 +02:00
|
|
|
yield dict.__getitem__(self, i)
|
2003-04-02 11:19:26 +02:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if len(self) == len(other):
|
|
|
|
for (x, y) in zip(self, other):
|
|
|
|
if x != y:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __repr__(self):
|
2003-04-05 12:26:26 +02:00
|
|
|
return 'queue([%s])' % ', '.join(map(repr, self.itervalues()))
|
2003-04-02 11:19:26 +02:00
|
|
|
|
2003-04-05 12:26:26 +02:00
|
|
|
def __getitem__(self, i):
|
|
|
|
try:
|
|
|
|
if i >= 0:
|
|
|
|
return dict.__getitem__(self, i+self.first)
|
|
|
|
else:
|
|
|
|
return dict.__getitem__(self, i+self.last)
|
|
|
|
except KeyError:
|
|
|
|
raise IndexError, i
|
|
|
|
|
|
|
|
def __setitem__(self, i, v):
|
|
|
|
try:
|
|
|
|
if i >= 0:
|
|
|
|
dict.__setitem__(self, i+self.first, v)
|
|
|
|
else:
|
|
|
|
dict.__setitem__(self, i+self.last, v)
|
|
|
|
except KeyError:
|
|
|
|
raise IndexError, i
|
2003-04-05 16:00:40 +02:00
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
return (self.first, self.last, dict(self))
|
|
|
|
|
|
|
|
def __setstate__(self, (first, last, d)):
|
|
|
|
self.first = first
|
|
|
|
self.last = last
|
|
|
|
for (k, v) in d.iteritems():
|
|
|
|
dict.__setitem__(self, k, v)
|
2003-04-05 12:26:26 +02:00
|
|
|
|
2003-04-09 20:12:38 +02:00
|
|
|
class MaxLengthQueue(queue):
|
|
|
|
__slots__ = ('length',)
|
2003-04-09 20:15:05 +02:00
|
|
|
def __init__(self, length, *args):
|
2003-04-09 20:12:38 +02:00
|
|
|
self.length = length
|
|
|
|
queue.__init__(self, *args)
|
2003-04-09 20:16:39 +02:00
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
return (self.length, queue.__getstate__(self))
|
|
|
|
|
|
|
|
def __setstate__(self, (length, q)):
|
|
|
|
self.length = length
|
2003-04-09 20:18:14 +02:00
|
|
|
queue.__setstate__(self, q)
|
2003-04-09 20:12:38 +02:00
|
|
|
|
|
|
|
def enqueue(self, elt):
|
2003-04-09 20:35:59 +02:00
|
|
|
queue.enqueue(self, elt)
|
2003-04-09 20:12:38 +02:00
|
|
|
if len(self) > self.length:
|
|
|
|
self.dequeue()
|
2003-04-02 09:01:36 +02:00
|
|
|
|
2003-04-09 20:35:59 +02:00
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
class IterableMap(object):
|
|
|
|
"""Define .iteritems() in a class and subclass this to get the other iters.
|
|
|
|
"""
|
|
|
|
def iteritems(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
|
|
for (key, _) in self.iteritems():
|
|
|
|
yield key
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
|
|
for (_, value) in self.iteritems():
|
|
|
|
yield value
|
|
|
|
|
|
|
|
def items(self):
|
2003-04-05 12:26:26 +02:00
|
|
|
return list(self.iteritems())
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def keys(self):
|
2003-04-05 12:26:26 +02:00
|
|
|
return list(self.iterkeys())
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def values(self):
|
2003-04-05 12:26:26 +02:00
|
|
|
return list(self.itervalues())
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
ret = 0
|
|
|
|
for _ in self.iteritems():
|
|
|
|
ret += 1
|
|
|
|
return ret
|
|
|
|
|
2003-04-05 12:26:26 +02:00
|
|
|
def __nonzero__(self):
|
|
|
|
for _ in self.iteritems():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2003-03-12 07:26:59 +01:00
|
|
|
def mktemp(suffix=''):
|
2003-04-09 20:12:38 +02:00
|
|
|
"""Gives a decent random string, suitable for a filename."""
|
2003-03-12 07:26:59 +01:00
|
|
|
import sha
|
|
|
|
import md5
|
|
|
|
import time
|
|
|
|
import random
|
|
|
|
r = random.Random()
|
|
|
|
m = md5.md5(suffix)
|
|
|
|
r.seed(time.time())
|
|
|
|
s = str(r.getstate())
|
|
|
|
for x in xrange(0, random.randrange(400), random.randrange(1, 5)):
|
|
|
|
m.update(str(x))
|
|
|
|
m.update(s)
|
|
|
|
m.update(str(time.time()))
|
|
|
|
s = m.hexdigest()
|
|
|
|
return sha.sha(s + str(time.time())).hexdigest() + suffix
|
|
|
|
|
|
|
|
def zipiter(*args):
|
2003-04-09 20:12:38 +02:00
|
|
|
args = map(iter, args)
|
|
|
|
while 1:
|
|
|
|
L = []
|
|
|
|
for arg in args:
|
|
|
|
L.append(arg.next())
|
|
|
|
yield tuple(L)
|
2003-03-12 07:26:59 +01:00
|
|
|
|
|
|
|
def reviter(L):
|
|
|
|
for i in xrange(len(L) - 1, -1, -1):
|
|
|
|
yield L[i]
|
|
|
|
|
|
|
|
def enumerate(L):
|
|
|
|
for i in xrange(len(L)):
|
|
|
|
yield (i, L[i])
|
|
|
|
|
|
|
|
def window(L, size):
|
2003-04-03 10:17:57 +02:00
|
|
|
if size < 1:
|
|
|
|
raise ValueError, 'size <= 0 unallowed.'
|
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
|
|
|
|
|
|
|
def itersplit(iterable, isSeparator, yieldEmpty=False):
|
|
|
|
acc = []
|
|
|
|
for element in iterable:
|
|
|
|
if isSeparator(element):
|
2003-03-28 03:34:03 +01:00
|
|
|
if acc or yieldEmpty:
|
2003-03-25 21:21:12 +01:00
|
|
|
yield acc
|
|
|
|
acc = []
|
|
|
|
else:
|
|
|
|
acc.append(element)
|
2003-03-28 03:34:03 +01:00
|
|
|
if acc or yieldEmpty:
|
2003-03-25 21:21:12 +01:00
|
|
|
yield acc
|
|
|
|
|
2003-04-05 15:35:25 +02:00
|
|
|
def flatten(seq, strings=False):
|
|
|
|
for elt in seq:
|
|
|
|
if not strings and type(elt) == str or type(elt) == unicode:
|
|
|
|
yield elt
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for x in flatten(elt):
|
|
|
|
yield x
|
|
|
|
except TypeError:
|
|
|
|
yield elt
|
2003-04-11 09:10:55 +02:00
|
|
|
|
2003-04-20 19:17:50 +02:00
|
|
|
def partition(p, L):
|
|
|
|
no = []
|
|
|
|
yes = []
|
|
|
|
for elt in L:
|
|
|
|
if p(elt):
|
|
|
|
yes.append(elt)
|
|
|
|
else:
|
|
|
|
no.append(elt)
|
|
|
|
return (yes, no)
|
|
|
|
|
2003-03-24 09:41:19 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|