Ad

How Do I Reference Multiple Variables In A For Loop?

- 1 answer

I am writing a script that submits multiple (30) SQL queries to Google BigQuery. What is the best way to loop through the queries? My code works but it doesn't feel very Pythonic.

I need to pass through the query name within the job_id and submit the query.

def run_query(query,job_id):
    try:
        query_job = client.query(query,job_id=job_id)
        polling = 1
        while query_job.done() is False:
            if "q1_" in job_id:
                time.sleep(20)
                print("Job State : {} - Polling : {}".format(query_job.state,polling))
                polling +=1 
                query_job.reload()
            else:
                time.sleep(1)
                print("Job State : {} - Polling : {}".format(query_job.state,polling))
                polling +=1 
                query_job.reload()                     
    except Conflict as err:
        print("Could not run Query.  System Message: \n{}".format(err))
        sys.exit()


q1 = """SELECT * FROM XYZ"""
q2 = """SELECT TOP 10 * FROM YZF"""
q3 = """select id from fjfj"""
q4 = """SELECT * FROM XYZ"""
q5 = """SELECT TOP 10 * FROM YZF"""
q6 = """select id from fjfj"""

query_jobs = [q1,q2,q3,q4,q5,q6]

q = 0

for query in query_jobs:
    randid = str(uuid.uuid4())
    q+=1
    queries = "q"+str(q)
    job_id = queries+"_"+randid
    run_query(query,job_id)
    print job_id

Ad

Answer

Looks fine to me, you could improve upon this slightly by using enumerate in your loop instead of a counter:

for i, query in enumerate(query_jobs):
    randid = str(uuid.uuid4())
    queries = "q"+str(i)
    job_id = queries+"_"+randid
    run_query(query,job_id)
    print job_id
Ad
source: stackoverflow.com
Ad