SQLite, python, unicode, and non-utf data

I’m still ignorant of whether there is a way to correctly convert ‘ó’ from latin-1 to utf-8 and not mangle it repr() and unicodedata.name() are your friends when it comes to debugging such problems: >>> oacute_latin1 = “\xF3” >>> oacute_unicode = oacute_latin1.decode(‘latin1’) >>> oacute_utf8 = oacute_unicode.encode(‘utf8’) >>> print repr(oacute_latin1) ‘\xf3′ >>> print repr(oacute_unicode) u’\xf3’ >>> … Read more

Is it possible to run python SimpleHTTPServer on localhost only?

In Python versions 3.4 and higher, the http.server module accepts a bind parameter. According to the docs: python -m http.server 8000 By default, server binds itself to all interfaces. The option -b/–bind specifies a specific address to which it should bind. For example, the following command causes the server to bind to localhost only: python … Read more

Printing a string prints ‘u’ before the string in Python?

I think what you’re actually surprised by here is that printing a single string doesn’t do the same thing as printing a list of strings—and this is true whether they’re Unicode or not: >>> hobby1 = u’Dizziness’ >>> hobby2 = u’Vértigo’ >>> hobbies = [hobby1, hobby2] >>> print hobby1 Dizziness >>> print hobbies [u’Dizziness’, u’V\xe9rtigo’] … Read more

Mock Python’s built in print function

I know that there is already an accepted answer but there is simpler solution for that problem – mocking the print in python 2.x. Answer is in the mock library tutorial: http://www.voidspace.org.uk/python/mock/patch.html and it is: >>> from StringIO import StringIO >>> def foo(): … print ‘Something’ … >>> @patch(‘sys.stdout’, new_callable=StringIO) … def test(mock_stdout): … foo() … Read more