-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmask.py
270 lines (250 loc) · 9.37 KB
/
mask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from typing import Callable, Optional, Any, List, Literal
from re import compile
class Mask:
def __init__(self,
format_type: Literal['numeric', 'fixed'],
mask: Optional[str] = None,
monetary: Optional[bool] = False,
decimal_places: Optional[int] = 2,
decimal_separator: Optional[str] = '.',
thousand_places: Optional[int] = 3,
thousand_separator: Optional[str] = ',',
symbol: Optional[str] = '',
format_negative: Optional[str] = '-%(symbol)s%(amount)s',
format_positive: Optional[str] = '%(symbol)s%(amount)s',
placeholder: Optional[str] = '_'):
self._basestring = type(u'')
self._type = format_type
if str(self._type).lower() == 'fixed':
assert self._type is not None, 'the fixed mask, is not present'
self._mask = mask
self._monetary = monetary
self._dec_places = decimal_places
self._dec_sep = decimal_separator
self._tho_places = thousand_places
self._tho_sep = thousand_separator
self._symbol = symbol
self._fmt_neg = format_negative
self._fmt_pos = format_positive
self._placeholder = placeholder
self._callbacks = []
self._buffer = []
self._defs = {
'9': '[0-9]',
'a': '[a-zA-Z]',
'x': '[a-zA-z0-9]'
}
self._tests = []
self._partialPosition = None
self._firstNonMaskPosition = None
self._len = len(self._mask)
self._start()
def _start(self):
self._tests = []
self._partialPosition = None
self._firstNonMaskPosition = None
self._len = len(self._mask)
for i, c in enumerate(self._mask.lower()):
if c == '?':
self._len -= 1
self._partialPosition = i
atom = self._defs.get(c, None)
self._tests.append(compile('(%s)' % atom) if atom else atom)
if not atom and self._firstNonMaskPosition is None:
self._firstNonMaskPosition = len(self._tests) - 1
def cget(self, key):
if key == 'format_type':
return self._type
if key == 'decimal_places':
return self._dec_places
if key == 'decimal_separator':
return self._dec_sep
if key == 'thousand_places':
return self._tho_places
if key == 'thousand_separator':
return self._tho_sep
if key == 'format_negative':
return self._fmt_neg
if key == 'format_positive':
return self._fmt_pos
return getattr(self, '_' + key)
def configure(self,
format_type: Optional[str] = None,
mask: Optional[str] = None,
monetary: Optional[bool] = None,
decimal_places: Optional[int] = None,
decimal_separator: Optional[str] = None,
thousand_places: Optional[int] = None,
thousand_separator: Optional[str] = None,
symbol: Optional[str] = None,
format_negative: Optional[str] = None,
format_positive: Optional[str] = None,
placeholder: Optional[str] = None):
changed = []
if format_type is not None:
self._type = format_type
changed.append('format_type')
if mask is not None:
self._mask = mask
changed.append('mask')
if monetary is not None:
self._monetary = monetary
changed.append('monetary')
if decimal_places is not None:
self._dec_places = decimal_places
changed.append('decimal_places')
if decimal_separator is not None:
self._dec_sep = decimal_separator
changed.append('decimal_separator')
if thousand_places is not None:
self._tho_places = thousand_places
changed.append('thousand_places')
if thousand_separator is not None:
self._tho_sep = thousand_separator
changed.append('thousand_separator')
if symbol is not None:
self._symbol = symbol
changed.append('symbol')
if format_negative is not None:
self._fmt_neg = format_negative
changed.append('format_negative')
if format_positive is not None:
self._fmt_pos = format_positive
changed.append('format_positive')
if placeholder is not None:
self._placeholder = placeholder
changed.append('placeholder')
if len(changed) > 0:
self._start()
for func in self._callbacks:
func(changed)
def trace(self, func: Callable[[List[str]], Any], add: Optional[bool] = False):
if add:
self._callbacks.append(func)
else:
self._callbacks = [func]
def clean_numeric(self, string):
if not isinstance(string, self._basestring):
string = str(string)
string = string.replace(self._symbol + ' ', '') \
.replace(self._tho_sep, '') \
.replace(self._dec_sep, '.')
if '.' not in string:
string = list(string)
string.insert(-2, '.')
string = ''.join(string)
return string.partition('.')
def fmt_numeric(self, amount):
temp = '00' if '.' not in str(amount) \
else str(amount).split('.')[1]
__l = []
amount = amount.split('.')[0]
try:
minus = float(''.join(self.clean_numeric(amount))) < 0
except ValueError:
minus = 0
if len(amount) > self._tho_places:
__nn = amount[-self._tho_places:]
__l.append(__nn)
amount = amount[:len(amount) - self._tho_places]
while len(amount) > self._tho_places:
nn = amount[len(amount) - self._tho_places:]
__l.insert(0, nn)
amount = amount[0:len(amount) - self._tho_places]
if len(''.join(self.clean_numeric(amount))) > 0:
__l.insert(0, amount)
amount = self._tho_sep.join(__l) + self._dec_sep + temp
if minus:
amount = self._fmt_neg % {
'symbol': self._symbol,
'amount': amount
}
else:
amount = self._fmt_pos % {
'symbol': (self._symbol + ' ') if self._symbol else '',
'amount': amount
}
return amount
def seeknext(self, pos):
if 0 <= pos + 1 < self._len:
if self._tests[pos + 1]:
return pos + 1
return self.seeknext(pos + 1)
return pos
def seekprev(self, pos):
if 0 <= pos - 1 < self._len:
if self._tests[pos - 1]:
return pos - 1
return self.seekprev(pos - 1)
return pos
def shiftl(self, begin):
if begin < 0:
return
for i in range(self._len):
j = self.seeknext(begin)
if self._tests[i]:
if j < self._len and self._tests[i].match(self._buffer[i]):
self._buffer[i] = self._buffer[j]
self._buffer[j] = self._placeholder
else:
break
def shiftr(self, pos, c):
if pos in range(self._len):
j = self.seeknext(pos)
t = self._buffer[pos]
if not t == c and j < self._len and t == self._placeholder:
self._buffer[pos] = c
def write(self) -> str:
return ''.join(
filter(
lambda x: x is not None,
map(
lambda c, self=self:
(self._placeholder
if self._defs.get(c, None)
else c)
if c != '?' else None, self._mask)
)
)
def clean_fixed(self, value: str):
__write = self.write()
__value = ''
for idx, char in enumerate(value):
if 0 <= idx < self._len and self._tests[idx]:
if __write[idx] != self._placeholder:
continue
__value += char
return __value
def clear(self, value) -> str:
if not len(value) == len(self._mask):
return value
if self._type.lower() == 'numeric':
_value = str(self.clean_numeric(value))
else:
_value = str(self.clean_fixed(value))
return _value
def fmt_fixed(self, string):
if self._type.lower() != 'fixed':
raise Exception('Format is not supported to a numeric mask.')
__string = self.clear(string)
__string_len = len(__string)
__len = 0
__buffer = self.write()
__value = ''
for char in __buffer:
if char == self._placeholder:
if __string_len > __len:
__value += __string[__len]
else:
__value += self._placeholder
__len += 1
else:
__value += char
return __value
def format_variable(self, string):
if self._type.lower() == 'fixed':
return self.fmt_fixed(string)
else:
return self.fmt_numeric(string)
def __len__(self) -> int:
return self._len