Skip to content

Instantly share code, notes, and snippets.

Last active Mar 2, 2016
What would you like to do?
I want to convert all the fields of str.split() at once...
# -*- encoding: utf-8 -*-
def map_apply(proc, args):
# return [f(x) for f, x in zip(proc, args)]
return map(lambda f,x:f(x), proc, args) # map(apply, proc, args) doesn't work like this
class FieldConverter:
def __init__(self, *args):
self._converters = args
def conv_proc(f):
def _wrap(conv_proc_to_num):
def proc(x):
return conv_proc_to_num(x)
except ValueError:
return 0
return proc
if f is None:
return lambda s:s # identity
elif isinstance(f, str):
return lambda s:s.decode(f) # -> unicode
elif f in (int, float):
return _wrap(f)
return f
if len(args) > 0:
self.converters = [conv_proc(f) for f in self._converters]
self.converters = None
def convert(self, fields):
if self.converters:
return map_apply(self.converters, fields)
return fields
def split_and_convert(line, types, delim=' '):
field_converter = FieldConverter(*types)
return field_converter.convert(line.split(delim))
def test_map_apply():
assert [0, -1, 3.14, 5.0] == map_apply([int,int,float,float], ['0','-1','3.14','5'])
assert [0,u'日本語','日本語'] == map_apply([int,lambda s:s.decode('utf-8'),lambda s:s], ['0','日本語','日本語'])
def test_field_converter():
assert [0, -1, 3.14, 5.0] == FieldConverter(int,int,float,float).convert(['0','-1','3.14','5'])
assert [0,u'日本語','日本語'] == FieldConverter(int,'utf-8',None).convert(['0','日本語','日本語'])
def test_split_and_convert():
assert [0, -1, 3.14, 5.0] == split_and_convert('0\t-1\t3.14\t5', (int,int,float,float), '\t')
assert [0, u'日本語', '日本語'] == split_and_convert('0,日本語,日本語', (int,'utf-8',None), ',')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment