# HG changeset patch # User Paul Boddie # Date 1366574647 -7200 # Node ID b87297d00005e5bdb0d8183d6c7bb773392ea03b # Parent b9819166a9860506659e07a81e4ca59cc9c0a40e Permitted the retrieval of multiple records when searching. Made status output from the test programs use standard error. diff -r b9819166a986 -r b87297d00005 simplex/__init__.py --- a/simplex/__init__.py Sat Oct 29 00:30:20 2011 +0200 +++ b/simplex/__init__.py Sun Apr 21 22:04:07 2013 +0200 @@ -49,8 +49,8 @@ """ In the resource whose 'reader' provides records, using a 'get_key' operation to yield the key for such records, and using the given index list 'l', find - the given 'term', returning a record employing the term or None if no such - record was found. + the given 'term', returning all records employing the term or an empty list + if no such records were found. """ i = bisect.bisect_left(l, (term, None)) @@ -58,7 +58,7 @@ try: found, pos = l[i] except IndexError: - return None + return [] # Since the index is more coarse than the underlying file, the bisect left # operation will most likely point to an index entry for later records than @@ -69,31 +69,29 @@ found, pos = l[i] reader.seek(pos) - - if found == term: - return reader.next() - else: - return find_in_file(reader, get_key, term) + return find_in_file(reader, get_key, term) def find_in_file(reader, get_key, term): """ In the resource whose 'reader' provides records, using a 'get_key' operation - to yield the key for such records, find the given 'term', returning a record - employing the term or None if no such record was found. + to yield the key for such records, find the given 'term', returning all + records employing the term or an empty list if no such records were found. """ + results = [] + for record in reader: key = get_key(record) if term == key: - return record + results.append(record) # Short-circuit failed searches. elif term < key: - return None + break - return None + return results def groups(l, length): diff -r b9819166a986 -r b87297d00005 test_indexed.py --- a/test_indexed.py Sat Oct 29 00:30:20 2011 +0200 +++ b/test_indexed.py Sun Apr 21 22:04:07 2013 +0200 @@ -18,7 +18,7 @@ try: t = time.time() l = make_index(f, accessor.get_key, int(interval)) - print "Indexed in %s seconds." % (time.time() - t) + print >>sys.stderr, "Indexed in %s seconds." % (time.time() - t) # Now use the index. @@ -32,9 +32,11 @@ # Perform the search. t = time.time() - line = index.find(term) - if line: - print "Found (at %s seconds)...\n%s" % (time.time() - t, line) + lines = index.find(term) + if lines: + print >>sys.stderr, "Found (at %s seconds)..." % (time.time() - t) + for line in lines: + sys.stdout.write(line) finally: f.close() diff -r b9819166a986 -r b87297d00005 test_read.py --- a/test_read.py Sat Oct 29 00:30:20 2011 +0200 +++ b/test_read.py Sun Apr 21 22:04:07 2013 +0200 @@ -26,7 +26,7 @@ try: t = time.time() l = [from_index_record(accessor.convert, record) for record in fi] - print "Read index (at %s seconds, with %d entries)." % (time.time() - t, len(l)) + print >>sys.stderr, "Read index (at %s seconds, with %d entries)." % (time.time() - t, len(l)) # Now use the index. @@ -41,9 +41,11 @@ # Perform the search. t = time.time() - line = index.find(term) - if line: - print "Found (at %s seconds)...\n%s" % (time.time() - t, line) + lines = index.find(term) + if lines: + print >>sys.stderr, "Found (at %s seconds)..." % (time.time() - t) + for line in lines: + sys.stdout.write(line) finally: f.close() diff -r b9819166a986 -r b87297d00005 test_scan.py --- a/test_scan.py Sat Oct 29 00:30:20 2011 +0200 +++ b/test_scan.py Sun Apr 21 22:04:07 2013 +0200 @@ -27,9 +27,11 @@ f.seek(0) t = time.time() - line = find_in_file(f, accessor.get_key, term) - if line: - print "Found (at %s seconds)...\n%s" % (time.time() - t, line) + lines = find_in_file(f, accessor.get_key, term) + if lines: + print >>sys.stderr, "Found (at %s seconds)..." % (time.time() - t) + for line in lines: + sys.stdout.write(line) finally: f.close() diff -r b9819166a986 -r b87297d00005 test_write.py --- a/test_write.py Sat Oct 29 00:30:20 2011 +0200 +++ b/test_write.py Sun Apr 21 22:04:07 2013 +0200 @@ -31,7 +31,7 @@ try: t = time.time() make_index(f, accessor.get_key, int(interval), writer) - print "Indexed in %s seconds." % (time.time() - t) + print >>sys.stderr, "Indexed in %s seconds." % (time.time() - t) finally: f.close()