def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
python类unichr()的实例源码
def test_invertibility_on_random_strings(self):
for _ in xrange(1000):
s = u"".join(six.unichr(random.randint(0, 65535))
for _ in xrange(10))
self.assertEqual(s, self.tokenizer.decode(self.tokenizer.encode(s)))
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def split(self, text):
a = _string_to_array(text)
if not a.size:
return []
b = np.copy(a)
b[b == ord(' ')] = 0
if self.margin != (1, 1):
# Dilate the image
structure = np.zeros((2 * (self.margin[1] - 1) + 1,
2 * (self.margin[0] - 1) + 1))
structure[self.margin[1] - 1:, self.margin[0] - 1:] = 1
labels = binary_dilation(b, structure=structure).astype(b.dtype)
else:
labels = b
num = label(labels, structure=np.ones((3, 3)), output=labels)
objects = find_objects(labels)
parts = []
for i, obj in enumerate(objects):
mask = labels[obj] != i + 1
region = np.copy(a[obj])
region[mask] = ord(' ')
part = '\n'.join(''.join(unichr(c or ord(' ')) for c in row)
for row in region.tolist())
if part.strip():
parts.append(part)
return parts
def safe_unichr(intval):
try:
return unichr(intval)
except ValueError:
# ValueError: unichr() arg not in range(0x10000) (narrow Python build)
s = "\\U%08x" % intval
# return UTF16 surrogate pair
return s.decode('unicode-escape')
def test_unichr():
assert six.u("\u1234") == six.unichr(0x1234)
assert type(six.u("\u1234")) is type(six.unichr(0x1234))
def encode(value):
"""Encodes bytes to a base65536 string."""
stream = io.StringIO()
length = len(value)
for x in range(0, length, 2):
b1 = indexbytes(value, x)
b2 = indexbytes(value, x + 1) if x + 1 < length else -1
code_point = BLOCK_START[b2] + b1
stream.write(unichr(code_point))
return stream.getvalue()
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def test____init__(self):
expected = b'\xc3\xa9\xe0\xaf\xb2\xe0\xbe\x84'
if six.PY3:
expected = expected.decode('utf-8')
message = six.unichr(233) + six.unichr(0x0bf2) + six.unichr(3972)
exc = exception.MoganException(message)
self.assertEqual(expected, exc.__str__())
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def test_json_handles_unicode(harness):
expected = b'''{
"Greetings": "\u00b5"
}'''
actual = harness.simple('''
from six import unichr
[---]
[---] application/json
{'Greetings': unichr(181)}
''', filepath="foo.json.spt").body
assert actual == expected
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def test_unichr():
assert six.u("\u1234") == six.unichr(0x1234)
assert type(six.u("\u1234")) is type(six.unichr(0x1234))
def uchr(c):
if not isinstance(c, int):
return c
if c > 255:
return six.unichr(c)
return chr(c)
def __c(x):
return six.unichr(x)
def _get_all_chars():
for i in range(0xFFFF):
yield six.unichr(i)
# build a regex that matches all printable characters. This allows
# spaces in the middle of the name. Also note that the regexp below
# deliberately allows the empty string. This is so only the constraint
# which enforces a minimum length for the name is triggered when an
# empty string is tested. Otherwise it is not deterministic which
# constraint fails and this causes issues for some unittests when
# PYTHONHASHSEED is set randomly.