# HG changeset patch # User Paul Boddie # Date 1317507589 -7200 # Node ID 807beac0d287031baeb7ef93e807a95534ea2d53 # Parent d254badcda0ab39f390a5c2f8c9daece6254e620 Reintroduced conversion objects, moving state-related classes into a new converters module, and permitting converters to retain state between operations. diff -r d254badcda0a -r 807beac0d287 simplex/__init__.py --- a/simplex/__init__.py Sat Oct 01 22:06:03 2011 +0200 +++ b/simplex/__init__.py Sun Oct 02 00:19:49 2011 +0200 @@ -31,7 +31,7 @@ from simplex.readers import * from simplex.iterators import * from simplex.accessors import * -from simplex.state import * +from simplex.converters import * import bisect diff -r d254badcda0a -r 807beac0d287 simplex/accessors.py --- a/simplex/accessors.py Sat Oct 01 22:06:03 2011 +0200 +++ b/simplex/accessors.py Sun Oct 02 00:19:49 2011 +0200 @@ -63,11 +63,16 @@ self.converters = converters def get_converter(self, converter): - return converter or (lambda x: x) + return converter and converter.convert or (lambda x: x) + + def reset(self): + for converter in self.converters: + if converter: + converter.reset() def convert(self, term): converters = map(self.get_converter, self.converters) - return [converter(value) for converter, value in zip(converters, term)] + return [convert(value) for convert, value in zip(converters, term)] def get_key(self, record): key = self.accessor.get_key(record) diff -r d254badcda0a -r 807beac0d287 simplex/converters.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/simplex/converters.py Sun Oct 02 00:19:49 2011 +0200 @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +""" +Converters for use with accessors. + +Copyright (C) 2011 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +""" + +from os.path import commonprefix + +class Converter: + + "A generic converter." + + def reset(self): + pass + + def convert(self, value): + return value + +class CommonPrefixState(Converter): + + "A class whose instances maintain common prefix state." + + def __init__(self, initial=""): + self.initial = initial + self.reset() + + def reset(self): + self.value = self.initial + +class CommonPrefixDecoder(CommonPrefixState): + + "A class whose instances decode common prefix information." + + def convert(self, common_plus_suffix): + common, suffix = common_plus_suffix + self.value = self.value[:common] + suffix + return self.value + +class CommonPrefixEncoder(CommonPrefixState): + + "A class whose instances encode common prefix information." + + def convert(self, value): + common = len(commonprefix((self.value, value))) + suffix = value[common:] + self.value = value + return common, suffix + +class NumericDecoder(Converter): + + "A class which decodes numbers." + + def convert(self, value): + return int(value) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r d254badcda0a -r 807beac0d287 simplex/state.py --- a/simplex/state.py Sat Oct 01 22:06:03 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -#!/usr/bin/env python - -""" -State management classes for iterators. - -Copyright (C) 2011 Paul Boddie - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 3 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program. If not, see . -""" - -from os.path import commonprefix - -class CommonPrefixState: - - "A class whose instances maintain common prefix state." - - def __init__(self, initial=""): - self.initial = initial - self.reset() - - def reset(self): - self.value = self.initial - -class CommonPrefixDecoder(CommonPrefixState): - - "A class whose instances decode common prefix information." - - def update(self, common_plus_suffix): - common, suffix = common_plus_suffix - self.value = self.value[:common] + suffix - return self.value - -class CommonPrefixEncoder(CommonPrefixState): - - "A class whose instances encode common prefix information." - - def update(self, value): - common = len(commonprefix((self.value, value))) - suffix = value[common:] - self.value = value - return common, suffix - -# vim: tabstop=4 expandtab shiftwidth=4