From 9e1d77b24d860b61356509cbf878c8f11b4537eb Mon Sep 17 00:00:00 2001 From: cclauss Date: Sun, 21 Jul 2019 13:31:22 +0200 Subject: [PATCH] Use print() function in both Python 2 and Python 3 Legacy __print__ statements are syntax errors in Python 3 but __print()__ function works as expected in both Python 2 and Python 3. --- test/python/monitoring/monitoring.py | 33 ++++++++++++++-------------- test/python/speech/speech.py | 3 ++- test/python/trace/trace.py | 15 +++++++------ test/python/vision/vision.py | 3 ++- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/test/python/monitoring/monitoring.py b/test/python/monitoring/monitoring.py index 843bda464..12645e454 100644 --- a/test/python/monitoring/monitoring.py +++ b/test/python/monitoring/monitoring.py @@ -1,4 +1,5 @@ """System tests for monitoring.""" +from __future__ import print_function from google.cloud.gapic.monitoring.v3 import group_service_client @@ -20,37 +21,37 @@ def testGroupCollection(): group = group_pb2.Group(display_name='displayName', filter='resource.type = gce_instance') createdGroup = api.create_group(projectName, group) - print "New group created: \n%s\n" % createdGroup + print("New group created: \n%s\n" % createdGroup) groupName = createdGroup.name groupToUpdate = group_pb2.Group(name=groupName, display_name='displayName2', filter='resource.type = gce_instance') updatedGroup = api.update_group(groupToUpdate) - print "Group updated: \n%s\n" % updatedGroup + print("Group updated: \n%s\n" % updatedGroup) # group id is the last segment in the returned group name. groupId = updatedGroup.name[createdGroup.name.rfind('/')+1:] api = group_service_client.GroupServiceClient() name = api.group_path(PROJECT_ID, groupId) response = api.get_group(name) - print "Group fetched: \n%s\n" % response + print("Group fetched: \n%s\n" % response) - print "List groups:\n" + print("List groups:\n") for group in api.list_groups(name): # process element print(group) - print "List group members:\n" + print("List group members:\n") name = api.group_path(PROJECT_ID, groupId) for member in api.list_group_members(name=name, interval=common_pb2.TimeInterval(end_time=timestamp_pb2.Timestamp(seconds=1475262206))): print(member) - print "Delete groups:\n" + print("Delete groups:\n") name = api.group_path(PROJECT_ID, groupId) api.delete_group(name) try: response = api.get_group(name) except: - print "Expect error here since the group should have been deleted" + print("Expect error here since the group should have been deleted") def testMetricCollection(): @@ -60,20 +61,20 @@ def testMetricCollection(): metric_descriptor = api_metric_pb2.MetricDescriptor(type=custom_metric_type, metric_kind=enums.MetricDescriptor.MetricKind.GAUGE, value_type=enums.MetricDescriptor.ValueType.BOOL) response = api.create_metric_descriptor(name, metric_descriptor) - print "Metric created: \n%s\n" % response + print("Metric created: \n%s\n" % response) name = api.metric_descriptor_path_path(PROJECT_ID, custom_metric_type) response = api.get_metric_descriptor(name) - print "Metric fetched: \n%s\n" % response + print("Metric fetched: \n%s\n" % response) - print "List metrics:" + print("List metrics:") name = api.project_path(PROJECT_ID) # Iterate over all results for metric in api.list_metric_descriptors(name): # process element - print metric + print(metric) - print "delete metric:" + print("delete metric:") name = api.metric_descriptor_path_path(PROJECT_ID, custom_metric_type) api.delete_metric_descriptor(name) @@ -94,22 +95,22 @@ def testTimeseriesCollection(): view = enums.ListTimeSeriesRequest.TimeSeriesView.FULL for element in api.list_time_series(name, filter_, interval, view): # process element - print element + print(element) def testMonitoredResourceCollection(): api = metric_service_client.MetricServiceClient() name = api.project_path(PROJECT_ID) - print "List monitored reourses:" + print("List monitored reourses:") # Iterate over all results for element in api.list_monitored_resource_descriptors(name): # process element - print element + print(element) name = api.monitored_resource_descriptor_path(PROJECT_ID, 'uptime_url') response = api.get_monitored_resource_descriptor(name) - print "Get monitored resource\n%s" % response + print("Get monitored resource\n%s" % response) testGroupCollection() diff --git a/test/python/speech/speech.py b/test/python/speech/speech.py index 42dd4e5c9..38a9f7bda 100644 --- a/test/python/speech/speech.py +++ b/test/python/speech/speech.py @@ -1,3 +1,4 @@ +from __future__ import print_function from google.cloud.gapic.speech.v1beta1 import enums from google.cloud.gapic.speech.v1beta1 import speech_client from google.cloud.proto.speech.v1beta1 import cloud_speech_pb2 as types @@ -17,7 +18,7 @@ def test_sync_recognize(): audio = types.RecognitionAudio(uri=INPUT_URI) response = api.sync_recognize(config, audio) - print response + print(response) def test_async_recognize(): def callback(operation_future): diff --git a/test/python/trace/trace.py b/test/python/trace/trace.py index 6d7c2979b..a942c2d9e 100644 --- a/test/python/trace/trace.py +++ b/test/python/trace/trace.py @@ -1,3 +1,4 @@ +from __future__ import print_function from google.cloud.gapic.trace.v1 import enums from google.cloud.gapic.trace.v1 import trace_service_api from google.devtools.cloudtrace.v1 import trace_pb2 @@ -28,23 +29,23 @@ trace = trace_pb2.Trace(project_id=PROJECT_ID, trace_id=id, spans=[span]) traces = trace_pb2.Traces(traces=[trace]) -print 'testing patch' +print('testing patch') api.patch_traces(PROJECT_ID, traces) -print 'patched' +print('patched') # 1 second seems to be sufficient to avoid race condition time.sleep(1) -print 'testing get' +print('testing get') get_response = api.get_trace(PROJECT_ID, id) -print 'got trace: {}'.format(get_response) +print('got trace: {}'.format(get_response)) -print 'testing list' +print('testing list') list_response = api.list_traces(PROJECT_ID) -print 'listed traces:' +print('listed traces:') i = 0 for trace in list_response: if i > 10: break i += 1 - print '> {}'.format(trace) + print('> {}'.format(trace)) diff --git a/test/python/vision/vision.py b/test/python/vision/vision.py index e8f1df805..9906dc24d 100644 --- a/test/python/vision/vision.py +++ b/test/python/vision/vision.py @@ -1,4 +1,5 @@ """Functional test for Google Cloud Vision API.""" +from __future__ import print_function from google.cloud.gapic.vision.v1.image_annotator_client import ImageAnnotatorClient from google.cloud.grpc.vision.v1 import image_annotator_pb2 @@ -19,4 +20,4 @@ requests = [request] response = api.batch_annotate_images(requests) -print response.__str__ +print(response.__str__)