<s> def keyset_result ( iter , params , ** kwargs ) : <EOL> import csv <EOL> import sys <EOL> def _column_map ( keyset , name ) : <EOL> if keyset . get ( '<STR_LIT>' , None ) : <EOL> mappings = keyset [ '<STR_LIT>' ] <EOL> if name in mappings and mappings [ name ] : <EOL> return mappings [ name ] <EOL> return name <EOL> last_keyset_name = None <EOL> writer = csv . writer ( sys . stdout ) <EOL> for keys , values in iter : <EOL> keyset_name = keys [ <NUM_LIT:0> ] <EOL> if last_keyset_name != keyset_name : <EOL> keyset = params . keysets [ keyset_name ] <EOL> columns = keyset [ '<STR_LIT>' ] [ <NUM_LIT:1> : ] + keyset [ '<STR_LIT>' ] <EOL> mapped = [ _column_map ( keyset , column ) for column in columns ] <EOL> writer . writerow ( mapped ) <EOL> last_keyset_name = keyset_name <EOL> data = keys [ <NUM_LIT:1> : ] + values <EOL> row = [ unicode ( x ) . encode ( '<STR_LIT:utf-8>' ) for x in data ] <EOL> writer . writerow ( row ) <EOL> def reduce_result ( iter , ** kwargs ) : <EOL> import csv <EOL> import sys <EOL> def default_flush_callback ( stream ) : <EOL> stream . flush ( ) <EOL> return stream <EOL> output_stream = kwargs . get ( '<STR_LIT>' , sys . stdout ) <EOL> flush_schedule = kwargs . get ( '<STR_LIT>' , <NUM_LIT> ) <EOL> flush_callback = kwargs . get ( '<STR_LIT>' , default_flush_callback ) <EOL> writer = csv . writer ( output_stream ) <EOL> count = <NUM_LIT:0> <EOL> for keys , values in iter : <EOL> data = keys + values <EOL> row = [ unicode ( x ) . encode ( '<STR_LIT:utf-8>' ) for x in data ] <EOL> writer . writerow ( row ) <EOL> count += <NUM_LIT:1> <EOL> if count > flush_schedule : <EOL> output_stream = flush_callback ( output_stream ) <EOL> count = <NUM_LIT:0> <EOL> flush_callback ( output_stream ) </s>
