Python six.PY2 Examples
The following are 30
code examples of six.PY2().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
six
, or try the search function
.
Example #1
Source File: config.py From custodia with GNU General Public License v3.0 | 6 votes |
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
Example #2
Source File: tokenization.py From BERT-for-Chinese-Question-Answering with Apache License 2.0 | 6 votes |
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?")
Example #3
Source File: _parser.py From plugin.video.emby with GNU General Public License v3.0 | 6 votes |
def __init__(self, instream): if six.PY2: # In Python 2, we can't duck type properly because unicode has # a 'decode' function, and we'd be double-decoding if isinstance(instream, (binary_type, bytearray)): instream = instream.decode() else: if getattr(instream, 'decode', None) is not None: instream = instream.decode() if isinstance(instream, text_type): instream = StringIO(instream) elif getattr(instream, 'read', None) is None: raise TypeError('Parser must be a string or character stream, not ' '{itype}'.format(itype=instream.__class__.__name__)) self.instream = instream self.charstack = [] self.tokenstack = [] self.eof = False
Example #4
Source File: _parser.py From recruit with Apache License 2.0 | 6 votes |
def __init__(self, instream): if six.PY2: # In Python 2, we can't duck type properly because unicode has # a 'decode' function, and we'd be double-decoding if isinstance(instream, (bytes, bytearray)): instream = instream.decode() else: if getattr(instream, 'decode', None) is not None: instream = instream.decode() if isinstance(instream, text_type): instream = StringIO(instream) elif getattr(instream, 'read', None) is None: raise TypeError('Parser must be a string or character stream, not ' '{itype}'.format(itype=instream.__class__.__name__)) self.instream = instream self.charstack = [] self.tokenstack = [] self.eof = False
Example #5
Source File: _common.py From recruit with Apache License 2.0 | 6 votes |
def tzname_in_python2(namefunc): """Change unicode output into bytestrings in Python 2 tzname() API changed in Python 3. It used to return bytes, but was changed to unicode strings """ if PY2: @wraps(namefunc) def adjust_encoding(*args, **kwargs): name = namefunc(*args, **kwargs) if name is not None: name = name.encode() return name return adjust_encoding else: return namefunc # The following is adapted from Alexander Belopolsky's tz library # https://github.com/abalkin/tz
Example #6
Source File: utils.py From OpenNRE with MIT License | 6 votes |
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?")
Example #7
Source File: utils.py From OpenNRE with MIT License | 6 votes |
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?")
Example #8
Source File: __init__.py From neuropythy with GNU Affero General Public License v3.0 | 6 votes |
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'])
Example #9
Source File: tokenization.py From BERT-Classification-Tutorial with Apache License 2.0 | 6 votes |
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?")
Example #10
Source File: tokenization.py From BERT-for-Chinese-Question-Answering with Apache License 2.0 | 6 votes |
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?")
Example #11
Source File: quora_qpairs.py From fine-lm with MIT License | 6 votes |
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) }
Example #12
Source File: python3.py From linter-pylama with MIT License | 6 votes |
def visit_raise(self, node): """Visit a raise statement and check for raising strings or old-raise-syntax. """ if six.PY2: if (node.exc is not None and node.inst is not None): self.add_message('old-raise-syntax', node=node) # Ignore empty raise. if node.exc is None: return expr = node.exc if self._check_raise_value(node, expr): return try: value = next(astroid.unpack_infer(expr)) except astroid.InferenceError: return self._check_raise_value(node, value)
Example #13
Source File: luci_context.py From recipes-py with Apache License 2.0 | 6 votes |
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
Example #14
Source File: generator_utils.py From fine-lm with MIT License | 6 votes |
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))
Example #15
Source File: helpers.py From linter-pylama with MIT License | 6 votes |
def _function_type(function, builtins): if isinstance(function, scoped_nodes.Lambda): if function.root().name == BUILTINS: cls_name = 'builtin_function_or_method' else: cls_name = 'function' elif isinstance(function, bases.BoundMethod): if six.PY2: cls_name = 'instancemethod' else: cls_name = 'method' elif isinstance(function, bases.UnboundMethod): if six.PY2: cls_name = 'instancemethod' else: cls_name = 'function' return _build_proxy_class(cls_name, builtins)
Example #16
Source File: field.py From decaNLP with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #17
Source File: example.py From decaNLP with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #18
Source File: tokenization.py From tudouNLP with MIT License | 6 votes |
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?")
Example #19
Source File: tokenization.py From tudouNLP with MIT License | 6 votes |
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?")
Example #20
Source File: multinli.py From fine-lm with MIT License | 6 votes |
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 }
Example #21
Source File: tokenization.py From BERT-Classification-Tutorial with Apache License 2.0 | 6 votes |
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?")
Example #22
Source File: bytecode.py From pwnypack with MIT License | 6 votes |
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 )
Example #23
Source File: bert_wrapper.py From UDPipe-Future with Mozilla Public License 2.0 | 6 votes |
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?")
Example #24
Source File: bert_wrapper.py From UDPipe-Future with Mozilla Public License 2.0 | 6 votes |
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?")
Example #25
Source File: _pickle_store.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def read(self, mongoose_lib, version, symbol, **kwargs): blob = version.get("blob") if blob is not None: if blob == _MAGIC_CHUNKEDV2: collection = mongoose_lib.get_top_level_collection() data = b''.join(decompress(x['data']) for x in sorted( collection.find({'symbol': symbol, 'parent': version_base_or_id(version)}), key=itemgetter('segment'))) elif blob == _MAGIC_CHUNKED: collection = mongoose_lib.get_top_level_collection() data = b''.join(x['data'] for x in sorted( collection.find({'symbol': symbol, 'parent': version_base_or_id(version)}), key=itemgetter('segment'))) data = decompress(data) else: if blob[:len(_MAGIC_CHUNKED)] == _MAGIC_CHUNKED: logger.error("Data was written by unsupported version of pickle store for symbol %s. Upgrade Arctic and try again" % symbol) raise UnsupportedPickleStoreVersion("Data was written by unsupported version of pickle store") try: data = decompress(blob) except: logger.error("Failed to read symbol %s" % symbol) if six.PY2: # Providing encoding is not possible on PY2 return pickle_compat_load(io.BytesIO(data)) else: try: # The default encoding is ascii. return pickle_compat_load(io.BytesIO(data)) except UnicodeDecodeError as ue: # Using encoding='latin1' is required for unpickling NumPy arrays and instances of datetime, date # and time pickled by Python 2: https://docs.python.org/3/library/pickle.html#pickle.load logger.info("Could not Unpickle with ascii, Using latin1.") encoding = kwargs.get('encoding', 'latin_1') # Check if someone has manually specified encoding. return pickle_compat_load(io.BytesIO(data), encoding=encoding) return version['data']
Example #26
Source File: numpy_records.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def deserialize(self, item, force_bytes_to_unicode=False): index = self._index_from_records(item) name = item.dtype.names[-1] data = item[name] if force_bytes_to_unicode: if six.PY2 and isinstance(name, (bytes, str)): name = name.decode('utf-8') if len(data) and isinstance(data[0], bytes): data = data.astype('unicode') if isinstance(index, MultiIndex): unicode_indexes = [] # MultiIndex requires a conversion at each level. for level in range(len(index.levels)): _index = index.get_level_values(level) if isinstance(_index[0], bytes): _index = _index.astype('unicode') unicode_indexes.append(_index) index = unicode_indexes else: if len(index) and type(index[0]) == bytes: index = index.astype('unicode') if PD_VER < '0.23.0': return Series.from_array(data, index=index, name=name) else: return Series(data, index=index, name=name)
Example #27
Source File: _pytestplugin.py From tox with MIT License | 5 votes |
def __repr__(self): res = "RunResult(ret={}, args={!r}, out=\n{}\n, err=\n{})".format( self.ret, self.args, self.out, self.err, ) if six.PY2: return res.encode("UTF-8") else: return res
Example #28
Source File: utils.py From hyperledger-py with Apache License 2.0 | 5 votes |
def split_command(command): if six.PY2 and not isinstance(command, six.binary_type): command = command.encode('utf-8') return shlex.split(command)
Example #29
Source File: _pytestplugin.py From tox with MIT License | 5 votes |
def clear(self): self._index = -1 if not six.PY2: self.instance.reported_lines.clear() else: del self.instance.reported_lines[:]
Example #30
Source File: base.py From hyperledger-py with Apache License 2.0 | 5 votes |
def assertIn(self, object, collection): if six.PY2 and sys.version_info[1] <= 6: return self.assertTrue(object in collection) return super(BaseTestCase, self).assertIn(object, collection)