This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Format.py
304 lines (238 loc) · 7.39 KB
/
Format.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import collections
def get_named_tuple(name, dict):
return collections.namedtuple(name, dict.keys())(*dict.values())
Align = get_named_tuple('align', {
'default': '',
'left': '<',
'right': '>',
'center': '^',
"right_sign": '='
})
Group = get_named_tuple('group', {
'no': '',
'yes': ','
})
Padding = get_named_tuple('padding', {
'no': '',
'yes': '0'
})
Prefix = get_named_tuple('prefix', {
'yocto': 10 ** -24,
'zepto': 10 ** -21,
'atto': 10 ** -18,
'femto': 10 ** -15,
'pico': 10 ** -12,
'nano': 10 ** -9,
'micro': 10 ** -6,
'milli': 10 ** -3,
'none': None,
'kilo': 10 ** 3,
'mega': 10 ** 6,
'giga': 10 ** 9,
'tera': 10 ** 12,
'peta': 10 ** 15,
'exa': 10 ** 18,
'zetta': 10 ** 21,
'yotta': 10 ** 24
})
Scheme = get_named_tuple('scheme', {
'default': '',
'decimal': 'r',
'decimal_integer': 'd',
'decimal_or_exponent': 'g',
'decimal_si_prefix': 's',
'exponent': 'e',
'fixed': 'f',
'percentage': '%',
'percentage_rounded': 'p',
'binary': 'b',
'octal': 'o',
'lower_case_hex': 'x',
'upper_case_hex': 'X',
'unicode': 'c'
})
Sign = get_named_tuple('sign', {
'default': '',
'negative': '-',
'positive': '+',
'parantheses': '(',
'space': ' '
})
Symbol = get_named_tuple('symbol', {
'no': '',
'yes': '$',
'binary': '#b',
'octal': '#o',
'hex': '#x'
})
Trim = get_named_tuple('trim', {
'no': '',
'yes': '~'
})
class Format():
def __init__(self, **kwargs):
self._locale = {}
self._nully = ''
self._prefix = Prefix.none
self._specifier = {
'align': Align.default,
'fill': '',
'group': Group.no,
'width': '',
'padding': Padding.no,
'precision': '',
'sign': Sign.default,
'symbol': Symbol.no,
'trim': Trim.no,
'type': Scheme.default
}
valid_methods = [
m for m in dir(self.__class__)
if m[0] != '_' and m != 'to_plotly_json'
]
for kw, val in kwargs.items():
if kw not in valid_methods:
raise TypeError(
'{0} is not a format method. Expected one of'.format(kw),
str(list(valid_methods))
)
getattr(self, kw)(val)
def _validate_char(self, value):
self._validate_string(value)
if len(value) != 1:
raise ValueError('expected value to a string of length one')
def _validate_non_negative_integer_or_none(self, value):
if value is None:
return
if not isinstance(value, int):
raise TypeError('expected value to be an integer')
if value < 0:
raise ValueError('expected value to be non-negative', str(value))
def _validate_named(self, value, named_values):
if value not in named_values:
raise TypeError('expected value to be one of',
str(list(named_values)))
def _validate_string(self, value):
if not isinstance(value, (str, u''.__class__)):
raise TypeError('expected value to be a string')
# Specifier
def align(self, value):
self._validate_named(value, Align)
self._specifier['align'] = value
return self
def fill(self, value):
self._validate_char(value)
self._specifier['fill'] = value
return self
def group(self, value):
if isinstance(value, bool):
value = Group.yes if value else Group.no
self._validate_named(value, Group)
self._specifier['group'] = value
return self
def padding(self, value):
if isinstance(value, bool):
value = Padding.yes if value else Padding.no
self._validate_named(value, Padding)
self._specifier['padding'] = value
return self
def padding_width(self, value):
self._validate_non_negative_integer_or_none(value)
self._specifier['width'] = value if value is not None else ''
return self
def precision(self, value):
self._validate_non_negative_integer_or_none(value)
self._specifier['precision'] = (
'.{0}'.format(value) if value is not None else ''
)
return self
def scheme(self, value):
self._validate_named(value, Scheme)
self._specifier['type'] = value
return self
def sign(self, value):
self._validate_named(value, Sign)
self._specifier['sign'] = value
return self
def symbol(self, value):
self._validate_named(value, Symbol)
self._specifier['symbol'] = value
return self
def trim(self, value):
if isinstance(value, bool):
value = Trim.yes if value else Trim.no
self._validate_named(value, Trim)
self._specifier['trim'] = value
return self
# Locale
def symbol_prefix(self, value):
self._validate_string(value)
if 'symbol' not in self._locale:
self._locale['symbol'] = [value, '']
else:
self._locale['symbol'][0] = value
return self
def symbol_suffix(self, value):
self._validate_string(value)
if 'symbol' not in self._locale:
self._locale['symbol'] = ['', value]
else:
self._locale['symbol'][1] = value
return self
def decimal_delimiter(self, value):
self._validate_char(value)
self._locale['decimal'] = value
return self
def group_delimiter(self, value):
self._validate_char(value)
self._locale['group'] = value
return self
def groups(self, groups):
groups = (
groups if isinstance(groups, list) else
[groups] if isinstance(groups, int) else None
)
if not isinstance(groups, list):
raise TypeError(
'expected groups to be an integer or a list of integers'
)
if len(groups) == 0:
raise ValueError(
'expected groups to be an integer or a list of '
'one or more integers'
)
for group in groups:
if not isinstance(group, int):
raise TypeError('expected entry to be an integer')
if group <= 0:
raise ValueError('expected entry to be a non-negative integer')
self._locale['grouping'] = groups
return self
# Nully
def nully(self, value):
self._nully = value
return self
# Prefix
def si_prefix(self, value):
self._validate_named(value, Prefix)
self._prefix = value
return self
def to_plotly_json(self):
f = {}
f['locale'] = self._locale.copy()
f['nully'] = self._nully
f['prefix'] = self._prefix
aligned = self._specifier['align'] != Align.default
f['specifier'] = '{}{}{}{}{}{}{}{}{}{}'.format(
self._specifier['fill'] if aligned else '',
self._specifier['align'],
self._specifier['sign'],
self._specifier['symbol'],
self._specifier['padding'],
self._specifier['width'],
self._specifier['group'],
self._specifier['precision'],
self._specifier['trim'],
self._specifier['type']
)
return f