Use print() function in both Python 2 and Python 3 (#722)
* 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.
* Fix undefined names
diff --git a/describe.py b/describe.py
index eb7a0f1..636eb5d 100755
--- a/describe.py
+++ b/describe.py
@@ -20,6 +20,7 @@
The documentation is generated from a combination of the discovery document and
the generated API surface itself.
"""
+from __future__ import print_function
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
@@ -354,7 +355,7 @@
try:
service = build(name, version)
except UnknownApiNameOrVersion as e:
- print 'Warning: {} {} found but could not be built.'.format(name, version)
+ print('Warning: {} {} found but could not be built.'.format(name, version))
return
http = build_http()
diff --git a/googleapiclient/http.py b/googleapiclient/http.py
index e795274..a57f83d 100644
--- a/googleapiclient/http.py
+++ b/googleapiclient/http.py
@@ -156,7 +156,7 @@
LOGGER.warning(
'Sleeping %.2f seconds before retry %d of %d for %s: %s %s, after %s',
sleep_time, retry_num, num_retries, req_type, method, uri,
- resp.status if resp else exception)
+ resp.status if resp else Exception)
sleep(sleep_time)
try:
diff --git a/samples/audit/audit.py b/samples/audit/audit.py
index 97f2773..839a8c3 100644
--- a/samples/audit/audit.py
+++ b/samples/audit/audit.py
@@ -38,6 +38,7 @@
__author__ = 'rahulpaul@google.com (Rahul Paul)'
import pprint
+import re
import sys
from oauth2client import client
diff --git a/samples/coordinate/coordinate.py b/samples/coordinate/coordinate.py
index 16775eb..e177f6d 100644
--- a/samples/coordinate/coordinate.py
+++ b/samples/coordinate/coordinate.py
@@ -92,7 +92,7 @@
pprint.pprint(update_result)
- except AccessTokenRefreshError as e:
+ except client.AccessTokenRefreshError as e:
print ('The credentials have been revoked or expired, please re-run'
'the application to re-authorize')
diff --git a/samples/searchconsole/search_analytics_api_sample.py b/samples/searchconsole/search_analytics_api_sample.py
index 167ec26..def3687 100644
--- a/samples/searchconsole/search_analytics_api_sample.py
+++ b/samples/searchconsole/search_analytics_api_sample.py
@@ -34,6 +34,7 @@
$ python search_analytics_api_sample.py 'https://www.example.com/' '2015-05-01' '2015-05-30'
"""
+from __future__ import print_function
import argparse
import sys
@@ -176,22 +177,22 @@
response: The server response to be printed as a table.
title: The title of the table.
"""
- print '\n --' + title + ':'
-
+ print('\n --' + title + ':')
+
if 'rows' not in response:
- print 'Empty response'
+ print('Empty response')
return
rows = response['rows']
row_format = '{:<20}' + '{:>20}' * 4
- print row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position')
+ print(row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position'))
for row in rows:
keys = ''
# Keys are returned only if one or more dimensions are requested.
if 'keys' in row:
keys = u','.join(row['keys']).encode('utf-8')
- print row_format.format(
- keys, row['clicks'], row['impressions'], row['ctr'], row['position'])
+ print(row_format.format(
+ keys, row['clicks'], row['impressions'], row['ctr'], row['position']))
if __name__ == '__main__':
main(sys.argv)
diff --git a/samples/searchforshopping/pagination.py b/samples/searchforshopping/pagination.py
index 89bee7c..026f29a 100644
--- a/samples/searchforshopping/pagination.py
+++ b/samples/searchforshopping/pagination.py
@@ -9,6 +9,11 @@
from googleapiclient.discovery import build
+try:
+ input = raw_input
+except NameError:
+ pass
+
SHOPPING_API_VERSION = 'v1'
DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'
@@ -30,8 +35,8 @@
itemsPerPage = response['itemsPerPage']
totalItems = response['totalItems']
for i in range(1, totalItems, itemsPerPage):
- answer = raw_input('About to display results from %s to %s, y/(n)? ' %
- (i, i + itemsPerPage))
+ answer = input('About to display results from %s to %s, y/(n)? ' %
+ (i, i + itemsPerPage))
if answer.strip().lower().startswith('n'):
# Stop if the user has had enough
break