Merge pull request #458 from clayadavis/docs_py3k

Update docs to use py3k
This commit is contained in:
Mike Helmick 2017-10-06 15:27:23 -04:00 committed by GitHub
commit 12e6b34d1d
3 changed files with 13 additions and 8 deletions

View file

@ -62,7 +62,12 @@ with a status update.
# Assume you are working with a JPEG # Assume you are working with a JPEG
from PIL import Image from PIL import Image
from StringIO import StringIO try:
# Python 3
from io import StringIO
except ImportError:
# Python 2
from StringIO import StringIO
photo = Image.open('/path/to/file/image.jpg') photo = Image.open('/path/to/file/image.jpg')
@ -98,7 +103,7 @@ That being said, Twython offers a generator for search results and can be access
results = twitter.cursor(twitter.search, q='python') results = twitter.cursor(twitter.search, q='python')
for result in results: for result in results:
print result print(result)
Manipulate the Request (headers, proxies, etc.) Manipulate the Request (headers, proxies, etc.)
----------------------------------------------- -----------------------------------------------

View file

@ -25,7 +25,7 @@ The Old Way
results = twitter.search(q='twitter') results = twitter.search(q='twitter')
if results.get('statuses'): if results.get('statuses'):
for result in results['statuses']: for result in results['statuses']:
print result['id_str'] print(result['id_str'])
The New Way The New Way
^^^^^^^^^^^ ^^^^^^^^^^^
@ -39,7 +39,7 @@ The New Way
results = twitter.cursor(twitter.search, q='twitter') results = twitter.cursor(twitter.search, q='twitter')
for result in results: for result in results:
print result['id_str'] print(result['id_str'])
Another example: Another example:
@ -47,7 +47,7 @@ Another example:
results = twitter.cursor(t.get_mentions_timeline) results = twitter.cursor(t.get_mentions_timeline)
for result in results: for result in results:
print result['id_str'] print(result['id_str'])
HTML for Tweet HTML for Tweet
@ -66,7 +66,7 @@ This function takes a tweet object received from the Twitter API and returns an
include_rts=True) include_rts=True)
for tweet in user_tweets: for tweet in user_tweets:
tweet['text'] = Twython.html_for_tweet(tweet) tweet['text'] = Twython.html_for_tweet(tweet)
print tweet['text'] print(tweet['text'])
The above code takes all the tweets from a specific users timeline, loops over them and replaces the value of ``tweet['text']`` with HTML. The above code takes all the tweets from a specific users timeline, loops over them and replaces the value of ``tweet['text']`` with HTML.

View file

@ -27,10 +27,10 @@ Now set up how you want to handle the signals.
class MyStreamer(TwythonStreamer): class MyStreamer(TwythonStreamer):
def on_success(self, data): def on_success(self, data):
if 'text' in data: if 'text' in data:
print data['text'].encode('utf-8') print(data['text'])
def on_error(self, status_code, data): def on_error(self, status_code, data):
print status_code print(status_code)
# Want to stop trying to get data because of the error? # Want to stop trying to get data because of the error?
# Uncomment the next line! # Uncomment the next line!