-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathutils.py
45 lines (32 loc) · 921 Bytes
/
utils.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
from __future__ import absolute_import, print_function, unicode_literals
import decimal
import re
from wolframclient.utils.encoding import force_bytes
# replacement method borrowed from json
ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')
ESCAPE_DCT = {
"\\": "\\\\",
'"': '\\"',
"\b": "\\b",
"\f": "\\f",
"\n": "\\n",
"\r": "\\r",
"\t": "\\t",
}
for i in range(0x20):
ESCAPE_DCT.setdefault(chr(i), "\\u{:04x}".format(i))
# ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
del i
def replace(match):
return ESCAPE_DCT[match.group(0)]
def py_encode_text(s):
yield b'"'
yield force_bytes(ESCAPE.sub(replace, s), encoding="iso-8859-1")
yield b'"'
def py_encode_decimal(number, prec=decimal.getcontext().prec):
return "{:f}``{:d}".format(number, prec).encode("utf-8")
def safe_len(obj):
try:
return len(obj)
except TypeError:
return