2017-04-15 10:07:02 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import six
|
2017-04-20 13:47:17 +00:00
|
|
|
import ftfy
|
2017-04-15 10:07:02 +00:00
|
|
|
import sys
|
2017-04-15 11:05:15 +00:00
|
|
|
import ujson
|
2017-07-25 16:57:59 +00:00
|
|
|
import itertools
|
2017-04-15 10:07:02 +00:00
|
|
|
|
2017-05-31 13:25:21 +00:00
|
|
|
from thinc.neural.util import copy_array
|
2017-05-31 12:14:29 +00:00
|
|
|
|
2017-04-15 10:07:02 +00:00
|
|
|
try:
|
|
|
|
import cPickle as pickle
|
|
|
|
except ImportError:
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
try:
|
|
|
|
import copy_reg
|
|
|
|
except ImportError:
|
|
|
|
import copyreg as copy_reg
|
|
|
|
|
2017-05-18 12:12:45 +00:00
|
|
|
try:
|
|
|
|
from cupy.cuda.stream import Stream as CudaStream
|
|
|
|
except ImportError:
|
|
|
|
CudaStream = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
import cupy
|
|
|
|
except ImportError:
|
|
|
|
cupy = None
|
|
|
|
|
|
|
|
|
2017-05-18 12:12:31 +00:00
|
|
|
pickle = pickle
|
|
|
|
copy_reg = copy_reg
|
|
|
|
CudaStream = CudaStream
|
|
|
|
cupy = cupy
|
|
|
|
fix_text = ftfy.fix_text
|
2017-05-31 13:25:21 +00:00
|
|
|
copy_array = copy_array
|
2017-07-25 16:57:59 +00:00
|
|
|
izip = getattr(itertools, 'izip', zip)
|
2017-04-15 10:07:02 +00:00
|
|
|
|
|
|
|
is_python2 = six.PY2
|
|
|
|
is_python3 = six.PY3
|
|
|
|
is_windows = sys.platform.startswith('win')
|
|
|
|
is_linux = sys.platform.startswith('linux')
|
|
|
|
is_osx = sys.platform == 'darwin'
|
|
|
|
|
|
|
|
|
|
|
|
if is_python2:
|
|
|
|
bytes_ = str
|
|
|
|
unicode_ = unicode
|
|
|
|
basestring_ = basestring
|
|
|
|
input_ = raw_input
|
2017-04-15 11:05:15 +00:00
|
|
|
json_dumps = lambda data: ujson.dumps(data, indent=2).decode('utf8')
|
2017-05-07 21:24:56 +00:00
|
|
|
path2str = lambda path: str(path).decode('utf8')
|
2017-04-15 10:07:02 +00:00
|
|
|
|
|
|
|
elif is_python3:
|
|
|
|
bytes_ = bytes
|
|
|
|
unicode_ = str
|
|
|
|
basestring_ = str
|
|
|
|
input_ = input
|
2017-04-15 11:05:15 +00:00
|
|
|
json_dumps = lambda data: ujson.dumps(data, indent=2)
|
2017-05-07 21:24:56 +00:00
|
|
|
path2str = lambda path: str(path)
|
2017-04-15 10:07:02 +00:00
|
|
|
|
2017-07-31 23:11:35 +00:00
|
|
|
|
|
|
|
def b_to_str(b_str):
|
|
|
|
if is_python2:
|
|
|
|
return b_str
|
|
|
|
# important: if no encoding is set, string becomes "b'...'"
|
|
|
|
return str(b_str, encoding='utf8')
|
|
|
|
|
|
|
|
|
2017-05-31 20:21:44 +00:00
|
|
|
def getattr_(obj, name, *default):
|
|
|
|
if is_python3 and isinstance(name, bytes):
|
|
|
|
name = name.decode('utf8')
|
|
|
|
return getattr(obj, name, *default)
|
|
|
|
|
2017-04-15 10:07:02 +00:00
|
|
|
|
|
|
|
def symlink_to(orig, dest):
|
2017-04-15 10:17:27 +00:00
|
|
|
if is_python2 and is_windows:
|
2017-04-15 10:07:02 +00:00
|
|
|
import subprocess
|
2017-05-07 21:24:56 +00:00
|
|
|
subprocess.call(['mklink', '/d', path2str(orig), path2str(dest)], shell=True)
|
2017-04-15 10:17:27 +00:00
|
|
|
else:
|
|
|
|
orig.symlink_to(dest)
|
2017-04-15 10:07:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_config(python2=None, python3=None, windows=None, linux=None, osx=None):
|
|
|
|
return ((python2 == None or python2 == is_python2) and
|
|
|
|
(python3 == None or python3 == is_python3) and
|
|
|
|
(windows == None or windows == is_windows) and
|
|
|
|
(linux == None or linux == is_linux) and
|
|
|
|
(osx == None or osx == is_osx))
|
2017-05-31 12:14:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def normalize_string_keys(old):
|
|
|
|
'''Given a dictionary, make sure keys are unicode strings, not bytes.'''
|
|
|
|
new = {}
|
2017-05-31 19:08:16 +00:00
|
|
|
for key, value in old.items():
|
2017-05-31 12:14:29 +00:00
|
|
|
if isinstance(key, bytes_):
|
|
|
|
new[key.decode('utf8')] = value
|
|
|
|
else:
|
|
|
|
new[key] = value
|
|
|
|
return new
|
|
|
|
|
|
|
|
|