handle switch from generator next() to __next__()

This commit is contained in:
Jeremy Carbaugh 2012-03-11 22:17:20 -07:00
parent e5f1012e17
commit 8d512d7e07
3 changed files with 15 additions and 5 deletions

View File

@ -301,7 +301,11 @@ class FieldAdder(Filter):
self._field_name = field_name self._field_name = field_name
self._field_value = field_value self._field_value = field_value
if hasattr(self._field_value, '__iter__'): if hasattr(self._field_value, '__iter__'):
self._field_value = iter(self._field_value).next value_iter = iter(self._field_value)
if hasattr(value_iter, "next"):
self._field_value = value_iter.next
else:
self._field_value = value_iter.__next__
self._replace = replace self._replace = replace
def process_record(self, record): def process_record(self, record):

View File

@ -59,13 +59,18 @@ class FixedWidthFileSource(object):
def __iter__(self): def __iter__(self):
return self return self
def next(self): def __next__(self):
line = self._fwfile.next() line = next(self._fwfile)
record = {} record = {}
for name, range_ in self._fields_dict.items(): 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
def next(self):
""" Keep Python 2 next() method that defers to __next__().
"""
return self.__next__()
class HtmlTableSource(object): class HtmlTableSource(object):
""" Saucebrush source for reading data from an HTML table. """ Saucebrush source for reading data from an HTML table.

View File

@ -63,7 +63,8 @@ class FilterTestCase(unittest.TestCase):
recipe = DummyRecipe() recipe = DummyRecipe()
f = Doubler() f = Doubler()
result = f.attach([1,2,3], recipe=recipe) result = f.attach([1,2,3], recipe=recipe)
result.next() # next has to be called for attach to take effect # next has to be called for attach to take effect
next(result)
f.reject_record('bad', 'this one was bad') f.reject_record('bad', 'this one was bad')
# ensure that the rejection propagated to the recipe # ensure that the rejection propagated to the recipe