Skip to content

Commit 9fdc2b2

Browse files
cclaussbusunkim96
authored andcommitted
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
1 parent c977304 commit 9fdc2b2

File tree

6 files changed

+19
-11
lines changed

6 files changed

+19
-11
lines changed

describe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
The documentation is generated from a combination of the discovery document and
2121
the generated API surface itself.
2222
"""
23+
from __future__ import print_function
2324

2425
__author__ = '[email protected] (Joe Gregorio)'
2526

@@ -354,7 +355,7 @@ def document_api(name, version):
354355
try:
355356
service = build(name, version)
356357
except UnknownApiNameOrVersion as e:
357-
print 'Warning: {} {} found but could not be built.'.format(name, version)
358+
print('Warning: {} {} found but could not be built.'.format(name, version))
358359
return
359360

360361
http = build_http()

googleapiclient/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
156156
LOGGER.warning(
157157
'Sleeping %.2f seconds before retry %d of %d for %s: %s %s, after %s',
158158
sleep_time, retry_num, num_retries, req_type, method, uri,
159-
resp.status if resp else exception)
159+
resp.status if resp else Exception)
160160
sleep(sleep_time)
161161

162162
try:

samples/audit/audit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
__author__ = '[email protected] (Rahul Paul)'
3939

4040
import pprint
41+
import re
4142
import sys
4243

4344
from oauth2client import client

samples/coordinate/coordinate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def main(argv):
9292

9393
pprint.pprint(update_result)
9494

95-
except AccessTokenRefreshError as e:
95+
except client.AccessTokenRefreshError as e:
9696
print ('The credentials have been revoked or expired, please re-run'
9797
'the application to re-authorize')
9898

samples/searchconsole/search_analytics_api_sample.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
$ python search_analytics_api_sample.py 'https://www.example.com/' '2015-05-01' '2015-05-30'
3535
3636
"""
37+
from __future__ import print_function
3738

3839
import argparse
3940
import sys
@@ -176,22 +177,22 @@ def print_table(response, title):
176177
response: The server response to be printed as a table.
177178
title: The title of the table.
178179
"""
179-
print '\n --' + title + ':'
180-
180+
print('\n --' + title + ':')
181+
181182
if 'rows' not in response:
182-
print 'Empty response'
183+
print('Empty response')
183184
return
184185

185186
rows = response['rows']
186187
row_format = '{:<20}' + '{:>20}' * 4
187-
print row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position')
188+
print(row_format.format('Keys', 'Clicks', 'Impressions', 'CTR', 'Position'))
188189
for row in rows:
189190
keys = ''
190191
# Keys are returned only if one or more dimensions are requested.
191192
if 'keys' in row:
192193
keys = u','.join(row['keys']).encode('utf-8')
193-
print row_format.format(
194-
keys, row['clicks'], row['impressions'], row['ctr'], row['position'])
194+
print(row_format.format(
195+
keys, row['clicks'], row['impressions'], row['ctr'], row['position']))
195196

196197
if __name__ == '__main__':
197198
main(sys.argv)

samples/searchforshopping/pagination.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
from googleapiclient.discovery import build
1111

12+
try:
13+
input = raw_input
14+
except NameError:
15+
pass
16+
1217

1318
SHOPPING_API_VERSION = 'v1'
1419
DEVELOPER_KEY = 'AIzaSyACZJW4JwcWwz5taR2gjIMNQrtgDLfILPc'
@@ -30,8 +35,8 @@ def main():
3035
itemsPerPage = response['itemsPerPage']
3136
totalItems = response['totalItems']
3237
for i in range(1, totalItems, itemsPerPage):
33-
answer = raw_input('About to display results from %s to %s, y/(n)? ' %
34-
(i, i + itemsPerPage))
38+
answer = input('About to display results from %s to %s, y/(n)? ' %
39+
(i, i + itemsPerPage))
3540
if answer.strip().lower().startswith('n'):
3641
# Stop if the user has had enough
3742
break

0 commit comments

Comments
 (0)