2 # -*- coding: koi8-r -*-
5 # opString - string/pathnames manipulation routines
6 # Some ideas came from Turbo Professional/Object Professional (t/o)pString.PAS
8 # Written by Broytman, Nov 1997. Copyright (C) 1997 PhiloSoft Design
16 ### Int/string conversion routines
22 Convert integer to binary string.
35 ### String manipulation routines
39 def PadCh(S, Ch, Len):
40 """ Return a string right-padded to length Len with Ch """
44 return S + Ch*(Len - len(S))
48 """ Return a string right-padded to length len with blanks """
49 return PadCh(S, ' ', Len)
52 def LeftPadCh(S, Ch, Len):
53 """ Return a string left-padded to length len with ch """
57 return Ch*(Len - len(S)) + S
61 """ Return a string left-padded to length len with blanks """
62 return LeftPadCh(S, ' ', Len)
65 def CenterCh(S, Ch, Width):
66 """ Return a string centered in a string of Ch with specified width """
70 l = (Width - len(S)) / 2
71 r = Width - len(S) - l
72 return Ch*l + S + Ch*r
76 """ Return a string centered in a blank string of specified width """
77 return CenterCh(S, ' ', Width)
80 def FindStr(str, list):
81 """ Find given string in the list of strings """
82 for i in range(len(list)):
89 def FindStrUC(str, list):
90 """ Find string ignoring case """
92 for i in range(len(list)):
93 if str == upper(list[i]):
102 "day" : ["день", "дня", "дней"],
103 "week" : ["неделя", "недели", "недель"],
104 "month": ["месяц", "месяца", "месяцев"],
105 "year" : ["год", "года", "лет"]
108 transl_adict["days"] = transl_adict["day"]
109 transl_adict["weeks"] = transl_adict["week"]
110 transl_adict["months"] = transl_adict["month"]
111 transl_adict["years"] = transl_adict["year"]
116 5: 2, 6: 2, 7: 2, 8: 2, 9: 2, 0: 2
119 def translate_a(val, id):
120 if not transl_adict.has_key(id):
123 if 5 <= (val % 100) <= 20:
126 val = transl_vdict[val % 10]
127 return transl_adict[id][val]
130 # Encodings, especially cyrillic. Requires Unicode, hence Python 2.0+
132 def recode(s, from_encoding, to_encoding, errors = "strict"):
133 return unicode(s, from_encoding, errors).encode(to_encoding, errors)
136 def win2koi(s, errors = "strict"):
137 return recode(s, "cp1251", "koi8-r", errors)
139 def koi2win(s, errors = "strict"):
140 return recode(s, "koi8-r", "cp1251", errors)
148 print "bin(0x6) =", bin(0x6)
149 print "bin(0xC) =", bin(0xC)
151 print "'Test' left-padded :", LeftPad("Test", 20)
152 print "'Test' right-padded:", PadCh("Test", '*', 20)
153 print "'Test' centered :", CenterCh("Test", '=', 20)
155 print "'Олег':", koi2win(win2koi("Олег"))
157 if __name__ == "__main__":