Skip to content

Instantly share code, notes, and snippets.

View toddysm's full-sized avatar

Toddy Mladenov toddysm

View GitHub Profile
@toddysm
toddysm / unicode.py
Last active December 14, 2015 22:29
Different ways to deal with Unicode strings in Python 2.x
# reset the default encoding from ASCII to Unicode
# Note: it may not always work (depends on installations)
# see: http://blog.ianbicking.org/illusive-setdefaultencoding.html
import sys
reload(sys) # else the method will be missing
sys.setdefaultencoding('utf-8')
# convert from <type 'str'> to <type 'unicode'>
text.decode('utf-8')
@toddysm
toddysm / tornado_twitterhandler.py
Last active December 14, 2015 06:19
Twitter authentication with Tornado
class TwitterHandler(tornado.web.RequestHandler,
tornado.auth.TwitterMixin):
"""Handling Twitter authentication at URL http://www.yoursite.com/twitter"""
@tornado.web.asynchronous
def get(self):
if self.get_argument("oauth_token", None):
user = self.get_authenticated_user(self.async_callback(self._on_auth))
return
self.authenticate_redirect()