How do I check if an iterator is actually an iterator container?

2024/11/16 19:52:02

I have a dummy example of an iterator container below (the real one reads a file too large to fit in memory):

class DummyIterator:def __init__(self, max_value):self.max_value = max_valuedef __iter__(self):for i in range(self.max_value):yield idef regular_dummy_iterator(max_value):for i in range(max_value):yield i

This allows me to iterate over the value more than once so that I can implement something like this:

def normalise(data):total = sum(i for i in data)for val in data:yield val / total# this works when I call next()
normalise(DummyIterator(100))# this doesn't work when I call next()
normalise(regular_dummy_iterator(100))

How do I check in the normalise function that I am being passed an iterator container rather than a normal generator?

Answer

First of all: There is no such thing as a iterator container. You have an iterable.

An iterable produces an iterator. Any iterator is also an iterable, but produces itself as the iterator:

>>> list_iter = iter([])
>>> iter(list_iter) is list_iter
True

You don't have an iterator if the iter(ob) is ob test is false.

https://en.xdnf.cn/q/71636.html

Related Q&A

Python Terminated Thread Cannot Restart

I have a thread that gets executed when some action occurs. Given the logic of the program, the thread cannot possibly be started while another instance of it is still running. Yet when I call it a sec…

TypeError: NoneType object is not subscriptable [duplicate]

This question already has an answer here:mysqldb .. NoneType object is not subscriptable(1 answer)Closed 8 years ago.The error: names = curfetchone()[0]TypeError: NoneType object is not subscriptable. …

Where can I find numpy.where() source code? [duplicate]

This question already has answers here:How do I use numpy.where()? What should I pass, and what does the result mean? [closed](2 answers)Closed 4 years ago.I have already found the source for the num…

NSUserNotificationCenter.defaultUserNotificationCenter() returns None in python

I am trying to connect to the Mountain Lion notification center via python. Ive installed pyobjc and am following the instructions here and here. Also see: Working with Mountain Lions Notification Cent…

Flask app hangs while processing the request

I have a simple flask app, single page, upload html and then do some processing on it on the POST; at POST request; i am using beautifulsoup, pandas and usually it takes 5-10 sec to complete the task. …

How do I make a server listen on multiple ports

I would like to listen on 100 different TCP port with the same server. Heres what Im currently doing:-import socket import selectdef main():server_socket = socket.socket(socket.AF_INET, socket.SOCK_STR…

Django REST Framework: return 404 (not 400) on POST if related field does not exist?

Im developing a REST API which takes POST requests from some really brain-dead software which cant PATCH or anything else. The POSTs are to update Model objects which already exist in the database.Spec…

How can i login in instagram with python requests?

Hello i am trying to login instagram with python requests library but when i try, instagram turns me "bad requests". İs anyone know how can i solve this problem?i searched to find a solve f…

How to abstract away command code in custom django commands

Im writing custom django commands under my apps management/commands directory. At the moment I have 6 different files in that directory. Each file has a different command that solves a unique need. How…

Python one-liner (converting perl to pyp)

I was wondering if its possible to make a one-liner with pyp that has the same functionality as this.perl -l -a -F, -p -eif ($. > 1) { $F[6] %= 12; $F[7] %= 12;$_ = join(q{,}, @F[6,7]) }This takes i…