Last active
March 2, 2016 17:00
-
-
Save naoyat/3db8cd96c8dcecb5caea to your computer and use it in GitHub Desktop.
I want to convert all the fields of str.split() at once...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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): | |
try: | |
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) | |
else: | |
return f | |
if len(args) > 0: | |
self.converters = [conv_proc(f) for f in self._converters] | |
else: | |
self.converters = None | |
def convert(self, fields): | |
if self.converters: | |
return map_apply(self.converters, fields) | |
else: | |
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