1.1 --- a/simplex/__init__.py Sat Oct 01 22:06:03 2011 +0200
1.2 +++ b/simplex/__init__.py Sun Oct 02 00:19:49 2011 +0200
1.3 @@ -31,7 +31,7 @@
1.4 from simplex.readers import *
1.5 from simplex.iterators import *
1.6 from simplex.accessors import *
1.7 -from simplex.state import *
1.8 +from simplex.converters import *
1.9
1.10 import bisect
1.11
2.1 --- a/simplex/accessors.py Sat Oct 01 22:06:03 2011 +0200
2.2 +++ b/simplex/accessors.py Sun Oct 02 00:19:49 2011 +0200
2.3 @@ -63,11 +63,16 @@
2.4 self.converters = converters
2.5
2.6 def get_converter(self, converter):
2.7 - return converter or (lambda x: x)
2.8 + return converter and converter.convert or (lambda x: x)
2.9 +
2.10 + def reset(self):
2.11 + for converter in self.converters:
2.12 + if converter:
2.13 + converter.reset()
2.14
2.15 def convert(self, term):
2.16 converters = map(self.get_converter, self.converters)
2.17 - return [converter(value) for converter, value in zip(converters, term)]
2.18 + return [convert(value) for convert, value in zip(converters, term)]
2.19
2.20 def get_key(self, record):
2.21 key = self.accessor.get_key(record)
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/simplex/converters.py Sun Oct 02 00:19:49 2011 +0200
3.3 @@ -0,0 +1,70 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +Converters for use with accessors.
3.8 +
3.9 +Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk>
3.10 +
3.11 +This program is free software; you can redistribute it and/or modify it under
3.12 +the terms of the GNU General Public License as published by the Free Software
3.13 +Foundation; either version 3 of the License, or (at your option) any later
3.14 +version.
3.15 +
3.16 +This program is distributed in the hope that it will be useful, but WITHOUT ANY
3.17 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
3.18 +PARTICULAR PURPOSE. See the GNU General Public License for more details.
3.19 +
3.20 +You should have received a copy of the GNU General Public License along
3.21 +with this program. If not, see <http://www.gnu.org/licenses/>.
3.22 +"""
3.23 +
3.24 +from os.path import commonprefix
3.25 +
3.26 +class Converter:
3.27 +
3.28 + "A generic converter."
3.29 +
3.30 + def reset(self):
3.31 + pass
3.32 +
3.33 + def convert(self, value):
3.34 + return value
3.35 +
3.36 +class CommonPrefixState(Converter):
3.37 +
3.38 + "A class whose instances maintain common prefix state."
3.39 +
3.40 + def __init__(self, initial=""):
3.41 + self.initial = initial
3.42 + self.reset()
3.43 +
3.44 + def reset(self):
3.45 + self.value = self.initial
3.46 +
3.47 +class CommonPrefixDecoder(CommonPrefixState):
3.48 +
3.49 + "A class whose instances decode common prefix information."
3.50 +
3.51 + def convert(self, common_plus_suffix):
3.52 + common, suffix = common_plus_suffix
3.53 + self.value = self.value[:common] + suffix
3.54 + return self.value
3.55 +
3.56 +class CommonPrefixEncoder(CommonPrefixState):
3.57 +
3.58 + "A class whose instances encode common prefix information."
3.59 +
3.60 + def convert(self, value):
3.61 + common = len(commonprefix((self.value, value)))
3.62 + suffix = value[common:]
3.63 + self.value = value
3.64 + return common, suffix
3.65 +
3.66 +class NumericDecoder(Converter):
3.67 +
3.68 + "A class which decodes numbers."
3.69 +
3.70 + def convert(self, value):
3.71 + return int(value)
3.72 +
3.73 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/simplex/state.py Sat Oct 01 22:06:03 2011 +0200
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,53 +0,0 @@
4.4 -#!/usr/bin/env python
4.5 -
4.6 -"""
4.7 -State management classes for iterators.
4.8 -
4.9 -Copyright (C) 2011 Paul Boddie <paul@boddie.org.uk>
4.10 -
4.11 -This program is free software; you can redistribute it and/or modify it under
4.12 -the terms of the GNU General Public License as published by the Free Software
4.13 -Foundation; either version 3 of the License, or (at your option) any later
4.14 -version.
4.15 -
4.16 -This program is distributed in the hope that it will be useful, but WITHOUT ANY
4.17 -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
4.18 -PARTICULAR PURPOSE. See the GNU General Public License for more details.
4.19 -
4.20 -You should have received a copy of the GNU General Public License along
4.21 -with this program. If not, see <http://www.gnu.org/licenses/>.
4.22 -"""
4.23 -
4.24 -from os.path import commonprefix
4.25 -
4.26 -class CommonPrefixState:
4.27 -
4.28 - "A class whose instances maintain common prefix state."
4.29 -
4.30 - def __init__(self, initial=""):
4.31 - self.initial = initial
4.32 - self.reset()
4.33 -
4.34 - def reset(self):
4.35 - self.value = self.initial
4.36 -
4.37 -class CommonPrefixDecoder(CommonPrefixState):
4.38 -
4.39 - "A class whose instances decode common prefix information."
4.40 -
4.41 - def update(self, common_plus_suffix):
4.42 - common, suffix = common_plus_suffix
4.43 - self.value = self.value[:common] + suffix
4.44 - return self.value
4.45 -
4.46 -class CommonPrefixEncoder(CommonPrefixState):
4.47 -
4.48 - "A class whose instances encode common prefix information."
4.49 -
4.50 - def update(self, value):
4.51 - common = len(commonprefix((self.value, value)))
4.52 - suffix = value[common:]
4.53 - self.value = value
4.54 - return common, suffix
4.55 -
4.56 -# vim: tabstop=4 expandtab shiftwidth=4