simplex

Changeset

26:b87297d00005
2013-04-21 Paul Boddie raw files shortlog changelog graph Permitted the retrieval of multiple records when searching. Made status output from the test programs use standard error. default tip
simplex/__init__.py (file) test_indexed.py (file) test_read.py (file) test_scan.py (file) test_write.py (file)
     1.1 --- a/simplex/__init__.py	Sat Oct 29 00:30:20 2011 +0200
     1.2 +++ b/simplex/__init__.py	Sun Apr 21 22:04:07 2013 +0200
     1.3 @@ -49,8 +49,8 @@
     1.4      """
     1.5      In the resource whose 'reader' provides records, using a 'get_key' operation
     1.6      to yield the key for such records, and using the given index list 'l', find
     1.7 -    the given 'term', returning a record employing the term or None if no such
     1.8 -    record was found.
     1.9 +    the given 'term', returning all records employing the term or an empty list
    1.10 +    if no such records were found.
    1.11      """
    1.12  
    1.13      i = bisect.bisect_left(l, (term, None))
    1.14 @@ -58,7 +58,7 @@
    1.15      try:
    1.16          found, pos = l[i]
    1.17      except IndexError:
    1.18 -        return None
    1.19 +        return []
    1.20  
    1.21      # Since the index is more coarse than the underlying file, the bisect left
    1.22      # operation will most likely point to an index entry for later records than
    1.23 @@ -69,31 +69,29 @@
    1.24          found, pos = l[i]
    1.25  
    1.26      reader.seek(pos)
    1.27 -
    1.28 -    if found == term:
    1.29 -        return reader.next()
    1.30 -    else:
    1.31 -        return find_in_file(reader, get_key, term)
    1.32 +    return find_in_file(reader, get_key, term)
    1.33  
    1.34  def find_in_file(reader, get_key, term):
    1.35  
    1.36      """
    1.37      In the resource whose 'reader' provides records, using a 'get_key' operation
    1.38 -    to yield the key for such records, find the given 'term', returning a record
    1.39 -    employing the term or None if no such record was found.
    1.40 +    to yield the key for such records, find the given 'term', returning all
    1.41 +    records employing the term or an empty list if no such records were found.
    1.42      """
    1.43  
    1.44 +    results = []
    1.45 +
    1.46      for record in reader:
    1.47          key = get_key(record)
    1.48          if term == key:
    1.49 -            return record
    1.50 +            results.append(record)
    1.51  
    1.52          # Short-circuit failed searches.
    1.53  
    1.54          elif term < key:
    1.55 -            return None
    1.56 +            break
    1.57  
    1.58 -    return None
    1.59 +    return results
    1.60  
    1.61  def groups(l, length):
    1.62  
     2.1 --- a/test_indexed.py	Sat Oct 29 00:30:20 2011 +0200
     2.2 +++ b/test_indexed.py	Sun Apr 21 22:04:07 2013 +0200
     2.3 @@ -18,7 +18,7 @@
     2.4  try:
     2.5      t = time.time()
     2.6      l = make_index(f, accessor.get_key, int(interval))
     2.7 -    print "Indexed in %s seconds." % (time.time() - t)
     2.8 +    print >>sys.stderr, "Indexed in %s seconds." % (time.time() - t)
     2.9  
    2.10      # Now use the index.
    2.11  
    2.12 @@ -32,9 +32,11 @@
    2.13          # Perform the search.
    2.14  
    2.15          t = time.time()
    2.16 -        line = index.find(term)
    2.17 -        if line:
    2.18 -            print "Found (at %s seconds)...\n%s" % (time.time() - t, line)
    2.19 +        lines = index.find(term)
    2.20 +        if lines:
    2.21 +            print >>sys.stderr, "Found (at %s seconds)..." % (time.time() - t)
    2.22 +            for line in lines:
    2.23 +                sys.stdout.write(line)
    2.24  
    2.25  finally:
    2.26      f.close()
     3.1 --- a/test_read.py	Sat Oct 29 00:30:20 2011 +0200
     3.2 +++ b/test_read.py	Sun Apr 21 22:04:07 2013 +0200
     3.3 @@ -26,7 +26,7 @@
     3.4  try:
     3.5      t = time.time()
     3.6      l = [from_index_record(accessor.convert, record) for record in fi]
     3.7 -    print "Read index (at %s seconds, with %d entries)." % (time.time() - t, len(l))
     3.8 +    print >>sys.stderr, "Read index (at %s seconds, with %d entries)." % (time.time() - t, len(l))
     3.9  
    3.10      # Now use the index.
    3.11  
    3.12 @@ -41,9 +41,11 @@
    3.13          # Perform the search.
    3.14  
    3.15          t = time.time()
    3.16 -        line = index.find(term)
    3.17 -        if line:
    3.18 -            print "Found (at %s seconds)...\n%s" % (time.time() - t, line)
    3.19 +        lines = index.find(term)
    3.20 +        if lines:
    3.21 +            print >>sys.stderr, "Found (at %s seconds)..." % (time.time() - t)
    3.22 +            for line in lines:
    3.23 +                sys.stdout.write(line)
    3.24  
    3.25  finally:
    3.26      f.close()
     4.1 --- a/test_scan.py	Sat Oct 29 00:30:20 2011 +0200
     4.2 +++ b/test_scan.py	Sun Apr 21 22:04:07 2013 +0200
     4.3 @@ -27,9 +27,11 @@
     4.4          f.seek(0)
     4.5  
     4.6          t = time.time()
     4.7 -        line = find_in_file(f, accessor.get_key, term)
     4.8 -        if line:
     4.9 -            print "Found (at %s seconds)...\n%s" % (time.time() - t, line)
    4.10 +        lines = find_in_file(f, accessor.get_key, term)
    4.11 +        if lines:
    4.12 +            print >>sys.stderr, "Found (at %s seconds)..." % (time.time() - t)
    4.13 +            for line in lines:
    4.14 +                sys.stdout.write(line)
    4.15  finally:
    4.16      f.close()
    4.17  
     5.1 --- a/test_write.py	Sat Oct 29 00:30:20 2011 +0200
     5.2 +++ b/test_write.py	Sun Apr 21 22:04:07 2013 +0200
     5.3 @@ -31,7 +31,7 @@
     5.4  try:
     5.5      t = time.time()
     5.6      make_index(f, accessor.get_key, int(interval), writer)
     5.7 -    print "Indexed in %s seconds." % (time.time() - t)
     5.8 +    print >>sys.stderr, "Indexed in %s seconds." % (time.time() - t)
     5.9  
    5.10  finally:
    5.11      f.close()