Skip to content

Commit 0f1fa02

Browse files
author
cclauss
committed
Modernize Python 2 code to get ready for Python 3
* Use __print()__ function in both Python 2 and Python 3 * __print()__ is a function in Python 3. * Old style exceptions --> new style for Python 3 * Python 3 treats old style exceptions as syntax errors but new style exceptions work as expected in both Python 2 and Python 3.
1 parent f234953 commit 0f1fa02

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

mail_export.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
The script needs write permissions in /var/local to save the ID of the
1313
most recently mailed link. This ID is saved independently per user and tag.
1414
"""
15+
from __future__ import print_function
1516

1617
import os
1718
import re
@@ -64,7 +65,7 @@ def get_links(self):
6465
try:
6566
posts = json.loads(posts)
6667
except ValueError:
67-
print posts
68+
print(posts)
6869
return []
6970
return [
7071
p for p in posts['posts']
@@ -94,7 +95,7 @@ def run(self, options):
9495
self.latest = max(int(l['id']) for l in links) if not options.dry_run else None
9596

9697
if not self.recipients and not options.full:
97-
print body
98+
print(body)
9899
return
99100

100101
msg = MIMEText(body.encode('utf-8'))
@@ -105,7 +106,7 @@ def run(self, options):
105106
msg['To'] = ', '.join(self.recipients)
106107

107108
if options.full:
108-
print msg.as_string()
109+
print(msg.as_string())
109110
return
110111

111112
smtp = smtplib.SMTP(SMTP_SERVER)

oauth.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import sys
23
import urlparse
34
import oauth2 as oauth
@@ -25,19 +26,19 @@
2526

2627
request_token = dict(urlparse.parse_qsl(content))
2728

28-
print "Request Token:"
29-
print " - oauth_token = %s" % request_token['oauth_token']
30-
print " - oauth_token_secret = %s\n" % request_token['oauth_token_secret']
29+
print("Request Token:")
30+
print(" - oauth_token = %s" % request_token['oauth_token'])
31+
print(" - oauth_token_secret = %s\n" % request_token['oauth_token_secret'])
3132

3233
# Step 2: Redirect to the provider. Since this is a CLI script we do not
3334
# redirect. In a web application you would redirect the user to the URL
3435
# below.
3536

36-
print "Go to the following link in your browser:"
37-
print "%s?%s\n" % (authorize_url, urllib.urlencode({
37+
print("Go to the following link in your browser:")
38+
print("%s?%s\n" % (authorize_url, urllib.urlencode({
3839
"oauth_token": request_token['oauth_token'],
3940
"oauth_callback": 'http://localhost/doctorstrange'
40-
}))
41+
})))
4142

4243
# After the user has granted access to you, the consumer, the provider will
4344
# redirect you to whatever URL you have told them to redirect to. You can
@@ -61,8 +62,8 @@
6162
resp, content = client.request(access_token_url, "POST")
6263
access_token = dict(urlparse.parse_qsl(content))
6364

64-
print "Access Token:"
65-
print " - oauth_token = %s" % access_token['oauth_token']
66-
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
67-
print
68-
print "You may now access protected resources using the access tokens above.\n"
65+
print("Access Token:")
66+
print(" - oauth_token = %s" % access_token['oauth_token'])
67+
print(" - oauth_token_secret = %s" % access_token['oauth_token_secret'])
68+
print()
69+
print("You may now access protected resources using the access tokens above.\n")

tumble.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def post(self, entry):
117117
return dict(url=url, entry=entry, data=data)
118118

119119
for k in data:
120-
if type(data[k]) is unicode:
120+
if isinstance(data[k], unicode):
121121
data[k] = data[k].encode('utf-8')
122122

123123
# do the OAuth thing
@@ -127,9 +127,9 @@ def post(self, entry):
127127
try:
128128
headers, resp = client.request(url, method='POST', body=urllib.urlencode(data))
129129
resp = json.loads(resp)
130-
except ValueError, e:
130+
except ValueError as e:
131131
return 'error', 'json', resp
132-
except EnvironmentError, e:
132+
except EnvironmentError as e:
133133
return 'error', str(e)
134134
if resp['meta']['status'] in (200, 201):
135135
return op, str(resp['response']['id'])

0 commit comments

Comments
 (0)