paul@11 | 1 | #!/usr/bin/env python |
paul@11 | 2 | |
paul@11 | 3 | """ |
paul@11 | 4 | Accessor classes for indexing. |
paul@11 | 5 | |
paul@11 | 6 | Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk> |
paul@11 | 7 | |
paul@11 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@11 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@11 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@11 | 11 | version. |
paul@11 | 12 | |
paul@11 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT ANY |
paul@11 | 14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
paul@11 | 15 | PARTICULAR PURPOSE. See the GNU General Public License for more details. |
paul@11 | 16 | |
paul@11 | 17 | You should have received a copy of the GNU General Public License along |
paul@11 | 18 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@11 | 19 | """ |
paul@11 | 20 | |
paul@11 | 21 | class DelimitedRecord: |
paul@11 | 22 | |
paul@11 | 23 | "An accessor using a delimiter to split a record." |
paul@11 | 24 | |
paul@12 | 25 | def __init__(self, keys=None, delimiter=None, converter=None): |
paul@11 | 26 | |
paul@11 | 27 | """ |
paul@11 | 28 | Initialise the accessor using a sequence of 'keys' indicating the |
paul@11 | 29 | columns in each record that provide the values in the eventual compound |
paul@11 | 30 | key provided by each record, along with a 'delimiter' indicating how |
paul@12 | 31 | such columns are identified. If 'converter' is specified, this will be |
paul@12 | 32 | used to convert the retrieved data. |
paul@11 | 33 | """ |
paul@11 | 34 | |
paul@11 | 35 | self.keys = keys or [0] |
paul@11 | 36 | self.delimiter = delimiter |
paul@12 | 37 | self.converter = converter |
paul@12 | 38 | self.convert = converter and converter.convert or (lambda x: x) |
paul@11 | 39 | |
paul@11 | 40 | def get_key(self, record): |
paul@11 | 41 | values = record.split(self.delimiter) |
paul@11 | 42 | return self.convert([values[key] for key in self.keys]) |
paul@11 | 43 | |
paul@11 | 44 | def get_sort_command(self): |
paul@11 | 45 | |
paul@11 | 46 | """ |
paul@11 | 47 | Return the Unix sort command invocation required to produce the ordering |
paul@11 | 48 | described by this instance. |
paul@11 | 49 | """ |
paul@11 | 50 | |
paul@11 | 51 | return "sort%s%s%s" % ( |
paul@11 | 52 | self.delimiter and (" -t $'%s'" % repr(self.delimiter)[1:-1]) or "", |
paul@12 | 53 | self.converter and self.converter.get_sort_options() or "", |
paul@11 | 54 | "".join([(" -k %d,%d" % (key + 1, key + 1)) for key in self.keys]) |
paul@11 | 55 | ) |
paul@11 | 56 | |
paul@12 | 57 | class ConvertNumeric: |
paul@12 | 58 | |
paul@12 | 59 | "Convert numeric values to integers." |
paul@12 | 60 | |
paul@12 | 61 | def convert(self, term): |
paul@12 | 62 | return map(int, term) |
paul@12 | 63 | |
paul@12 | 64 | def get_sort_options(self): |
paul@12 | 65 | return " -n" |
paul@12 | 66 | |
paul@11 | 67 | # vim: tabstop=4 expandtab shiftwidth=4 |