Skip to content
Permalink
Browse files

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
  • Loading branch information...
cclauss authored and busunkim96 committed Jul 22, 2019
1 parent c977304 commit 9fdc2b20a998bbac3eed80595f4be7a001d3e652
@@ -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 @@ def document_api(name, version):
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()
@@ -156,7 +156,7 @@ def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
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:
@@ -38,6 +38,7 @@
__author__ = 'rahulpaul@google.com (Rahul Paul)'

import pprint
import re
import sys

from oauth2client import client
@@ -92,7 +92,7 @@ def main(argv):

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')

@@ -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 @@ def print_table(response, title):
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)
@@ -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 @@ def main():
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

0 comments on commit 9fdc2b2

Please sign in to comment.
You canā€™t perform that action at this time.