1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/simplex/readers.py Sat Oct 01 15:07:17 2011 +0200
1.3 @@ -0,0 +1,54 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Reader and accessor classes for indexing.
1.8 +
1.9 +Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT ANY
1.17 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
1.18 +PARTICULAR PURPOSE. See the GNU General Public License for more details.
1.19 +
1.20 +You should have received a copy of the GNU General Public License along
1.21 +with this program. If not, see <http://www.gnu.org/licenses/>.
1.22 +"""
1.23 +
1.24 +class TextFile:
1.25 +
1.26 + "A wrapper around text files."
1.27 +
1.28 + def __init__(self, f):
1.29 + self.f = f
1.30 +
1.31 + def seek(self, pos):
1.32 + self.f.seek(pos)
1.33 +
1.34 + def get_records(self):
1.35 + return self.f.xreadlines()
1.36 +
1.37 +class DelimitedRecord:
1.38 +
1.39 + "An accessor using a delimiter to split a record."
1.40 +
1.41 + def __init__(self, keys=None, delimiter=None):
1.42 +
1.43 + """
1.44 + Initialise the accessor using a sequence of 'keys' indicating the
1.45 + columns in each record that provide the values in the eventual compound
1.46 + key provided by each record, along with a 'delimiter' indicating how
1.47 + such columns are identified.
1.48 + """
1.49 +
1.50 + self.keys = keys or [0]
1.51 + self.delimiter = delimiter
1.52 +
1.53 + def get_key(self, record):
1.54 + values = record.split(self.delimiter)
1.55 + return [values[key] for key in self.keys]
1.56 +
1.57 +# vim: tabstop=4 expandtab shiftwidth=4