X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=m_lib%2Frus%2Frus2lat.py;h=e7f731b1c24a7f79431ad7f9d2273db76eb6d5e6;hb=130518d2d521f2972c9e6aba5038c4927f235dc7;hp=b2677b244a99d69878b35da21d48c229baee9042;hpb=8d79317cdc8220ff3c2192e68fc1dfc749c3ea1c;p=m_lib.git diff --git a/m_lib/rus/rus2lat.py b/m_lib/rus/rus2lat.py index b2677b2..e7f731b 100755 --- a/m_lib/rus/rus2lat.py +++ b/m_lib/rus/rus2lat.py @@ -1,6 +1,8 @@ #! /usr/bin/env python # -*- coding: koi8-r -*- +from __future__ import print_function + # # Rus -> Lat transliteration (koi2lat and win2lat) # @@ -69,26 +71,33 @@ koi2lat_d = { "Ù": "y", "Ü": "e", "À": "yu", - "Ñ": "ya" + "Ñ": "ya", } def make_xxx2lat(encoding="cp1251"): d = {} for k, v in koi2lat_d.items(): - k = unicode(k, "koi8-r").encode(encoding) + if isinstance(k, bytes): + k = k.decode("koi8-r") + k = k.encode(encoding) d[k] = v return d -from m_lib.lazy.dict import LazyDictInitFunc +from ..lazy.dict import LazyDictInitFunc win2lat_d = LazyDictInitFunc(make_xxx2lat, encoding="cp1251") def rus2lat(instr, rus2lat_d = koi2lat_d): out = [] for c in instr: - out.append(rus2lat_d.get(c, c)) - return ''.join(out) + c = rus2lat_d.get(c, c) + if isinstance(c, bytes): + c = c.decode('ascii') + elif isinstance(c, int): + c = chr(c) + out.append(c.encode('ascii')) + return b''.join(out) koi2lat = rus2lat @@ -99,6 +108,8 @@ def win2lat(instr): if __name__ == "__main__": Test = "ýÅÒÂÁËÏ× éÇÏÒØ çÒÉÇÏÒØÅ×ÉÞ. áâ÷ xyz ÁÂ× øøüàñ ßØÜÀÑ" - print "Test:", Test - print "ôÅÓÔ:", koi2lat(Test) - print "ôÅÓÔ:", win2lat(unicode(Test, "koi8-r").encode("cp1251")) + print("Test:", Test) + print("ôÅÓÔ:", koi2lat(Test)) + if isinstance(Test, bytes): + Test = Test.decode("cp1251") + print("ôÅÓÔ:", win2lat(Test.encode("cp1251")))