1.1 --- a/simplex/accessors.py Sat Oct 01 19:33:58 2011 +0200
1.2 +++ b/simplex/accessors.py Sat Oct 01 20:49:15 2011 +0200
1.3 @@ -22,26 +22,20 @@
1.4
1.5 "An accessor using a delimiter to split a record."
1.6
1.7 - def __init__(self, keys=None, delimiter=None, numeric=0):
1.8 + def __init__(self, keys=None, delimiter=None, converter=None):
1.9
1.10 """
1.11 Initialise the accessor using a sequence of 'keys' indicating the
1.12 columns in each record that provide the values in the eventual compound
1.13 key provided by each record, along with a 'delimiter' indicating how
1.14 - such columns are identified. If 'numeric' is set to a true value, keys
1.15 - will be interpreted as numbers.
1.16 + such columns are identified. If 'converter' is specified, this will be
1.17 + used to convert the retrieved data.
1.18 """
1.19
1.20 self.keys = keys or [0]
1.21 self.delimiter = delimiter
1.22 - self.numeric = numeric
1.23 -
1.24 - # Define a conversion method.
1.25 -
1.26 - self.convert = numeric and self.convert_numeric or (lambda x: x)
1.27 -
1.28 - def convert_numeric(self, term):
1.29 - return map(int, term)
1.30 + self.converter = converter
1.31 + self.convert = converter and converter.convert or (lambda x: x)
1.32
1.33 def get_key(self, record):
1.34 values = record.split(self.delimiter)
1.35 @@ -56,8 +50,18 @@
1.36
1.37 return "sort%s%s%s" % (
1.38 self.delimiter and (" -t $'%s'" % repr(self.delimiter)[1:-1]) or "",
1.39 - self.numeric and " -n" or "",
1.40 + self.converter and self.converter.get_sort_options() or "",
1.41 "".join([(" -k %d,%d" % (key + 1, key + 1)) for key in self.keys])
1.42 )
1.43
1.44 +class ConvertNumeric:
1.45 +
1.46 + "Convert numeric values to integers."
1.47 +
1.48 + def convert(self, term):
1.49 + return map(int, term)
1.50 +
1.51 + def get_sort_options(self):
1.52 + return " -n"
1.53 +
1.54 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/test_indexed.py Sat Oct 01 19:33:58 2011 +0200
2.2 +++ b/test_indexed.py Sat Oct 01 20:49:15 2011 +0200
2.3 @@ -13,8 +13,11 @@
2.4 sys.exit(1)
2.5
2.6 f = open(filename)
2.7 -accessor = DelimitedRecord(keys, numeric=(numeric == "true"))
2.8 +accessor = DelimitedRecord(keys, converter=(numeric == "true" and ConvertNumeric() or None))
2.9 reader = TextFile(f, Iterator(accessor))
2.10 +
2.11 +print "Sort command:", accessor.get_sort_command()
2.12 +
2.13 try:
2.14 t = time.time()
2.15 l = make_index(reader, int(interval))
3.1 --- a/test_scan.py Sat Oct 01 19:33:58 2011 +0200
3.2 +++ b/test_scan.py Sat Oct 01 20:49:15 2011 +0200
3.3 @@ -13,8 +13,11 @@
3.4 sys.exit(1)
3.5
3.6 f = open(filename)
3.7 -accessor = DelimitedRecord(keys, numeric=(numeric == "true"))
3.8 +accessor = DelimitedRecord(keys, converter=(numeric == "true" and ConvertNumeric() or None))
3.9 reader = TextFile(f, Iterator(accessor))
3.10 +
3.11 +print "Sort command:", accessor.get_sort_command()
3.12 +
3.13 try:
3.14 for term in terms:
3.15 reader.seek(0)