BappoHotel
BappoHotel

Reputation: 112

Code Loop Fails After an Hour of Nonstop Running

I've made a piece of code to try to challenge myself a bit with python, as I'm relatively new, but this one has me stumped.

I've been running some code, and the part that failed is supposed to check the time it takes to complete 1 million iterations, and then log these times every 10 million iterations, when the program saves its progress. The program had been running for about an hour, with me checking in every 10-20 minutes, when I came back and saw this error message:

Traceback (most recent call last):
  File "C:\Users\BruhK\PycharmProjects\Conjecture.py", line 101, in <module>
    run(stl[0], stl[1], stl[2], stl[3])
  File "C:\Users\BruhK\PycharmProjects\Conjecture.py", line 78, in run
    ntimestr = ntimestr + timesplit.pop(0)
IndexError: pop from empty list

I've gone over the code again and again trying to figure out what would cause this, but found nothing. I considered adding the following code:

try:
    if timehold != 0:
        timediff = time.time()*1000 - timehold
        timestr = str(timediff)
        timesplit = Functions.split(timestr)
        ntimestr = ""
        for x in range(7):
            ntimestr = ntimestr + timesplit.pop(0)
            timelist.append(ntimestr)
            print("Completed in {} ms.".format(ntimestr))
        timehold = time.time()*1000
except IndexError as err:
    # write err to a log file
    pass

I don't want to do this, however. I'd rather find a proper solution. Any help with this would be appreciated.

(Unrelated) at the moment I'm saving the progress of the program to a file on my computer, then running a separate piece of code to export it to a spreadsheet to make a graph of the timings, but I plan to combine the two once I get this resolved.

import time
...

def run(snum, maxval, maxitn, maxtrialnum):
    itn = snum
    timehold = 0
    timelist = []
    if itn == 0:
        itn = 1
    val = False
    while not val:
        trialnum = 0
        value = itn
        val2 = False
        if itn % 10000000 == 0 and itn > 9999999:
            f = open("conjecturestore.txt", "w+")
            f.write(str(itn))
            f.close()
            f = open("conjecturedata.txt", "w+")
            f.write("maxval:{}\nmaxitn:{}\nmaxtrialnum:{}".format(maxval, maxitn, maxtrialnum))
            f.close()
            f = open("conjecturetimings.txt", "a+")
            for x in range(len(timelist)):
                f.write("{}\n".format(timelist.pop(0)))
            f.close()
        while not val2:
            if itn % 2 == 0:
                val2 = True
                if itn % 1000000 == 0:
                    print("MI+{} MV+{} MTR+{}".format(maxitn, int(maxval), maxtrialnum))
                    print("Inconclusive sample on I+{} V+{} TR+{}.".format(itn, int(value), trialnum))
                    if timehold != 0:
                        timediff = time.time()*1000 - timehold
                        timestr = str(timediff)
                        timesplit = Functions.split(timestr)
                        ntimestr = ""
                        for x in range(7):
                            ntimestr = ntimestr + timesplit.pop(0)
                        timelist.append(ntimestr)
                        print("Completed in {} ms.".format(ntimestr))
                    timehold = time.time()*1000

Upvotes: 1

Views: 59

Answers (1)

BappoHotel
BappoHotel

Reputation: 112

for x in range(7): 
    ntimestr = ntimestr + timesplit.pop(0)

to give that error, your timesplit list has to be shorter then 7 elements. You can test that and log it out to avoid the error. You can try: except: handle the error. Without the data you operate on, nobody here can help you. And we do not want that data - bc this is not a minimal reproducible example. The error is quite self explanatory and has lots of potentials dupes around - time to get hands on and debug your code. -- Patrick Artner"

Very simple mistake on my part. I've done a bit of testing and this ended up being the problem, just flew over my head.

Upvotes: 1

Related Questions