-2

I am having some trouble with a script which produces an IndexError around 1 out 1000 times its run.

The script is an add on for a piece of software that I cant actually edit or it crashes the external software im using this for.

I am calling this script multiple times from my own script is there any way to detect if an error has occurred and loop back to try running the script again? (The script has multiple random elements, this is why the risk to repeated failures is low). I would prefer to go into the add on script and edit it manually but this doesnt appear to be an option

I have the following using the PYPI goto statement (https://pypi.org/project/goto-statement/) and using except, however the error doesnt appear to be recognized and I get the same error message as I would get without the goto or except statements.

def function (stuff thats passed into the other script):
    for i in range(5000):
        print(i)
        try:
            runscript (info passed into script) 
        except IndexError:
            print(i,'fails')
            continue
MattC1990
  • 57
  • 6
  • Please, at least replace the goto with continue. Generally you should avoid goto as it can cause wierd code timelines and make it difficult to read. – CATboardBETA Jan 14 '21 at 19:39
  • Apologies, this is not the full code, just a guide (the software im using uses its branch of python, so ive tried to simplify it a little) also im relativly new to python and still learning – MattC1990 Jan 14 '21 at 19:40

2 Answers2

0

There are many things wrong with this, lets go over it:

from goto import with_goto

You should avoid using goto as it can cause strange code timelines and make it difficult to read or understand your code.

def function:

You forgot your parenthesis, it should look like def function():

for i in range(5000):

Replace this with while i < 5000

print(i,'fails')

If you are trying to make an error message, use raise().

i=i+1

In a python for loop, it automatically increments.

CATboardBETA
  • 418
  • 6
  • 29
  • I am trying to edit this as I get comments, there are 5000 entries for a reason, this is actually a value calculated elsewhere in teh code, this is purely a placeholder for this page, this can be replaced by while i<5000 though. – MattC1990 Jan 14 '21 at 19:47
  • @MattC1990 Ah. It appeared to me like you just wanted it to loop for a long time. My apologies. – CATboardBETA Jan 14 '21 at 19:49
  • No problems, in trying to simplify my (albeit woeful) code, I think I have made this more confusing – MattC1990 Jan 14 '21 at 19:51
  • @MattC1990 Oh, well, we all write bad code from time to time (or in my case constantly) – CATboardBETA Jan 14 '21 at 19:54
0

Can you ... just ... not?

def func():
  for i in range(5000):
    try:
      runscript
    except IndexError:
      return False
  return True

while not func():
  pass

This will run your function until it manages to do whatever runscript is 5000 times in a row, but it doesn't have any weird bytecode manipulation. There are other ways as well:

successes = 0
while successes < 5000:
  try:
    runscript
    successes += 1
  except IndexError:
    successes = 0

It should be clear that this is a Bad Idea™ based on this quote at the bottom of that page:

The idea of goto in Python isn't new. There is another module that has been released as April Fool's joke in 2004.

If you're trying to use something that was originally released as an April Fool's joke, there's probably a better way.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111