change iteritems and iterkeys to items and keys views

This commit is contained in:
Jeremy Carbaugh 2012-03-11 22:05:46 -07:00
parent c25550f8da
commit 12847d342e
5 changed files with 19 additions and 19 deletions

View File

@ -3,7 +3,7 @@ from saucebrush.outputs import CSVOutput, DebugOutput
def merge_columns(datasource, mapping, merge_func): def merge_columns(datasource, mapping, merge_func):
for rowdata in datasource: for rowdata in datasource:
for to_col,from_cols in mapping.iteritems(): for to_col,from_cols in mapping.items():
values = [rowdata.pop(col, None) for col in from_cols] values = [rowdata.pop(col, None) for col in from_cols]
rowdata[to_col] = reduce(merge_func, values) rowdata[to_col] = reduce(merge_func, values)
yield rowdata yield rowdata

View File

@ -84,7 +84,7 @@ class FECSource(object):
@staticmethod @staticmethod
def get_form_type(rectype): def get_form_type(rectype):
for type_re, type in FECSource.FORM_MAPPING.iteritems(): for type_re, type in FECSource.FORM_MAPPING.items():
if type_re.match(rectype): if type_re.match(rectype):
return type return type

View File

@ -217,17 +217,17 @@ class FieldModifier(FieldFilter):
class FieldKeeper(Filter): class FieldKeeper(Filter):
""" Filter that removes all but the given set of fields. """ Filter that removes all but the given set of fields.
FieldKeeper(('spam', 'eggs')) removes all bu tthe spam and eggs FieldKeeper(('spam', 'eggs')) removes all bu tthe spam and eggs
fields from every record filtered. fields from every record filtered.
""" """
def __init__(self, keys): def __init__(self, keys):
super(FieldKeeper, self).__init__() super(FieldKeeper, self).__init__()
self._target_keys = utils.str_or_list(keys) self._target_keys = utils.str_or_list(keys)
def process_record(self, record): def process_record(self, record):
for key in record.keys(): for key in list(record.keys()):
if key not in self._target_keys: if key not in self._target_keys:
del record[key] del record[key]
return record return record
@ -269,7 +269,7 @@ class FieldMerger(Filter):
self._keep_fields = keep_fields 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.items():
if self._keep_fields: if self._keep_fields:
values = [record.get(col, None) for col in from_cols] values = [record.get(col, None) for col in from_cols]
else: else:
@ -328,7 +328,7 @@ class FieldCopier(Filter):
def process_record(self, record): def process_record(self, record):
# mapping is dest:source # mapping is dest:source
for dest, source in self._copy_mapping.iteritems(): for dest, source in self._copy_mapping.items():
record[dest] = record[source] record[dest] = record[source]
return record return record
@ -343,7 +343,7 @@ class FieldRenamer(Filter):
def process_record(self, record): def process_record(self, record):
# mapping is dest:source # mapping is dest:source
for dest, source in self._rename_mapping.iteritems(): for dest, source in self._rename_mapping.items():
record[dest] = record.pop(source) record[dest] = record.pop(source)
return record return record
@ -363,7 +363,7 @@ class Splitter(Filter):
self._split_mapping = split_mapping self._split_mapping = split_mapping
def process_record(self, record): def process_record(self, record):
for key, filters in self._split_mapping.iteritems(): for key, filters in self._split_mapping.items():
# if the key doesn't exist -- move on to next key # if the key doesn't exist -- move on to next key
try: try:
@ -479,7 +479,7 @@ class UnicodeFilter(Filter):
self._errors = errors self._errors = errors
def process_record(self, record): def process_record(self, record):
for key, value in record.iteritems(): for key, value in record.items():
if isinstance(value, str): if isinstance(value, str):
record[key] = unicode(value, self._encoding, self._errors) record[key] = unicode(value, self._encoding, self._errors)
elif isinstance(value, unicode): elif isinstance(value, unicode):
@ -494,7 +494,7 @@ class StringFilter(Filter):
self._errors = errors self._errors = errors
def process_record(self, record): def process_record(self, record):
for key, value in record.iteritems(): for key, value in record.items():
if isinstance(value, unicode): if isinstance(value, unicode):
record[key] = value.encode(self._encoding, self._errors) record[key] = value.encode(self._encoding, self._errors)
return record return record
@ -584,7 +584,7 @@ class NameCleaner(Filter):
# if there is a match, remove original name and add pieces # if there is a match, remove original name and add pieces
if match: if match:
record.pop(key) record.pop(key)
for k,v in match.groupdict().iteritems(): for k,v in match.groupdict().items():
record[self._name_prefix + k] = v record[self._name_prefix + k] = v
break break

View File

@ -62,7 +62,7 @@ class FixedWidthFileSource(object):
def next(self): def next(self):
line = self._fwfile.next() line = self._fwfile.next()
record = {} record = {}
for name, range_ in self._fields_dict.iteritems(): for name, range_ in self._fields_dict.items():
record[name] = line[range_[0]:range_[1]].rstrip(self._fillchars) record[name] = line[range_[0]:range_[1]].rstrip(self._fillchars)
return record return record
@ -182,7 +182,7 @@ class SqliteSource(object):
self._conn = sqlite3.connect(self._dbpath) self._conn = sqlite3.connect(self._dbpath)
self._conn.row_factory = dict_factory self._conn.row_factory = dict_factory
if self._conn_params: if self._conn_params:
for param, value in self._conn_params.iteritems(): for param, value in self._conn_params.items():
setattr(self._conn, param, value) setattr(self._conn, param, value)
def _process_query(self): def _process_query(self):

View File

@ -38,20 +38,20 @@ def string_dig(element, separator=''):
def flatten(item, prefix='', separator='_', keys=None): def flatten(item, prefix='', separator='_', keys=None):
""" """
Flatten nested dictionary into one with its keys concatenated together. Flatten nested dictionary into one with its keys concatenated together.
>>> flatten({'a':1, 'b':{'c':2}, 'd':[{'e':{'r':7}}, {'e':5}], >>> flatten({'a':1, 'b':{'c':2}, 'd':[{'e':{'r':7}}, {'e':5}],
'f':{'g':{'h':6}}}) 'f':{'g':{'h':6}}})
{'a': 1, 'b_c': 2, 'd': [{'e_r': 7}, {'e': 5}], 'f_g_h': 6} {'a': 1, 'b_c': 2, 'd': [{'e_r': 7}, {'e': 5}], 'f_g_h': 6}
""" """
# update dictionaries recursively # update dictionaries recursively
if isinstance(item, dict): if isinstance(item, dict):
# don't prepend a leading _ # don't prepend a leading _
if prefix != '': if prefix != '':
prefix += separator prefix += separator
retval = {} retval = {}
for key, value in item.iteritems(): for key, value in item.items():
if (not keys) or (key in keys): if (not keys) or (key in keys):
retval.update(flatten(value, prefix + key, separator, keys)) retval.update(flatten(value, prefix + key, separator, keys))
else: else: