paul@6 | 1 | #!/usr/bin/env python |
paul@6 | 2 | |
paul@6 | 3 | """ |
paul@6 | 4 | Reader and accessor classes for indexing. |
paul@6 | 5 | |
paul@6 | 6 | Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk> |
paul@6 | 7 | |
paul@6 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@6 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@6 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@6 | 11 | version. |
paul@6 | 12 | |
paul@6 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@6 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@6 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@6 | 16 | |
paul@6 | 17 | You should have received a copy of the GNU General Public License along |
paul@6 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@6 | 19 | """ |
paul@6 | 20 | |
paul@6 | 21 | class TextFile: |
paul@6 | 22 | |
paul@6 | 23 | "A wrapper around text files." |
paul@6 | 24 | |
paul@6 | 25 | def __init__(self, f): |
paul@6 | 26 | self.f = f |
paul@6 | 27 | |
paul@6 | 28 | def seek(self, pos): |
paul@6 | 29 | self.f.seek(pos) |
paul@6 | 30 | |
paul@16 | 31 | def __iter__(self): |
paul@16 | 32 | return iter(self.f.xreadlines()) |
paul@6 | 33 | |
paul@6 | 34 | class DelimitedRecord: |
paul@6 | 35 | |
paul@6 | 36 | "An accessor using a delimiter to split a record." |
paul@6 | 37 | |
paul@18 | 38 | def __init__(self, fields=None, delimiter=None, numeric=0): |
paul@6 | 39 | |
paul@6 | 40 | """ |
paul@18 | 41 | Initialise the accessor using a sequence of 'fields' indicating the |
paul@6 | 42 | columns in each record that provide the values in the eventual compound |
paul@18 | 43 | field provided by each record, along with a 'delimiter' indicating how |
paul@18 | 44 | such columns are identified. If 'numeric' is set to a true value, fields |
paul@7 | 45 | will be interpreted as numbers. |
paul@6 | 46 | """ |
paul@6 | 47 | |
paul@18 | 48 | self.fields = fields or [0] |
paul@6 | 49 | self.delimiter = delimiter |
paul@7 | 50 | self.convert = numeric and self.convert_numeric or (lambda x: x) |
paul@7 | 51 | |
paul@7 | 52 | def convert_numeric(self, term): |
paul@7 | 53 | return map(int, term) |
paul@6 | 54 | |
paul@6 | 55 | def get_key(self, record): |
paul@6 | 56 | values = record.split(self.delimiter) |
paul@18 | 57 | return self.convert([values[field] for field in self.fields]) |
paul@6 | 58 | |
paul@6 | 59 | # vim: tabstop=4 expandtab shiftwidth=4 |