I'm working with some iterator generated using itertools.imap
, and I was thinking if there is a way to access the iterator length inside the for-loop
that I use to loop over the elements.
What I can say for sure is that the iterator doesn't generate an infinite amount of data.
Also, because the information I'm looping are from a query to a database, I can get the length of the information from there, but the function I'm using has to return an iterator.
I thought of some options:
def iterator_function(some, arguments, that, I, need):
query_result = query()
return_iterator = itertools.imap(
mapping_function,
query_result
)
return return_iterator
Because I cannot change the returned iterator, I thought of something (really ugly) like:
query_result = query()
query_size = len(query_result)
return_iterator = itertools.imap(
lambda item: (mapping_function(item), query_size),
query_result
)
return return_iterator
But I don't really like this option, and I was thinking if there is a way, in Python, to get the iterator size from inside the loop over the iterator, something like:
for item in iterator():
print item.iterator_length()
# do other stuff
Or even something like:
for item in iterator():
print iterator.length() # or iterator().length()???
Thanks!