|
64 | 64 | from larray.core.axis import Axis, AxisReference, AxisCollection, X, _make_axis
|
65 | 65 | from larray.util.misc import (table2str, size2str, basestring, izip, rproduct, ReprString, duplicates,
|
66 | 66 | float_error_handler_factory, _isnoneslice, light_product, unique_list, common_type,
|
67 |
| - renamed_to, deprecate_kwarg, LHDFStore, lazy_attribute) |
| 67 | + renamed_to, deprecate_kwarg, LHDFStore, lazy_attribute, PY2) |
68 | 68 | from larray.util.options import _OPTIONS, DISPLAY_MAXLINES, DISPLAY_EDGEITEMS, DISPLAY_WIDTH, DISPLAY_PRECISION
|
69 | 69 |
|
70 | 70 |
|
@@ -312,20 +312,21 @@ def concat(arrays, axis=0, dtype=None):
|
312 | 312 |
|
313 | 313 | class LArrayIterator(object):
|
314 | 314 | def __init__(self, array):
|
315 |
| - self.array = array |
316 |
| - self.index = 0 |
| 315 | + data_iter = iter(array.data) |
| 316 | + self.nextfunc = data_iter.next if PY2 else data_iter.__next__ |
| 317 | + self.axes = array.axes[1:] |
317 | 318 |
|
318 | 319 | def __iter__(self):
|
319 | 320 | return self
|
320 | 321 |
|
321 | 322 | def __next__(self):
|
322 |
| - array = self.array |
323 |
| - if self.index == len(self.array): |
324 |
| - raise StopIteration |
325 |
| - # result = array.i[array.axes[0].i[self.index]] |
326 |
| - result = array.i[self.index] |
327 |
| - self.index += 1 |
328 |
| - return result |
| 323 | + data = self.nextfunc() |
| 324 | + axes = self.axes |
| 325 | + if len(axes): |
| 326 | + return LArray(data, axes) |
| 327 | + else: |
| 328 | + return data |
| 329 | + |
329 | 330 | # Python 2
|
330 | 331 | next = __next__
|
331 | 332 |
|
@@ -2327,7 +2328,11 @@ def __str__(self):
|
2327 | 2328 | __repr__ = __str__
|
2328 | 2329 |
|
2329 | 2330 | def __iter__(self):
|
2330 |
| - return LArrayIterator(self) |
| 2331 | + # fast path for 1D arrays where we return elements |
| 2332 | + if self.ndim <= 1: |
| 2333 | + return iter(self.data) |
| 2334 | + else: |
| 2335 | + return LArrayIterator(self) |
2331 | 2336 |
|
2332 | 2337 | def __contains__(self, key):
|
2333 | 2338 | return any(key in axis for axis in self.axes)
|
|
0 commit comments