# -*- coding: utf-8 -*- import unittest import doctest def _to_xsistemo(s): """Turn a nice unicode string into an hideous x-systemo one. >>> _to_xsistemo(u"ĈĉĜĝĤĥĴĵŜŝŬŭ") 'CxcxGxgxHxhxJxjxSxsxUxux' """ replacements = [ (u"Ĉ", "Cx"), (u"ĉ", "cx"), (u"Ĝ", "Gx"), (u"ĝ", "gx"), (u"Ĥ", "Hx"), (u"ĥ", "hx"), (u"Ĵ", "Jx"), (u"ĵ", "jx"), (u"Ŝ", "Sx"), (u"ŝ", "sx"), (u"Ŭ", "Ux"), (u"ŭ", "ux"), ] for old, new in replacements: s = s.replace(old, new) try: s = s.encode("ascii") return s except: return repr(s) class ToXSistemoTest(unittest.TestCase): def test_to_xsistemo(self): self.assertEquals( 'CxcxGxgxHxhxJxjxSxsxUxux', _to_xsistemo(u"ĈĉĜĝĤĥĴĵŜŝŬŭ")) def _test(): doctest.testmod() unittest.main() if __name__ == "__main__": _test()