1.1 --- a/simplex.py Sat Oct 01 00:01:04 2011 +0200
1.2 +++ b/simplex.py Sat Oct 01 00:40:38 2011 +0200
1.3 @@ -41,9 +41,9 @@
1.4
1.5 "A wrapper around text files."
1.6
1.7 - def __init__(self, f, key=0, delimiter=None):
1.8 + def __init__(self, f, keys=None, delimiter=None):
1.9 self.f = f
1.10 - self.key = key
1.11 + self.keys = keys or [0]
1.12 self.delimiter = delimiter
1.13
1.14 def seek(self, pos):
1.15 @@ -53,7 +53,8 @@
1.16 return self.f.xreadlines()
1.17
1.18 def get_key(self, record):
1.19 - return record.split(self.delimiter)[self.key]
1.20 + values = record.split(self.delimiter)
1.21 + return [values[key] for key in self.keys]
1.22
1.23 def index_file(f, interval):
1.24
1.25 @@ -95,7 +96,11 @@
1.26 """
1.27
1.28 i = bisect.bisect_left(l, (term, None))
1.29 - found, pos = l[i]
1.30 +
1.31 + try:
1.32 + found, pos = l[i]
1.33 + except IndexError:
1.34 + return None
1.35
1.36 # Since the index is more coarse than the underlying file, the bisect left
1.37 # operation will most likely point to an index entry for later records than
1.38 @@ -132,4 +137,20 @@
1.39 def find(self, term):
1.40 return find_with_index(self.f, self.entries, term)
1.41
1.42 +def groups(l, length):
1.43 +
1.44 + "Split 'l' into groups of the given 'length'."
1.45 +
1.46 + if length <= 0:
1.47 + raise ValueError, "Groups must be greater than zero."
1.48 +
1.49 + i = 0
1.50 + g = []
1.51 +
1.52 + while i < len(l):
1.53 + g.append(l[i:i+length])
1.54 + i += length
1.55 +
1.56 + return g
1.57 +
1.58 # vim: tabstop=4 expandtab shiftwidth=4