0

I am facing some problems regarding a generator. I have a list of 1000 elements. I want to read single item one by one and do some operation. The operation is something like comparing with some specific value. If I able to find that value from the list I want to stop iteration and reset the yield again.

I looking for the funtionality how to reset __next__ pointer in generator. Also I have to make 100 object in runtime FN_SOVLS.

class FN_SOV1S:

    def __init__(self,elementlist,idxNo):
        self._elementlist = elementlist
        self._idxNo =idxNo
        setup()
        process()


    def setup(self):
        try:
            self.df = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Valve1S')
            for tag,col in self.readcmd():
                if col==4:
                    self.cmd = tag

                if col == 5:
                    self.openFB = tag

                if col == 6:
                    self.clsFB = tag

                if col == 7:
                    self.delaytime = tag

        except Exception as e:
            log_exception(e)



    def process(self):

        for tagname,tagvalue in self.searchValueBytag():
            if tagname == self.cmd:
                if tagvalue == 1:
                    sleep(self.delaytime)
                    gen.writegeneral.writenodevalue(self.openFB,1)
                    gen.writegeneral.writenodevalue(self.clsFB,0)

                else:
                    gen.writegeneral.writenodevalue(self.openFB, 0)
                    gen.writegeneral.writenodevalue(self.clsFB, 1)


    def searchValueBytag(self):
        n = 0
        while n < len(self._elementlist):
            tagname, tagvalue = self._elementlist[n]
            yield tagname, tagvalue
            n =+ 1

The condition is to reset Generator function is:

 for tagname,tagvalue in self.searchValueBytag():
            if tagname == self.cmd:
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • Possible duplicate of [Resetting generator object in Python](https://stackoverflow.com/questions/1271320/resetting-generator-object-in-python) – lcongdon Aug 11 '19 at 18:51

2 Answers2

1

You cannot "reset" a running generator.

What you can do is break out of the for-loop that uses the generator.

Later you can then create the generator anew by calling searchValueBytag again.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • okye , This is understable but problem is suppose my elementlist contains 1000 of elements.If I found this tagname ==cmd then I don't need any iteration and I want to reset Yield. – Subrata Mitra Aug 12 '19 at 14:09
0

I don't completely understand your question, but hopefully, this can help. This uses a flag that will continue restarting the generator until the self.cmd value is no longer found.

Obviously, this is not identical to your code and more is needed for it to work perfectly, but you can easily use this flag to reset the generator

def generator():
    # arbitrary length
    length = 100
    n = 0

    while n < length:
        yield n
        n += 1

# create a complete flag that is only true when the end of the iteration is reached
complete = False

# keep trying until complete is true
while not complete:

    # restarts the generator by making a new one
    g = generator()

    # keeps going until 'break'
    while True:
        # try/catch because next returns error when the end of the generator is reached
        # when the end is reached we know that to turn complete to true
        try:
            # get the next val in the iterator
            value = next(g)

            # if value is the reset flag, then break out of while loop and restart generator
            if value == RESET_FLAG:
                break
        except:
            # StopIteration exeption received, job finished
            complete = True
            break
    def generator():
        length = 100
        n = 0

        while n < length:
           yield n
           n += 1
Grobbed
  • 317
  • 2
  • 12