Skip to main content

Posts

Showing posts from June, 2013

A workaround for UnicodeDecodeError for kid template

Like previous post , the UnicodeDecodeError may occur in the following code: import kid t_mod = kid.load_template(template_file t_class = t_mod.Template t_class.serializer = kid.HTMLSerializer() t = t_class(**data) return t.serialize(output='html') The problem is caused by data contains some non-ascii data, and the default encoding kid choose for decoding is ascii. Actually kid has an attributing assme_encoding, are used to converting data to unicode. import kid t_mod = kid.load_template(template_file t_class = t_mod.Template t_class.serializer = kid.HTMLSerializer() t = t_class(**data) t.assume_encoding = 'utf-8' return t.serialize(output='html')

A workaround for UnicodeDecodeError when using mako template

Consider following code to rendering data with mako: from mako.template import Template tmpl = Template(filename=some_template_flle, module_directory=some_template_module_path) print tmpl.render(**some_data) The code may have following error, if the some_data contains non-ascii characters, because the implementation use directly use unicode method to convert some_data: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128) The error can be fixed by decode the some_data. Or there is another workaround, which is injecting a partial unicode function into the module generated: from mako.template import Template import functools tmpl = Template(filename=some_template_flle, module_directory=some_template_module_path) tmpl.module.unicode = functools.partial(unicode, encoding='utf-8') print tmpl.render(**some_data)

Several Cases of Bit Torrent Sync

Here we are going to test several cases when using BitTorrent Sync. Assuming we have two devices, A and B. Below are the cases: Case 1 1) Create and share folder test1 on Device A, and use the readonly key to create the same folder on Device B. 2) Create test.txt in folder test1 on Device A, the file is synced correctly on Device B. 3) Change test.txt in on Device A, the change is synced correctly on Device B. 4) Create other files in folder test1 on Device B, nothing synced back to Device A. 5) Change the test.txt in Device B, nothing synced back to Device A. 6) Change the test.txt in Device A, nothing synced to Device B. 7) Create test2.txt in folder test1 on Device A, the file is synced correctly on Device B, but test.txt remains modified version on Device B. 8) Remove test.txt in Device B, nothing happend. 9) Change the test.txt in Device A, nothing happened. 10) Remove the folder test1 on Device B from btsync, and readd it with the readonly key, all files in Device A synced t