@@ -68,10 +68,10 @@ def delete(self):
6868
6969
7070@snippet
71- def client_list_datasets (client , to_delete ): # pylint: disable=unused-argument
71+ def client_list_datasets (client , _ ):
7272 """List datasets for a project."""
7373
74- def do_something_with (sub ): # pylint: disable=unused-argument
74+ def do_something_with (_ ):
7575 pass
7676
7777 # [START client_list_datasets]
@@ -182,7 +182,7 @@ def dataset_update(client, to_delete):
182182
183183
184184@snippet
185- def dataset_delete (client , to_delete ): # pylint: disable=unused-argument
185+ def dataset_delete (client , _ ):
186186 """Delete a dataset."""
187187 DATASET_NAME = 'dataset_delete_%d' % (_millis (),)
188188 dataset = client .dataset (DATASET_NAME )
@@ -439,13 +439,12 @@ def table_upload_from_file(client, to_delete):
439439
440440
441441@snippet
442- def table_delete (client , to_delete ): # pylint: disable=unused-argument
442+ def table_delete (client , _ ):
443443 """Delete a table."""
444444 DATASET_NAME = 'table_delete_dataset_%d' % (_millis (),)
445445 TABLE_NAME = 'table_create_table_%d' % (_millis (),)
446446 dataset = client .dataset (DATASET_NAME )
447447 dataset .create ()
448- to_delete .append (dataset )
449448
450449 table = dataset .table (TABLE_NAME , SCHEMA )
451450 table .create ()
@@ -457,6 +456,122 @@ def table_delete(client, to_delete): # pylint: disable=unused-argument
457456 # [END table_delete]
458457
459458
459+ @snippet
460+ def client_list_jobs (client , _ ):
461+ """List jobs for a project."""
462+
463+ def do_something_with (_ ):
464+ pass
465+
466+ # [START client_list_jobs]
467+ jobs , token = client .list_jobs () # API request
468+ while True :
469+ for job in jobs :
470+ do_something_with (job )
471+ if token is None :
472+ break
473+ jobs , token = client .list_jobs (page_token = token ) # API request
474+ # [END client_list_jobs]
475+
476+
477+ @snippet
478+ def client_run_sync_query (client , _ ):
479+ """Run a synchronous query."""
480+ LIMIT = 100
481+ LIMITED = '%s LIMIT %d' % (QUERY , LIMIT )
482+ TIMEOUT_MS = 1000
483+
484+ # [START client_run_sync_query]
485+ query = client .run_sync_query (LIMITED )
486+ query .timeout_ms = TIMEOUT_MS
487+ query .run () # API request
488+
489+ assert query .complete
490+ assert len (query .rows ) == LIMIT
491+ assert [field .name for field in query .schema ] == ['name' ]
492+ # [END client_run_sync_query]
493+
494+
495+ @snippet
496+ def client_run_sync_query_paged (client , _ ):
497+ """Run a synchronous query with paged results."""
498+ TIMEOUT_MS = 1000
499+ PAGE_SIZE = 100
500+ LIMIT = 1000
501+ LIMITED = '%s LIMIT %d' % (QUERY , LIMIT )
502+
503+ all_rows = []
504+
505+ def do_something_with (rows ):
506+ all_rows .extend (rows )
507+
508+ # [START client_run_sync_query_paged]
509+ query = client .run_sync_query (LIMITED )
510+ query .timeout_ms = TIMEOUT_MS
511+ query .max_results = PAGE_SIZE
512+ query .run () # API request
513+
514+ assert query .complete
515+ assert query .page_token is not None
516+ assert len (query .rows ) == PAGE_SIZE
517+ assert [field .name for field in query .schema ] == ['name' ]
518+
519+ rows = query .rows
520+ token = query .page_token
521+
522+ while True :
523+ do_something_with (rows )
524+ if token is None :
525+ break
526+ rows , total_count , token = query .fetch_data (
527+ page_token = token ) # API request
528+ # [END client_run_sync_query_paged]
529+
530+ assert total_count == LIMIT
531+ assert len (all_rows ) == LIMIT
532+
533+
534+ @snippet
535+ def client_run_sync_query_timeout (client , _ ):
536+ """Run a synchronous query w/ timeout"""
537+ TIMEOUT_MS = 10
538+
539+ all_rows = []
540+
541+ def do_something_with (rows ):
542+ all_rows .extend (rows )
543+
544+ # [START client_run_sync_query_timeout]
545+ query = client .run_sync_query (QUERY )
546+ query .timeout_ms = TIMEOUT_MS
547+ query .use_query_cache = False
548+ query .run () # API request
549+
550+ assert not query .complete
551+
552+ job = query .job
553+ job .reload () # API rquest
554+ retry_count = 0
555+
556+ while retry_count < 10 and job .state != u'DONE' :
557+ time .sleep (1.5 ** retry_count ) # exponential backoff
558+ retry_count += 1
559+ job .reload () # API request
560+
561+ assert job .state == u'DONE'
562+
563+ rows , total_count , token = query .fetch_data () # API request
564+ while True :
565+ do_something_with (rows )
566+ if token is None :
567+ break
568+ rows , total_count , token = query .fetch_data (
569+ page_token = token ) # API request
570+ # [END client_run_sync_query_timeout]
571+
572+ assert len (all_rows ) == total_count
573+
574+
460575def _find_examples ():
461576 funcs = [obj for obj in globals ().values ()
462577 if getattr (obj , '_snippet' , False )]
@@ -468,7 +583,7 @@ def main():
468583 client = Client ()
469584 for example in _find_examples ():
470585 to_delete = []
471- print ('%-25s : %s' % (
586+ print ('%-30s : %s' % (
472587 example .func_name , example .func_doc ))
473588 try :
474589 example (client , to_delete )
0 commit comments