From 12847d342e17aa6878c3fcf27b32ab237fd447be Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Sun, 11 Mar 2012 22:05:46 -0700 Subject: [PATCH] change iteritems and iterkeys to items and keys views --- examples/earmarkwatch.py | 2 +- examples/fec_electronic.py | 2 +- saucebrush/filters.py | 22 +++++++++++----------- saucebrush/sources.py | 4 ++-- saucebrush/utils.py | 8 ++++---- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/earmarkwatch.py b/examples/earmarkwatch.py index 88f7454..54a0088 100644 --- a/examples/earmarkwatch.py +++ b/examples/earmarkwatch.py @@ -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 diff --git a/examples/fec_electronic.py b/examples/fec_electronic.py index 6bf1b78..d482eef 100644 --- a/examples/fec_electronic.py +++ b/examples/fec_electronic.py @@ -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 diff --git a/saucebrush/filters.py b/saucebrush/filters.py index 380628f..2b9584b 100644 --- a/saucebrush/filters.py +++ b/saucebrush/filters.py @@ -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 diff --git a/saucebrush/sources.py b/saucebrush/sources.py index 7427471..06cf60b 100644 --- a/saucebrush/sources.py +++ b/saucebrush/sources.py @@ -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): diff --git a/saucebrush/utils.py b/saucebrush/utils.py index 6f98257..2139137 100644 --- a/saucebrush/utils.py +++ b/saucebrush/utils.py @@ -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: