From f65829b768b37eeb73daae8ab76ef2bb4daec3ca Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 4 Sep 2003 20:42:37 +0000 Subject: [PATCH] Moved several things from fix.py to utils.py. --- plugins/Utilities.py | 1 - plugins/baseplugin.py | 3 +- src/cdb.py | 9 +++-- src/fix.py | 88 ++----------------------------------------- src/irclib.py | 4 +- src/utils.py | 79 ++++++++++++++++++++++++++++++++++++++ test/test_Gameknot.py | 10 +++-- test/test_Notes.py | 8 ++-- test/test_fix.py | 67 +++++++------------------------- test/test_utils.py | 59 +++++++++++++++++++++++++++++ 10 files changed, 175 insertions(+), 153 deletions(-) diff --git a/plugins/Utilities.py b/plugins/Utilities.py index f35246b89..8ca813616 100644 --- a/plugins/Utilities.py +++ b/plugins/Utilities.py @@ -95,7 +95,6 @@ class Utilities(callbacks.Privmsg): Joins all the arguments together with . """ sep = args.pop(0) - args = flatten(map(callbacks.tokenize, args)) irc.reply(msg, sep.join(args)) def strtranslate(self, irc, msg, args): diff --git a/plugins/baseplugin.py b/plugins/baseplugin.py index d2343974f..6217b7c7d 100644 --- a/plugins/baseplugin.py +++ b/plugins/baseplugin.py @@ -18,6 +18,7 @@ import threading import cdb import conf import debug +import utils import world import ircutils @@ -104,7 +105,7 @@ class PeriodicFileDownloader(object): def _downloadFile(self, filename, url, f): infd = urllib2.urlopen(url) - newFilename = mktemp() + newFilename = utils.mktemp() outfd = file(newFilename, 'wb') start = time.time() s = infd.read(4096) diff --git a/src/cdb.py b/src/cdb.py index 92f836a0c..9afa96cc5 100644 --- a/src/cdb.py +++ b/src/cdb.py @@ -40,6 +40,7 @@ import struct import os.path import cPickle as pickle +import utils def hash(s): h = 5381 @@ -166,7 +167,7 @@ class Maker(object): self.fd.write(pack2Ints(hashPos, hashLen)) -class Reader(IterableMap): +class Reader(utils.IterableMap): def __init__(self, filename): self.filename = filename self.fd = file(filename, 'r') @@ -263,7 +264,7 @@ class Reader(IterableMap): __getitem__ = find -class ReaderWriter(IterableMap): +class ReaderWriter(utils.IterableMap): def __init__(self, filename, journalName=None, maxmods=0): if journalName is None: journalName = filename + '.journal' @@ -315,7 +316,7 @@ class ReaderWriter(IterableMap): except IOError: pass if removals or adds: - tempfilename = mktemp('.db') + tempfilename = utils.mktemp('.db') maker = Maker(tempfilename) cdb = Reader(self.filename) for (key, value) in cdb.iteritems(): @@ -423,7 +424,7 @@ class ReaderWriter(IterableMap): return default -class Shelf(ReaderWriter, IterableMap): +class Shelf(ReaderWriter, utils.IterableMap): def __getitem__(self, key): return pickle.loads(ReaderWriter.__getitem__(self, key)) diff --git a/src/fix.py b/src/fix.py index 15f53f466..28c1dd5de 100644 --- a/src/fix.py +++ b/src/fix.py @@ -36,11 +36,11 @@ Fixes stuff that Python should have but doesn't. """ import sys -import string if 'others' not in sys.path: sys.path.insert(0, 'others') +import string string.ascii = string.maketrans('', '') def ignore(*args, **kwargs): @@ -54,58 +54,6 @@ def catch(f, *args, **kwargs): except: return None - -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): - return list(self.iteritems()) - - def keys(self): - return list(self.iterkeys()) - - def values(self): - return list(self.itervalues()) - - def __len__(self): - ret = 0 - for _ in self.iteritems(): - ret += 1 - return ret - - def __nonzero__(self): - for _ in self.iteritems(): - return True - return False - -def mktemp(suffix=''): - """Gives a decent random string, suitable for a filename.""" - 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 reviter(L): """Iterates through a list in reverse.""" for i in xrange(len(L) - 1, -1, -1): @@ -118,12 +66,15 @@ def window(L, size): for i in xrange(len(L) - (size-1)): yield L[i:i+size] +import itertools def ilen(iterator): """Returns the length of an iterator.""" i = 0 for _ in iterator: i += 1 return i +itertools.ilen = ilen +del ilen def group(seq, groupSize, noneFill=True): """Groups a given sequence into sublists of length groupSize.""" @@ -146,32 +97,6 @@ def group(seq, groupSize, noneFill=True): ret.append(L) return ret -def itersplit(iterable, isSeparator, yieldEmpty=False): - """Splits an iterator based on a predicate isSeparator.""" - acc = [] - for element in iterable: - if isSeparator(element): - if acc or yieldEmpty: - yield acc - acc = [] - else: - acc.append(element) - if acc or yieldEmpty: - yield acc - -def flatten(seq, strings=False): - """Flattens a list of lists into a single list. See the test for examples. - """ - 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 - def partition(p, L): """Partitions a list L based on a predicate p. Returns a (yes,no) tuple""" no = [] @@ -183,11 +108,6 @@ def partition(p, L): no.append(elt) return (yes, no) -def flip((x, y)): - """Flips a two-tuple around. (x, y) becomes (y, x).""" - return (y, x) - - def any(p, seq): """Returns true if any element in seq satisfies predicate p.""" if p is None: diff --git a/src/irclib.py b/src/irclib.py index 7b71ad944..f2af6445f 100644 --- a/src/irclib.py +++ b/src/irclib.py @@ -39,6 +39,7 @@ import atexit import conf import debug +import utils import world import ircdb import ircmsgs @@ -472,7 +473,8 @@ class Irc(object): u.addHostmask(msg.prefix) ircdb.users.setUser(self.nick, u) else: - u = ircdb.IrcUser(capabilities=['owner'], password=mktemp(), + u = ircdb.IrcUser(capabilities=['owner'], + password=utils.mktemp(), hostmasks=[msg.prefix]) ircdb.users.setUser(self.nick, u) atexit.register(lambda: catch(ircdb.users.delUser(self.nick))) diff --git a/src/utils.py b/src/utils.py index 64aac7ea8..7eee74831 100755 --- a/src/utils.py +++ b/src/utils.py @@ -330,4 +330,83 @@ def sortBy(f, L, cmp=cmp): for (i, elt) in enumerate(L): L[i] = L[i][1] +def mktemp(suffix=''): + """Gives a decent random string, suitable for a filename.""" + 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 itersplit(isSeparator, iterable, maxsplit=-1, yieldEmpty=False): + """Splits an iterator based on a predicate isSeparator.""" + acc = [] + for element in iterable: + if maxsplit == 0 or not isSeparator(element): + acc.append(element) + else: + maxsplit -= 1 + if acc or yieldEmpty: + yield acc + acc = [] + if acc or yieldEmpty: + yield acc + +def flatten(seq, strings=False): + """Flattens a list of lists into a single list. See the test for examples. + """ + 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 + +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): + return list(self.iteritems()) + + def keys(self): + return list(self.iterkeys()) + + def values(self): + return list(self.itervalues()) + + def __len__(self): + ret = 0 + for _ in self.iteritems(): + ret += 1 + return ret + + def __nonzero__(self): + for _ in self.iteritems(): + return True + return False + + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/test/test_Gameknot.py b/test/test_Gameknot.py index 96e06e5f2..00cee4252 100644 --- a/test/test_Gameknot.py +++ b/test/test_Gameknot.py @@ -31,11 +31,13 @@ from test import * +import utils + class GameknotTestCase(PluginTestCase): plugins = ('Gameknot',) def testGkstats(self): self.assertNotError('gkstats jemfinch') - self.assertError('gkstats %s' % mktemp()) + self.assertError('gkstats %s' % utils.mktemp()) def testUrlSnarfer(self): self.assertNotError('http://gameknot.com/chess.pl?bd=1019508') @@ -47,11 +49,11 @@ class GameknotTestCase(PluginTestCase): def testSnarfer(self): self.assertRegexp('http://gameknot.com/chess.pl?bd=907498', - '\x02ddipaolo\x02 won') + '\x02ddipaolo\x0f won') self.assertRegexp('http://gameknot.com/chess.pl?bd=907498', - '\x02chroniqueur\x02 resigned') + '\x02chroniqueur\x0f resigned') self.assertRegexp('http://gameknot.com/chess.pl?bd=955432', - '\x02ddipaolo\x02 lost') + '\x02ddipaolo\x0f lost') diff --git a/test/test_Notes.py b/test/test_Notes.py index d49c71877..1d61bc169 100644 --- a/test/test_Notes.py +++ b/test/test_Notes.py @@ -29,19 +29,19 @@ # POSSIBILITY OF SUCH DAMAGE. ### - from test import * +import utils + class NotesTestCase(PluginTestCase): plugins = ('Notes',) - def testSendnote(self): self.assertNotError('sendnote inkedmn test') - self.assertError('sendnote %s %s' % (mktemp(), mktemp())) + self.assertError('sendnote alsdkjfasldk foo') def testNote(self): # self.assertNotError('note 1') - self.assertError('note %s' % mktemp()) + self.assertError('note blah') def testNotes(self): self.assertNotError('notes') diff --git a/test/test_fix.py b/test/test_fix.py index 38053e8a4..ed20453c1 100644 --- a/test/test_fix.py +++ b/test/test_fix.py @@ -32,6 +32,8 @@ from test import * +import itertools + class FunctionsTest(unittest.TestCase): def testCatch(self): def f(): @@ -65,58 +67,6 @@ class FunctionsTest(unittest.TestCase): self.assertRaises(ValueError, wwindow, [], 0) self.assertRaises(ValueError, wwindow, [], -1) - def testItersplit(self): - L = [1, 2, 3] * 3 - s = 'foo bar baz' - self.assertEqual(list(itersplit(L, lambda x: x == 3)), - [[1, 2], [1, 2], [1, 2]]) - self.assertEqual(list(itersplit(L, lambda x: x == 3, True)), - [[1, 2], [1, 2], [1, 2], []]) - self.assertEqual(list(itersplit([], lambda x: x)), []) - self.assertEqual(list(itersplit(s, lambda c: c.isspace())), - map(list, s.split())) - self.assertEqual(list(itersplit(['foo', 'for', 'bar'], 'for'.__eq__)), - [['foo'], ['bar']]) - - def testIterableMap(self): - class alist(IterableMap): - def __init__(self): - self.L = [] - - def __setitem__(self, key, value): - self.L.append((key, value)) - - def iteritems(self): - for (k, v) in self.L: - yield (k, v) - AL = alist() - self.failIf(AL) - AL[1] = 2 - AL[2] = 3 - AL[3] = 4 - self.failUnless(AL) - self.assertEqual(AL.items(), [(1, 2), (2, 3), (3, 4)]) - self.assertEqual(list(AL.iteritems()), [(1, 2), (2, 3), (3, 4)]) - self.assertEqual(AL.keys(), [1, 2, 3]) - self.assertEqual(list(AL.iterkeys()), [1, 2, 3]) - self.assertEqual(AL.values(), [2, 3, 4]) - self.assertEqual(list(AL.itervalues()), [2, 3, 4]) - self.assertEqual(len(AL), 3) - - def testFlatten(self): - def lflatten(seq): - return list(flatten(seq)) - self.assertEqual(lflatten([]), []) - self.assertEqual(lflatten([1]), [1]) - self.assertEqual(lflatten(range(10)), range(10)) - twoRanges = range(10)*2 - twoRanges.sort() - self.assertEqual(lflatten(zip(range(10), range(10))), twoRanges) - self.assertEqual(lflatten([1, [2, 3], 4]), [1, 2, 3, 4]) - self.assertEqual(lflatten([[[[[[[[[[]]]]]]]]]]), []) - self.assertEqual(lflatten([1, [2, [3, 4], 5], 6]), [1, 2, 3, 4, 5, 6]) - self.assertRaises(TypeError, lflatten, 1) - def testAny(self): self.failUnless(any(lambda i: i == 0, range(10))) self.failIf(any(None, range(1))) @@ -126,8 +76,17 @@ class FunctionsTest(unittest.TestCase): self.failIf(all(lambda i: i == 0, range(10))) self.failUnless(any(lambda i: i % 2, range(2))) self.failIf(any(lambda i: i % 2 == 0, [1, 3, 5])) - - + + def testPartition(self): + L = range(10) + def even(i): + return not(i % 2) + (yes, no) = partition(even, L) + self.assertEqual(yes, [0, 2, 4, 6, 8]) + self.assertEqual(no, [1, 3, 5, 7, 9]) + + def testIlen(self): + self.assertEqual(itertools.ilen(iter(range(10))), 10) # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: diff --git a/test/test_utils.py b/test/test_utils.py index 678aeefbc..830e9f678 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -176,5 +176,64 @@ class UtilsTest(unittest.TestCase): self.assertEqual(utils.nItems(2, 'tool', 'crazy'), '2 crazy tools') self.assertEqual(utils.nItems(2, 'tool'), '2 tools') + def testItersplit(self): + from utils import itersplit + L = [1, 2, 3] * 3 + s = 'foo bar baz' + self.assertEqual(list(itersplit(lambda x: x == 3, L)), + [[1, 2], [1, 2], [1, 2]]) + self.assertEqual(list(itersplit(lambda x: x == 3, L, yieldEmpty=True)), + [[1, 2], [1, 2], [1, 2], []]) + self.assertEqual(list(itersplit(lambda x: x, [])), []) + self.assertEqual(list(itersplit(lambda c: c.isspace(), s)), + map(list, s.split())) + self.assertEqual(list(itersplit('for'.__eq__, ['foo', 'for', 'bar'])), + [['foo'], ['bar']]) + self.assertEqual(list(itersplit('for'.__eq__, + ['foo','for','bar','for', 'baz'], 1)), + [['foo'], ['bar', 'for', 'baz']]) + + def testIterableMap(self): + class alist(utils.IterableMap): + def __init__(self): + self.L = [] + + def __setitem__(self, key, value): + self.L.append((key, value)) + + def iteritems(self): + for (k, v) in self.L: + yield (k, v) + AL = alist() + self.failIf(AL) + AL[1] = 2 + AL[2] = 3 + AL[3] = 4 + self.failUnless(AL) + self.assertEqual(AL.items(), [(1, 2), (2, 3), (3, 4)]) + self.assertEqual(list(AL.iteritems()), [(1, 2), (2, 3), (3, 4)]) + self.assertEqual(AL.keys(), [1, 2, 3]) + self.assertEqual(list(AL.iterkeys()), [1, 2, 3]) + self.assertEqual(AL.values(), [2, 3, 4]) + self.assertEqual(list(AL.itervalues()), [2, 3, 4]) + self.assertEqual(len(AL), 3) + + def testFlatten(self): + def lflatten(seq): + return list(utils.flatten(seq)) + self.assertEqual(lflatten([]), []) + self.assertEqual(lflatten([1]), [1]) + self.assertEqual(lflatten(range(10)), range(10)) + twoRanges = range(10)*2 + twoRanges.sort() + self.assertEqual(lflatten(zip(range(10), range(10))), twoRanges) + self.assertEqual(lflatten([1, [2, 3], 4]), [1, 2, 3, 4]) + self.assertEqual(lflatten([[[[[[[[[[]]]]]]]]]]), []) + self.assertEqual(lflatten([1, [2, [3, 4], 5], 6]), [1, 2, 3, 4, 5, 6]) + self.assertRaises(TypeError, lflatten, 1) + + + + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: