Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stop Iteration Error
#1
Hello,

here is my class:
class inportProp:
    
    def __init__(self, t0, tM, incoming, phys, clinkdt, otherport=None, name=''):
        self.DEBUG=True 
        self.lastt=t0
        self.tM=tM
        if self.tM == 0: 
            def updateqmem(t):
                pass 
        else :
            def updateqmem(t):
                 
                perase=-expm1(-(t-self.lastt)/self.tM)
                for i,q in enumerate(self.physqueue): 
                    if random()<perase:
                        q.lost() 
                        
                        del self.physqueue[i]
        self.updateqmem=updateqmem
        self.incoming=incoming
        self.phys=phys
        self.physqueue=[]
        self.dataqueue=[]
        self.clinkdt=clinkdt
        self.otherport=otherport
        if otherport != None:
            if otherport.otherport==None :
                otherport.otherport=self
                otherport.clinkdt=clinkdt
            else: raise ValueError(f"{otherport} already ok") 
            self.nextitemin()
            self.otherport.nextitemin()
        self.name=name
 
    def __next__(self):
        while True:
            if len(self.dataqueue)==0:
                self.otherport.nextitemin() 
            while self.lastt < self.dataqueue[0][0]: 
                self.nextitemin()
            td, cdata = self.dataqueue.pop(0)
            self.updateqmem(td)
            self.lastt=td
            while len(self.physqueue)>0 and self.physqueue[0][1].idt<cdata:
                self.physqueue.pop(0) 
            if  len(self.physqueue)>0 and self.physqueue[0][1].idt==cdata:
                tq, qb = self.physqueue.pop(0)
                return [td, qb]
         
    def nextitemin(self):
        tq, q = next(self.incoming)
        self.updateqmem(tq)
        if self.phys==0 or len(self.physqueue)<=self.phys:
            self.physqueue.append([tq, q]) 
        self.lastt=tq
        self.otherport.dataqueue.append([tq+self.clinkdt, q.idt])
        if self.DEBUG :
            print(f'{self.name}.nextitemin')
            self.showstate()
 
    def showstate(self):
        print(f'{self.name}, t={self.lastt}')
        print(f'Physqueue:{self.physqueue}')
        print(f'Dataqueue:{self.dataqueue}')
here the code that I used for testing:

src=source(rate=.5,eta=1)
print("PAIRS************************",src)
Abits=link(eta=.8, deltat=1.5, inputs=src.portA)
Bbits=link(eta=0.7, deltat=3.2, inputs=src.portB)
for x in Abits:
    print(f"{x} —> {x[1].idt}, {x[1].QEO.state}")
    print('=')
    print(f'fifos: A {len(src.Afifo)}, B {len(src.Bfifo)}')
print(10*"=")
for x in Bbits:
    print(f"{x} —> {x[1].idt}, {x[1].QEO.state}")
    print(f'fifos: A {len(src.Afifo)}, B {len(src.Bfifo)}')


inport1=inportProp(t0=0, tM=0, incoming=Abits, phys=10, clinkdt=4.7, name='A')
inport2=inportProp(t0=0, tM=0, incoming=Bbits, phys=10, clinkdt=4.7, otherport=inport1, name='B')
And I am getting this error:
Error:
... --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) <ipython-input-128-6a60e37ecdbf> in <module> 14 15 inport1=inportProp(t0=0, tM=200.0, inbits=Abits, phys=10, clinkdt=4.7,name='A') ---> 16 inport2=inportProp(t0=0, tM=200.0, inbits=Bbits, phys=10, clinkdt=4.7, otherport=Ainport, name='B')#otherport=Ainport, 17 18 <ipython-input-127-5d7a8dbcb4e0> in __init__(self, t0, tM, inbits, phys, clinkdt, otherport, name) 42 otherport.clinkdt=clinkdt 43 else: raise ValueError(f"{otherport} already connected to someone else") ---> 44 self.nextitemin() 45 self.otherport.nextitemin() 46 <ipython-input-127-5d7a8dbcb4e0> in nextitemin(self) 64 65 def nextitemin(self): ---> 66 tq, q = next(self.inbits) 67 self.updateqmem(tq) 68 if self.phys==0 or len(self.physqueue)<=self.physqueuesize: StopIteration:
However if I do not write this part:

for x in Abits:
    print(f"{x} —> {x[1].idt}, {x[1].QEO.state}")
    print('=')
    print(f'fifos: A {len(src.Afifo)}, B {len(src.Bfifo)}')
print(10*"=")
for x in Bbits:
    print(f"{x} —> {x[1].idt}, {x[1].QEO.state}")
    print(f'fifos: A {len(src.Afifo)}, B {len(src.Bfifo)}')
code is working..
Probably some timing/ memory problem and I need skip stop Iteration part and maybe I need to write a function for that but I do not know how to do

Any helps...
Bests
Reply
#2
The StopIteration exception does not necessarily mean an error. It is the way Python indicates the end of a sequence when you call the next() function. It may simply mean that there are no more items in the sequence.
quest likes this post
Reply
#3
(Mar-30-2021, 08:49 AM)Gribouillis Wrote: The StopIteration exception does not necessarily mean an error. It is the way Python indicates the end of a sequence when you call the next() function. It may simply mean that there are no more items in the sequence.

how can I pass it? because really I do not want to see it. It appears the line of
tq, q = next(self.incoming)
then it is going next function and then stop iteration...
How to manage with that because I change nothing in the code and sometimes it is perfectly fine but sometimes "StopItreration" and ot is very annoying since I change nothing...
Reply
#4
If you want to pass it you need to say what the program has to do when there are no more items in the sequence
try:
    tq, q = next(self.incoming)
except StopIteration:
    # No more items in self.incoming --> what to do here?
You can also catch the exception at higher level in the calling stack.
quest likes this post
Reply
#5
(Mar-30-2021, 09:19 AM)Gribouillis Wrote: If you want to pass it you need to say what the program has to do when there are no more items in the sequence
try:
    tq, q = next(self.incoming)
except StopIteration:
    # No more items in self.incoming --> what to do here?
You can also catch the exception at higher level in the calling stack.

this time I am in a never endless loop...

I will think and try something and will let you know..
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to make this error stop ? Mawixy 1 4,291 Apr-19-2022, 03:02 PM
Last Post: Mawixy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020