Mar-28-2021, 08:02 PM
Hello,
I created this class:
I created this class:
class ssource:
def __init__(self, n):
self.list1=[]
self.list2=[]
self.i=0
self.max=n
self.At=stream(self, self.list1)
self.Bt=stream(self, self.list2)
def __next__(self):
if self.i>=self.max: raise StopIteration
print(f"NEXTSYNC:{self.i}")
self.list1.append(f'A{self.i}')
self.list2.append(f'B{self.i}')
self.i += 1And I am testing my class with this code script:src=ssource(10)
Ai=src.Ait
Bi=src.Bit
for i in range(5):
print(f'A->{next(Ai)}')
for i in range(8):
print(f'B->{next(Bi)}')
for a in Ai : print(f'A->{a}')And I am getting this error:Error:NEXTSYNC:0
A->A0
NEXTSYNC:1
A->A1
NEXTSYNC:2
A->A2
NEXTSYNC:3
A->A3
NEXTSYNC:4
A->A4
B->B0
B->B1
B->B2
B->B3
B->B4
NEXTSYNC:5
B->B5
NEXTSYNC:6
B->B6
NEXTSYNC:7
B->B7
A->A5
A->A6
A->A7
NEXTSYNC:8
A->A8
NEXTSYNC:9
A->A9
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-36-b322da59c757> in stream(src, fifo)
4 while True:
----> 5 if len(fifo)==0: next(src)
6 yield(fifo.pop(0))
<ipython-input-36-b322da59c757> in __next__(self)
17 def __next__(self):
---> 18 if self.i>=self.max: raise StopIteration
19 print(f"NEXTSYNC:{self.i}")
StopIteration:
The above exception was the direct cause of the following exception:
RuntimeError Traceback (most recent call last)
<ipython-input-39-9b42e44489d5> in <module>
7 for i in range(8):
8 print(f'B->{next(Bi)}')
----> 9 for a in Ai : print(f'A->{a}')
RuntimeError: generator raised StopIterationhere is my another functiondef stream(src, flist):
try:
while True:
while len(flist)==0: next(src)
yield(flist.pop(0))
except StopIteration:
pass
# raise StopIteration
