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):
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]
rowdata[to_col] = reduce(merge_func, values)
yield rowdata

View File

@ -84,7 +84,7 @@ class FECSource(object):
@staticmethod
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):
return type

View File

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

View File

@ -62,7 +62,7 @@ class FixedWidthFileSource(object):
def next(self):
line = self._fwfile.next()
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)
return record
@ -182,7 +182,7 @@ class SqliteSource(object):
self._conn = sqlite3.connect(self._dbpath)
self._conn.row_factory = dict_factory
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)
def _process_query(self):

View File

@ -38,20 +38,20 @@ def string_dig(element, separator=''):
def flatten(item, prefix='', separator='_', keys=None):
"""
Flatten nested dictionary into one with its keys concatenated together.
>>> flatten({'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
if isinstance(item, dict):
# don't prepend a leading _
if prefix != '':
prefix += separator
retval = {}
for key, value in item.iteritems():
for key, value in item.items():
if (not keys) or (key in keys):
retval.update(flatten(value, prefix + key, separator, keys))
else: