2 # -*- coding: koi8-r -*-
5 # opString - string/pathnames manipulation routines
6 # Some ideas came from Turbo Professional/Object Professional (t/o)pString.PAS
14 ### Int/string conversion routines
20 Convert integer to binary string.
33 ### String manipulation routines
37 def PadCh(S, Ch, Len):
38 """ Return a string right-padded to length Len with Ch """
42 return S + Ch*(Len - len(S))
46 """ Return a string right-padded to length len with blanks """
47 return PadCh(S, ' ', Len)
50 def LeftPadCh(S, Ch, Len):
51 """ Return a string left-padded to length len with ch """
55 return Ch*(Len - len(S)) + S
59 """ Return a string left-padded to length len with blanks """
60 return LeftPadCh(S, ' ', Len)
63 def CenterCh(S, Ch, Width):
64 """ Return a string centered in a string of Ch with specified width """
68 l = (Width - len(S)) / 2
69 r = Width - len(S) - l
70 return Ch*l + S + Ch*r
74 """ Return a string centered in a blank string of specified width """
75 return CenterCh(S, ' ', Width)
78 def FindStr(str, list):
79 """ Find given string in the list of strings """
80 for i in range(len(list)):
87 def FindStrUC(str, list):
88 """ Find string ignoring case """
90 for i in range(len(list)):
91 if str == upper(list[i]):
100 "day" : ["день", "дня", "дней"],
101 "week" : ["неделя", "недели", "недель"],
102 "month": ["месяц", "месяца", "месяцев"],
103 "year" : ["год", "года", "лет"]
106 transl_adict["days"] = transl_adict["day"]
107 transl_adict["weeks"] = transl_adict["week"]
108 transl_adict["months"] = transl_adict["month"]
109 transl_adict["years"] = transl_adict["year"]
114 5: 2, 6: 2, 7: 2, 8: 2, 9: 2, 0: 2
117 def translate_a(val, id):
118 if not transl_adict.has_key(id):
121 if 5 <= (val % 100) <= 20:
124 val = transl_vdict[val % 10]
125 return transl_adict[id][val]
128 # Encodings, especially cyrillic. Requires Unicode, hence Python 2.0+
130 def recode(s, from_encoding, to_encoding, errors = "strict"):
131 return unicode(s, from_encoding, errors).encode(to_encoding, errors)
134 def win2koi(s, errors = "strict"):
135 return recode(s, "cp1251", "koi8-r", errors)
137 def koi2win(s, errors = "strict"):
138 return recode(s, "koi8-r", "cp1251", errors)
146 print "bin(0x6) =", bin(0x6)
147 print "bin(0xC) =", bin(0xC)
149 print "'Test' left-padded :", LeftPad("Test", 20)
150 print "'Test' right-padded:", PadCh("Test", '*', 20)
151 print "'Test' centered :", CenterCh("Test", '=', 20)
153 print "'Олег':", koi2win(win2koi("Олег"))
155 if __name__ == "__main__":