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, interval = sys.argv[1:4] |
paul@18 | 9 | fields = map(int, sys.argv[4:separator]) |
paul@18 | 10 | terms = groups(sys.argv[separator+1:], len(fields)) |
paul@3 | 11 | except (IndexError, ValueError): |
paul@18 | 12 | print >>sys.stderr, "Usage: %s <filename> <interval> <field>... -- <term value>..." % sys.argv[0] |
paul@3 | 13 | sys.exit(1) |
paul@0 | 14 | |
paul@0 | 15 | f = open(filename) |
paul@18 | 16 | accessor = DelimitedRecord(fields, numeric=(numeric == "true")) |
paul@22 | 17 | |
paul@0 | 18 | try: |
paul@0 | 19 | t = time.time() |
paul@19 | 20 | l = make_index(f, accessor.get_key, int(interval)) |
paul@0 | 21 | print "Indexed in %s seconds." % (time.time() - t) |
paul@0 | 22 | |
paul@0 | 23 | # Now use the index. |
paul@0 | 24 | |
paul@22 | 25 | index = IndexedFile(f, l, accessor.get_key) |
paul@0 | 26 | for term in terms: |
paul@22 | 27 | |
paul@22 | 28 | # Convert the term to the appropriate type. |
paul@22 | 29 | |
paul@22 | 30 | term = accessor.convert(term) |
paul@22 | 31 | |
paul@22 | 32 | # Perform the search. |
paul@22 | 33 | |
paul@0 | 34 | t = time.time() |
paul@22 | 35 | line = index.find(term) |
paul@0 | 36 | if line: |
paul@0 | 37 | print "Found (at %s seconds)...\n%s" % (time.time() - t, line) |
paul@0 | 38 | |
paul@0 | 39 | finally: |
paul@0 | 40 | f.close() |
paul@0 | 41 | |
paul@0 | 42 | # vim: tabstop=4 expandtab shiftwidth=4 |