- edited description
Reiteration over the reader is unintuitive
Issue #48
new
The way iteration works (only tested in python3) acts very oddly. My testcase is below.
with SavReader(savFileName=file_name) as reader:
first_row = reader[0]
second_row = reader[1]
# Just checking I get the same thing, this works
print(first_row == reader[0])
print(second_row == reader[1])
# Prints: True
# Prints: True
# Again still works, just checking
print(second_row != first_row)
# Prints: True
for i, r in enumerate(reader):
if i == 0:
# Here's the first issue, when iterating over the reader, I expect the first row
print(r == first_row)
# Prints: False
# but get the third row, see?
print(r == reader[2])
# Prints: True
break
# Okay lets finish iterating
for r in reader:
pass
test = iter(reader)
before = next(test)
for i, r in enumerate(reader):
# And here's the other issue, after you've finished iterating over the whole thing once
# you just get the same (last) entry in an infinite loop
print(before == r)
# Prints: True (every time)
if i > 10:
break
By subclassing it with the following code, I get expected behavior, but there's obviously probably a better way inside the library itself.
class SavReaderWrapper(SavReader):
def __iter__(self):
def iterator():
for i in range(self.shape.nrows):
yield self[i]
return iterator()
Comments (3)
-
reporter -
reporter - edited description
-
repo owner Hi Dan,
Thanks for reporting this. I think this stems from the fact that
__getitem__
and__iter__
share much of each other's code in a helper method. I have been staring at my own code for a while and concluded that I'd better re-write both methods. I also did that withSavReaderNp
and it has become a lot cleaner. I hope I have time for this soon.Best wishes, Albert-Jan
- Log in to comment