add keep_records parameter on FieldMerger to prevent it from removing merged fields

This commit is contained in:
Jeremy Carbaugh 2009-10-09 17:28:41 -04:00
parent dcda3db140
commit 2cde3badad

View File

@ -169,13 +169,17 @@ class FieldMerger(Filter):
column bacon that is the result of spam+eggs column bacon that is the result of spam+eggs
""" """
def __init__(self, mapping, merge_func): def __init__(self, mapping, merge_func, keep_fields=False):
super(FieldMerger, self).__init__() super(FieldMerger, self).__init__()
self._field_mapping = mapping self._field_mapping = mapping
self._merge_func = merge_func self._merge_func = merge_func
self._keep_fields = keep_fields
def process_record(self, record): def process_record(self, record):
for to_col, from_cols in self._field_mapping.iteritems(): for to_col, from_cols in self._field_mapping.iteritems():
if self._keep_fields:
values = [record.get(col, None) for col in from_cols]
else:
values = [record.pop(col, None) for col in from_cols] values = [record.pop(col, None) for col in from_cols]
record[to_col] = self._merge_func(*values) record[to_col] = self._merge_func(*values)
return record return record