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