Skip to content

Instantly share code, notes, and snippets.

@reorx
Created February 12, 2019 05:36
Show Gist options
  • Save reorx/d4d30d2a68b141153cba8cb58619006c to your computer and use it in GitHub Desktop.
Save reorx/d4d30d2a68b141153cba8cb58619006c to your computer and use it in GitHub Desktop.
functions that perform string literals like `u'`, 'b'`, works for both Python 2 and Python 3
"""
An example usage of string_literals.py, shows code point table from command line.
```
codepoint.py '你好,世界'
Char Ord Hex Code Point
你 20320 0x4f60 U+4F60
好 22909 0x597d U+597D
, 65292 0xff0c U+FF0C
世 19990 0x4e16 U+4E16
界 30028 0x754c U+754C
```
"""
# coding: utf-8
from __future__ import print_function
import os
import sys
import subprocess as sp
PY2 = sys.version_info.major == 2
if not PY2:
unicode = str
def b_(s):
"""ensure binary type"""
if PY2:
if isinstance(s, unicode):
return s.encode('utf8')
return s
if isinstance(s, str):
return s.encode()
return s
def t_(b):
"""ensure text type"""
if PY2:
if isinstance(b, str):
return b.decode('utf8')
return b
if isinstance(b, bytes):
return b.decode()
return b
def usage(*args):
if args:
print(*args)
print('Usage: codepoint <chars>')
if __name__ == '__main__':
try:
chars = sys.argv[1]
except IndexError:
usage('invalid arguments, <chars> missing')
sys.exit(1)
if chars == '-h':
usage()
sys.exit()
h = [t_('Char'), t_('Ord'), t_('Hex'), t_('Code Point')]
d = [h]
for i in t_(chars):
n = ord(i)
x = hex(n)
xs = str(x)[2:]
if len(xs) < 4:
xs = t_('0') * (4 - len(xs)) + xs
cp = t_('U+') + xs
d.append([i, str(n), str(x), cp.upper()])
# add LF, or `column` in macos will raise `line too long` error
text = t_('\n').join(t_(',').join(l) for l in d) + t_('\n')
p = sp.Popen(
['column', '-s,', '-t'],
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.PIPE,
env=os.environ.copy(),
)
out, err = p.communicate(b_(text))
out = t_(out)
err = t_(err)
if p.returncode == 0:
print(out)
else:
print(
t_('Out: {}\n\nErr:{}').format(
out, err,
)
)
sys.exit(p.returncode)
import sys
PY2 = sys.version_info.major == 2
if not PY2:
unicode = str
def b_(s):
"""ensure binary type"""
if PY2:
if isinstance(s, unicode):
return s.encode('utf8')
return s
if isinstance(s, str):
return s.encode()
return s
def t_(b):
"""ensure text type"""
if PY2:
if isinstance(b, str):
return b.decode('utf8')
return b
if isinstance(b, bytes):
return b.decode()
return b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment