]> git.phdru.name Git - m_lib.git/blob - m_lib/opstring.py
Remove wrong copyright lines, fix module docstrings
[m_lib.git] / m_lib / opstring.py
1 #! /usr/bin/env python
2 # -*- coding: koi8-r -*-
3
4 #
5 # opString - string/pathnames manipulation routines
6 # Some ideas came from Turbo Professional/Object Professional (t/o)pString.PAS
7 #
8
9
10 from string import *
11
12
13 #
14 ### Int/string conversion routines
15 #
16
17
18 def bin(i):
19    """
20       Convert integer to binary string.
21    """
22    s = ''
23    q = i
24    while (1):
25       q, r = divmod(q, 2)
26       s = digits[r] + s
27       if q == 0: break
28
29    return s
30
31
32 #
33 ### String manipulation routines
34 #
35
36
37 def PadCh(S, Ch, Len):
38    """ Return a string right-padded to length Len with Ch """
39    if len(S) >= Len:
40       return S
41    else:
42       return S + Ch*(Len - len(S))
43
44
45 def Pad(S, Len):
46    """ Return a string right-padded to length len with blanks """
47    return PadCh(S, ' ', Len)
48
49
50 def LeftPadCh(S, Ch, Len):
51    """ Return a string left-padded to length len with ch """
52    if len(S) >= Len:
53       return S
54    else:
55       return Ch*(Len - len(S)) + S
56
57
58 def LeftPad(S, Len):
59    """ Return a string left-padded to length len with blanks """
60    return LeftPadCh(S, ' ', Len)
61
62
63 def CenterCh(S, Ch, Width):
64    """ Return a string centered in a string of Ch with specified width """
65    if len(S) >= Width:
66       return S
67    else:
68       l = (Width - len(S)) / 2
69       r = Width - len(S) - l
70       return Ch*l + S + Ch*r
71
72
73 def Center(S, Width):
74    """ Return a string centered in a blank string of specified width """
75    return CenterCh(S, ' ', Width)
76
77
78 def FindStr(str, list):
79    """ Find given string in the list of strings """
80    for i in range(len(list)):
81       if str == list[i]:
82          return i
83
84    return -1
85
86
87 def FindStrUC(str, list):
88    """ Find string ignoring case """
89    str = upper(str)
90    for i in range(len(list)):
91       if str == upper(list[i]):
92          return i
93
94    return -1
95
96
97 # Склонения
98
99 transl_adict = {
100    "day"  : ["день", "дня", "дней"],
101    "week" : ["неделя", "недели", "недель"],
102    "month": ["месяц", "месяца", "месяцев"],
103    "year" : ["год", "года", "лет"]
104 }
105
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"]
110
111 transl_vdict = {
112    1: 0,
113    2: 1, 3: 1, 4: 1,
114    5: 2, 6: 2, 7: 2, 8: 2, 9: 2, 0: 2
115 }
116
117 def translate_a(val, id):
118    if not transl_adict.has_key(id):
119       return ''
120
121    if 5 <= (val % 100) <= 20:
122       val = 2
123    else:
124       val = transl_vdict[val % 10]
125    return transl_adict[id][val]
126
127
128 # Encodings, especially cyrillic. Requires Unicode, hence Python 2.0+
129
130 def recode(s, from_encoding, to_encoding, errors = "strict"):
131    return unicode(s, from_encoding, errors).encode(to_encoding, errors)
132
133
134 def win2koi(s, errors = "strict"):
135    return recode(s, "cp1251", "koi8-r", errors)
136
137 def koi2win(s, errors = "strict"):
138    return recode(s, "koi8-r", "cp1251", errors)
139
140
141 #
142 ### Test stuff
143 #
144
145 def test():
146    print "bin(0x6) =", bin(0x6)
147    print "bin(0xC) =", bin(0xC)
148
149    print "'Test' left-padded :", LeftPad("Test", 20)
150    print "'Test' right-padded:", PadCh("Test", '*', 20)
151    print "'Test' centered    :", CenterCh("Test", '=', 20)
152
153    print "'Олег':", koi2win(win2koi("Олег"))
154
155 if __name__ == "__main__":
156    test()