本文整理汇总了Python中six.PY2属性的典型用法代码示例。如果您正苦于以下问题:Python six.PY2属性的具体用法?Python six.PY2怎么用?Python six.PY2使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类six
的用法示例。
在下文中一共展示了six.PY2属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_to_unicode
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def convert_to_unicode(text):
"""Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text.decode("utf-8", "ignore")
elif isinstance(text, unicode):
return text
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
示例2: printable_text
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def printable_text(text):
"""Returns text encoded in a way suitable for print or `tf.logging`."""
# These functions want `str` for both Python2 and Python3, but in one case
# it's a Unicode string and in the other it's a byte string.
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text
elif isinstance(text, unicode):
return text.encode("utf-8")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
示例3: reload_neuropythy
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def reload_neuropythy():
'''
reload_neuropythy() reloads all of the modules of neuropythy and returns the reloaded
neuropythy module. This is similar to reload(neuropythy) except that it reloads all the
neuropythy submodules prior to reloading neuropythy.
Example:
import neuropythy as ny
# ... some nonsense that breaks the library ...
ny = ny.reload_neuropythy()
'''
import sys, six
if not six.PY2:
try: from importlib import reload
except Exception: from imp import reload
for mdl in submodules:
if mdl in sys.modules:
sys.modules[mdl] = reload(sys.modules[mdl])
return reload(sys.modules['neuropythy'])
示例4: printable_text
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def printable_text(text):
""" Returns text encoded in a way suitable for print or `tf.logging`.
These functions want `str` for both Python2 and Python3, but in one case
it's a Unicode string and in the other it's a byte string.
"""
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text
elif isinstance(text, unicode):
return text.encode("utf-8")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
示例5: example_generator
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def example_generator(self, filename):
skipped = 0
for idx, line in enumerate(tf.gfile.Open(filename, "rb")):
if idx == 0: continue # skip header
if six.PY2:
line = unicode(line.strip(), "utf-8")
else:
line = line.strip().decode("utf-8")
split_line = line.split("\t")
if len(split_line) < 6:
skipped += 1
tf.logging.info("Skipping %d" % skipped)
continue
s1, s2, l = split_line[3:]
# A neat data augmentation trick from Radford et al. (2018)
# https://blog.openai.com/language-unsupervised/
inputs = [[s1, s2], [s2, s1]]
for inp in inputs:
yield {
"inputs": inp,
"label": int(l)
}
示例6: example_generator
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def example_generator(self, filename):
label_list = self.class_labels(data_dir=None)
for idx, line in enumerate(tf.gfile.Open(filename, "rb")):
if idx == 0: continue # skip header
if six.PY2:
line = unicode(line.strip(), "utf-8")
else:
line = line.strip().decode("utf-8")
split_line = line.split("\t")
# Works for both splits even though dev has some extra human labels.
s1, s2 = split_line[8:10]
l = label_list.index(split_line[-1])
inputs = [s1, s2]
yield {
"inputs": inputs,
"label": l
}
示例7: to_example
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def to_example(dictionary):
"""Helper: build tf.Example from (string -> int/float/str list) dictionary."""
features = {}
for (k, v) in six.iteritems(dictionary):
if not v:
raise ValueError("Empty generated field: %s" % str((k, v)))
if isinstance(v[0], six.integer_types):
features[k] = tf.train.Feature(int64_list=tf.train.Int64List(value=v))
elif isinstance(v[0], float):
features[k] = tf.train.Feature(float_list=tf.train.FloatList(value=v))
elif isinstance(v[0], six.string_types):
if not six.PY2: # Convert in python 3.
v = [bytes(x, "utf-8") for x in v]
features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
elif isinstance(v[0], bytes):
features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
else:
raise ValueError("Value for %s is not a recognized type; v: %s type: %s" %
(k, str(v[0]), str(type(v[0]))))
return tf.train.Example(features=tf.train.Features(feature=features))
示例8: ResourceUUID
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def ResourceUUID(value, creator):
if isinstance(value, uuid.UUID):
return value
if '/' in value:
raise ValueError("'/' is not supported in resource id")
try:
return uuid.UUID(value)
except ValueError:
if len(value) <= 255:
if creator is None:
creator = "\x00"
# value/creator must be str (unicode) in Python 3 and str (bytes)
# in Python 2. It's not logical, I know.
if six.PY2:
value = value.encode('utf-8')
creator = creator.encode('utf-8')
return uuid.uuid5(RESOURCE_ID_NAMESPACE,
value + "\x00" + creator)
raise ValueError(
'transformable resource id >255 max allowed characters')
示例9: convert_to_unicode
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def convert_to_unicode(text):
"""Converts `text` to Unicode (if it's not already), assuming utf-8 input."""
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text.decode("utf-8", "ignore")
elif isinstance(text, unicode):
return text
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
示例10: printable_text
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def printable_text(text):
"""Returns text encoded in a way suitable for print or `tf.logging`."""
# These functions want `str` for both Python2 and Python3, but in one case
# it's a Unicode string and in the other it's a byte string.
if six.PY3:
if isinstance(text, str):
return text
elif isinstance(text, bytes):
return text.decode("utf-8", "ignore")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
elif six.PY2:
if isinstance(text, str):
return text
elif isinstance(text, unicode):
return text.encode("utf-8")
else:
raise ValueError("Unsupported string type: %s" % (type(text)))
else:
raise ValueError("Not running on Python2 or Python 3?")
示例11: to_code
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def to_code(self):
"""
Convert this instance back into a native python code object. This
only works if the internals of the code object are compatible with
those of the running python version.
Returns:
types.CodeType: The native python code object.
"""
if self.internals is not get_py_internals():
raise ValueError('CodeObject is not compatible with the running python internals.')
if six.PY2:
return types.CodeType(
self.co_argcount, self.co_nlocals, self.co_stacksize, self.co_flags, self.co_code, self.co_consts,
self.co_names, self.co_varnames, self.co_filename, self.co_name, self.co_firstlineno, self.co_lnotab,
self.co_freevars, self.co_cellvars
)
else:
return types.CodeType(
self.co_argcount, self.co_kwonlyargcount, self.co_nlocals, self.co_stacksize, self.co_flags,
self.co_code, self.co_consts, self.co_names, self.co_varnames, self.co_filename, self.co_name,
self.co_firstlineno, self.co_lnotab, self.co_freevars, self.co_cellvars
)
示例12: create_parser
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def create_parser(self):
parser = configparser.ConfigParser(
interpolation=configparser.ExtendedInterpolation(),
defaults=self.defaults
)
parser.optionxform = str
# add env
parser.add_section(u'ENV')
for k, v in os.environ.items():
if set(v).intersection('\r\n\x00'):
continue
if six.PY2:
k = k.decode('utf-8', 'replace')
v = v.decode('utf-8', 'replace')
parser.set(u'ENV', k, v.replace(u'$', u'$$'))
# default globals
parser.add_section(u'global')
parser.set(u'global', u'auditlog', u'${logdir}/audit.log')
parser.set(u'global', u'debug', u'false')
parser.set(u'global', u'umask', u'027')
parser.set(u'global', u'makedirs', u'false')
return parser
示例13: _initial_load
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def _initial_load():
global _CUR_CONTEXT
to_assign = {}
ctx_path = os.environ.get(ENV_KEY)
if ctx_path:
if six.PY2:
ctx_path = ctx_path.decode(sys.getfilesystemencoding())
_LOGGER.debug('Loading LUCI_CONTEXT: %r', ctx_path)
try:
with open(ctx_path, 'r') as f:
loaded = _to_utf8(json.load(f))
if _check_ok(loaded):
to_assign = loaded
except OSError as ex:
_LOGGER.error('LUCI_CONTEXT failed to open: %s', ex)
except IOError as ex:
_LOGGER.error('LUCI_CONTEXT failed to read: %s', ex)
except ValueError as ex:
_LOGGER.error('LUCI_CONTEXT failed to decode: %s', ex)
_CUR_CONTEXT = to_assign
示例14: fromCSV
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def fromCSV(cls, data, fields):
data = data.rstrip("\n")
# If Python 2, encode to utf-8 since CSV doesn't take unicode input
if six.PY2:
data = data.encode('utf-8')
# Use Python CSV module to parse the CSV line
parsed_csv_lines = csv.reader([data])
# If Python 2, decode back to unicode (the original input format).
if six.PY2:
for line in parsed_csv_lines:
parsed_csv_line = [six.text_type(col, 'utf-8') for col in line]
break
else:
parsed_csv_line = list(parsed_csv_lines)[0]
return cls.fromlist(parsed_csv_line, fields)
示例15: preprocess
# 需要导入模块: import six [as 别名]
# 或者: from six import PY2 [as 别名]
def preprocess(self, x):
"""Load a single example using this field, tokenizing if necessary.
If the input is a Python 2 `str`, it will be converted to Unicode
first. If `sequential=True`, it will be tokenized. Then the input
will be optionally lowercased and passed to the user-provided
`preprocessing` Pipeline."""
if (six.PY2 and isinstance(x, six.string_types) and not
isinstance(x, six.text_type)):
x = Pipeline(lambda s: six.text_type(s, encoding='utf-8'))(x)
if self.sequential and isinstance(x, six.text_type):
x = self.tokenize(x.rstrip('\n'))
if self.lower:
x = Pipeline(six.text_type.lower)(x)
if self.preprocessing is not None:
return self.preprocessing(x)
else:
return x