2 # -*- coding: koi8-r -*-
5 # opString - string/pathnames manipulation routines
6 # Some ideas came from Turbo Professional/Object Professional (t/o)pString.PAS
10 from __future__ import print_function
15 ### Int/string conversion routines
21 Convert integer to binary string.
34 ### String manipulation routines
38 def PadCh(S, Ch, Len):
39 """ Return a string right-padded to length Len with Ch """
43 return S + Ch*(Len - len(S))
47 """ Return a string right-padded to length len with blanks """
48 return PadCh(S, ' ', Len)
51 def LeftPadCh(S, Ch, Len):
52 """ Return a string left-padded to length len with ch """
56 return Ch*(Len - len(S)) + S
60 """ Return a string left-padded to length len with blanks """
61 return LeftPadCh(S, ' ', Len)
64 def CenterCh(S, Ch, Width):
65 """ Return a string centered in a string of Ch with specified width """
69 l = (Width - len(S)) / 2
70 r = Width - len(S) - l
71 return Ch*l + S + Ch*r
75 """ Return a string centered in a blank string of specified width """
76 return CenterCh(S, ' ', Width)
79 def FindStr(str, list):
80 """ Find given string in the list of strings """
81 for i in range(len(list)):
88 def FindStrUC(str, list):
89 """ Find string ignoring case """
91 for i in range(len(list)):
92 if str == upper(list[i]):
101 "day" : ["день", "дня", "дней"],
102 "week" : ["неделя", "недели", "недель"],
103 "month": ["месяц", "месяца", "месяцев"],
104 "year" : ["год", "года", "лет"]
107 transl_adict["days"] = transl_adict["day"]
108 transl_adict["weeks"] = transl_adict["week"]
109 transl_adict["months"] = transl_adict["month"]
110 transl_adict["years"] = transl_adict["year"]
115 5: 2, 6: 2, 7: 2, 8: 2, 9: 2, 0: 2
118 def translate_a(val, id):
119 if not transl_adict.has_key(id):
122 if 5 <= (val % 100) <= 20:
125 val = transl_vdict[val % 10]
126 return transl_adict[id][val]
129 # Encodings, especially cyrillic. Requires Unicode, hence Python 2.0+
131 def recode(s, from_encoding, to_encoding, errors = "strict"):
132 return unicode(s, from_encoding, errors).encode(to_encoding, errors)
135 def win2koi(s, errors = "strict"):
136 return recode(s, "cp1251", "koi8-r", errors)
138 def koi2win(s, errors = "strict"):
139 return recode(s, "koi8-r", "cp1251", errors)
147 print("bin(0x6) =", bin(0x6))
148 print("bin(0xC) =", bin(0xC))
150 print("'Test' left-padded :", LeftPad("Test", 20))
151 print("'Test' right-padded:", PadCh("Test", '*', 20))
152 print("'Test' centered :", CenterCh("Test", '=', 20))
154 print("'Олег':", koi2win(win2koi("Олег")))
156 if __name__ == "__main__":