simplex

Annotated test_scan.py

7:48a194ecc68c
2011-10-01 Paul Boddie Added short-circuiting of failed searches. Added term/key conversion support so that numerically sorted keys can be used.
paul@0 1
#!/usr/bin/env python
paul@0 2
paul@0 3
from simplex import *
paul@0 4
import sys, time
paul@0 5
paul@3 6
try:
paul@3 7
    separator = sys.argv.index("--")
paul@7 8
    filename, numeric = sys.argv[1:3]
paul@7 9
    keys = map(int, sys.argv[3:separator])
paul@3 10
    terms = groups(sys.argv[separator+1:], len(keys))
paul@3 11
except (IndexError, ValueError):
paul@3 12
    print >>sys.stderr, "Usage: %s <filename> <key>... -- <term value>..." % sys.argv[0]
paul@3 13
    sys.exit(1)
paul@0 14
paul@0 15
f = open(filename)
paul@5 16
reader = TextFile(f)
paul@7 17
accessor = DelimitedRecord(keys, numeric=(numeric == "true"))
paul@0 18
try:
paul@0 19
    for term in terms:
paul@4 20
        reader.seek(0)
paul@0 21
paul@0 22
        t = time.time()
paul@7 23
        line = find_in_file(reader, accessor, accessor.convert(term))
paul@0 24
        if line:
paul@0 25
            print "Found (at %s seconds)...\n%s" % (time.time() - t, line)
paul@0 26
finally:
paul@0 27
    f.close()
paul@0 28
paul@0 29
# vim: tabstop=4 expandtab shiftwidth=4