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