MAINT: Support both Python 2 and 3 next interfaces.

Python 3 uses the `__next__` method instead of `next`,
and uses the syntax of `next(foo)` accordingly.

Add `__next__` and `next` side-by-side so both Python 2 and 3 have
a method that can be used during iteration.
This commit is contained in:
Eddie Hebert
2014-01-07 11:46:57 -05:00
parent 045e2975b7
commit 36f8b77290
5 changed files with 12 additions and 6 deletions
+1 -1
View File
@@ -329,7 +329,7 @@ class SlippageTestCase(TestCase):
slippage_model = VolumeShareSlippage()
try:
_, txn = slippage_model.simulate(event, [order]).next()
_, txn = next(slippage_model.simulate(event, [order]))
except StopIteration:
txn = None
+3 -3
View File
@@ -293,7 +293,7 @@ class TestEventsThroughRisk(unittest.TestCase):
crm = algo.perf_tracker.cumulative_risk_metrics
first_msg = gen.next()
first_msg = next(gen)
self.assertIsNotNone(first_msg,
"There should be a message emitted.")
@@ -310,7 +310,7 @@ class TestEventsThroughRisk(unittest.TestCase):
crm.metrics.algorithm_volatility[algo.datetime.date()],
"On the first day algorithm volatility does not exist.")
second_msg = gen.next()
second_msg = next(gen)
self.assertIsNotNone(second_msg, "There should be a message "
"emitted.")
@@ -325,7 +325,7 @@ class TestEventsThroughRisk(unittest.TestCase):
crm.algorithm_returns[-1],
decimal=6)
third_msg = gen.next()
third_msg = next(gen)
self.assertEqual(1, len(algo.portfolio.positions),
"Number of positions should stay the same.")
+2 -2
View File
@@ -31,7 +31,7 @@ class TestDataFrameSource(TestCase):
assert isinstance(source.end, pd.lib.Timestamp)
for expected_dt, expected_price in df.iterrows():
sid0 = source.next()
sid0 = next(source)
assert expected_dt == sid0.dt
assert expected_price[0] == sid0.price
@@ -74,4 +74,4 @@ class TestDataFrameSource(TestCase):
for check_field in check_fields:
self.assertIn(check_field, event)
self.assertTrue(isinstance(event['volume'], (integer_types)))
self.assertEqual(stocks_iter.next(), event['sid'])
self.assertEqual(next(stocks_iter), event['sid'])
+3
View File
@@ -62,3 +62,6 @@ class DataSource(object):
def next(self):
return self.mapped_data.next()
def __next__(self):
return next(self.mapped_data)
+3
View File
@@ -166,6 +166,9 @@ class SpecificEquityTrades(object):
def next(self):
return self.generator.next()
def __next__(self):
return next(self.generator)
def rewind(self):
self.generator = self.create_fresh_generator()