Yak Shaving

just me

Archive for February, 2009

App Engine and utf-8 Encoding

without comments

You may or may not have seen the error:

<type ‘exceptions.UnicodeDecodeError’>: ‘ascii’ codec can’t decode byte 0xc3 in position 2223: ordinal not in range(128)
args = (‘ascii’, ‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Tra… Engine” />\n\t\t</div>\n\n\t</div>\n\n</body>\n\n</html>\n\n’, 2223, 2224, ‘ordinal not in range(128)’)
encoding = ‘ascii’
end = 2224
message = ”
object = ‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Tra… Engine” />\n\t\t</div>\n\n\t</div>\n\n</body>\n\n</html>\n\n’
reason = ‘ordinal not in range(128)’
start = 2223

This had me foxed as I fetching band names which sometimes had a fancy character in them: Motörhead for example.

To allow the string to be rendered using the following:

unicode_string = unicode(string_with_char_init)
self.response.out.write(unicode_string.encode('utf-8'))

Thats it! For App Engine that works both to render to the page or to use in urlfetch.fetch.

Written by channam

February 17th, 2009 at 6:17 pm

Posted in App Engine, python

Basic Auth in Python

without comments

While hacking about with Google App Engine I decided to integrate some Twitter APIs. The code below is a quick method for access urls that require basic auth such as your Twitter timeline

authstring = base64.encodestring('%s:%s' % ('username', 'password'))
authstring = authstring.replace('\n', '')
r = urlfetch.fetch("http://twitter.com/statuses/friends_timeline.atom", headers={'Content-Type': 'application/x-www-form-urlencoded','Authorization': 'Basic %s' % authstring})
self.response.out.write(r.content)

Written by channam

February 15th, 2009 at 10:22 am

Posted in Uncategorized

Gone but never forgotten

without comments

You may have only been with us for the shortest of time but you will never be forgotten.

Written by channam

February 5th, 2009 at 11:41 pm

Posted in Uncategorized

Joost Test

without comments

Written by channam

February 1st, 2009 at 5:30 pm

Posted in Uncategorized