simplex

Annotated test_scan.py

13:d254badcda0a
2011-10-01 Paul Boddie Introduced a mechanism for wrapping accessors with converters, removing the simple mechanism supporting numeric conversion. Renamed keys to fields in the context of delimited input data. Merged the stateful iterator support into the basic iterator and accessor.
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@13 9
    fields = map(int, sys.argv[3:separator])
paul@13 10
    terms = groups(sys.argv[separator+1:], len(fields))
paul@3 11
except (IndexError, ValueError):
paul@13 12
    print >>sys.stderr, "Usage: %s <filename> <field>... -- <term value>..." % sys.argv[0]
paul@3 13
    sys.exit(1)
paul@0 14
paul@0 15
f = open(filename)
paul@13 16
converters = [(numeric == "true" and int or None) for field in fields]
paul@13 17
accessor = Converted(DelimitedRecord(fields), converters)
paul@11 18
reader = TextFile(f, Iterator(accessor))
paul@12 19
paul@0 20
try:
paul@0 21
    for term in terms:
paul@4 22
        reader.seek(0)
paul@0 23
paul@0 24
        t = time.time()
paul@8 25
        line = find_in_file(reader, accessor.convert(term))
paul@0 26
        if line:
paul@0 27
            print "Found (at %s seconds)...\n%s" % (time.time() - t, line)
paul@0 28
finally:
paul@0 29
    f.close()
paul@0 30
paul@0 31
# vim: tabstop=4 expandtab shiftwidth=4