Ad
How Do I Reference Multiple Variables In A For Loop?
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
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad