I have attempted to write some code in python to do a loop 4 times. It will fail as spTest
doesn't exist. So I want to try loop again (repeated 4 times total) if it still can't find it, I want to break out and raise an error.
import traceback
import urllib
from datetime import datetime
import numpy as np
import pandas as pd
import sqlalchemy as db
from sqlalchemy import event
import logging
from tqdm import tqdm
import smtplib
import ssl
from time import sleepdef test():for x in range(0, 4): # try 4 timestry:df = pd.read_sql(sql='EXEC [prod].[spTest]', con=engine, )str_error = Noneexcept Exception as str_error:print(str_error)passif str_error:sleep(2) # wait for 2 seconds before trying to fetch the data againelse:breakprint(df)
Error I get:
if str_error: UnboundLocalError: local variable 'str_error' referenced before assignment
Updated code:
def test():str_error = Nonefor x in range(0, 4): # try 4 timestry:df = pd.read_sql(sql='EXEC [prod].[spTest]', con=engine, )except Exception as str_error:print(str_error)if str_error:sleep(2)else:breakprint(df)
Issue: No longer loops 4 times.