本文整理汇总了Python中ipython_genutils.py3compat.PY3属性的典型用法代码示例。如果您正苦于以下问题:Python py3compat.PY3属性的具体用法?Python py3compat.PY3怎么用?Python py3compat.PY3使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类ipython_genutils.py3compat
的用法示例。
在下文中一共展示了py3compat.PY3属性的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unicode_std_stream
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def unicode_std_stream(stream='stdout'):
u"""Get a wrapper to write unicode to stdout/stderr as UTF-8.
This ignores environment variables and default encodings, to reliably write
unicode to stdout or stderr.
::
unicode_std_stream().write(u'ł@e¶ŧ←')
"""
assert stream in ('stdout', 'stderr')
stream = getattr(sys, stream)
if PY3:
try:
stream_b = stream.buffer
except AttributeError:
# sys.stdout has been replaced - use it directly
return stream
else:
stream_b = stream
return codecs.getwriter('utf-8')(stream_b)
示例2: _lexer_class_default
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def _lexer_class_default(self):
if py3compat.PY3:
return 'pygments.lexers.Python3Lexer'
else:
return 'pygments.lexers.PythonLexer'
示例3: __init__
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def __init__(self, parent, lexer=None):
super(PygmentsHighlighter, self).__init__(parent)
self._document = self.document()
self._formatter = HtmlFormatter(nowrap=True)
self.set_style('default')
if lexer is not None:
self._lexer = lexer
else:
if PY3:
self._lexer = Python3Lexer()
else:
self._lexer = PythonLexer()
示例4: _buffer_list_equal
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def _buffer_list_equal(a, b):
"""Compare two lists of buffers for equality.
Used to decide whether two sequences of buffers (memoryviews,
bytearrays, or python 3 bytes) differ, such that a sync is needed.
Returns True if equal, False if unequal
"""
if len(a) != len(b):
return False
if a == b:
return True
for ia, ib in zip(a, b):
# Check byte equality, since bytes are what is actually synced
# NOTE: Simple ia != ib does not always work as intended, as
# e.g. memoryview(np.frombuffer(ia, dtype='float32')) !=
# memoryview(np.frombuffer(b)), since the format info differs.
if PY3:
# compare without copying
if memoryview(ia).cast('B') != memoryview(ib).cast('B'):
return False
else:
# python 2 doesn't have memoryview.cast, so we may have to copy
if isinstance(ia, memoryview) and ia.format != 'B':
ia = ia.tobytes()
if isinstance(ib, memoryview) and ib.format != 'B':
ib = ib.tobytes()
if ia != ib:
return False
return True
示例5: get_state
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def get_state(self, key=None, drop_defaults=False):
"""Gets the widget state, or a piece of it.
Parameters
----------
key : unicode or iterable (optional)
A single property's name or iterable of property names to get.
Returns
-------
state : dict of states
metadata : dict
metadata for each field: {key: metadata}
"""
if key is None:
keys = self.keys
elif isinstance(key, string_types):
keys = [key]
elif isinstance(key, collections.Iterable):
keys = key
else:
raise ValueError("key must be a string, an iterable of keys, or None")
state = {}
traits = self.traits()
for k in keys:
to_json = self.trait_metadata(k, 'to_json', self._trait_to_json)
value = to_json(getattr(self, k), self)
if not PY3 and isinstance(traits[k], Bytes) and isinstance(value, bytes):
value = memoryview(value)
if not drop_defaults or not self._compare(value, traits[k].default_value):
state[k] = value
return state
示例6: _nbytes
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def _nbytes(buf):
"""Return byte-size of a memoryview or buffer."""
if isinstance(buf, memoryview):
if PY3:
# py3 introduces nbytes attribute
return buf.nbytes
else:
# compute nbytes on py2
size = buf.itemsize
for dim in buf.shape:
size *= dim
return size
else:
# not a memoryview, raw bytes/ py2 buffer
return len(buf)
示例7: get_object
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def get_object(self, g=None):
from numpy import frombuffer
data = self.buffers[0]
if self.pickled:
from . import serialize
# we just pickled it
return serialize.pickle.loads(buffer_to_bytes_py2(data))
else:
if not py3compat.PY3 and isinstance(data, memoryview):
# frombuffer doesn't accept memoryviews on Python 2,
# so cast to old-style buffer
data = buffer(data.tobytes())
return frombuffer(data, dtype=self.dtype).reshape(self.shape)
示例8: test_frontend_to_kernel
# 需要导入模块: from ipython_genutils import py3compat [as 别名]
# 或者: from ipython_genutils.py3compat import PY3 [as 别名]
def test_frontend_to_kernel(self):
"""Communicate from the frontend to the kernel."""
comm_manager = self.comm_manager
blocking_client = self.blocking_client
blocking_client.execute(
"class DummyCommHandler():\n"
" def __init__(self):\n"
" get_ipython().kernel.comm_manager.register_target(\n"
" 'test_api', self.comm_open)\n"
" def comm_open(self, comm, msg):\n"
" comm.on_msg(self.comm_message)\n"
" comm.on_close(self.comm_message)\n"
" print(msg['content']['data'])\n"
" def comm_message(self, msg):\n"
" print(msg['content']['data'])\n"
"dummy = DummyCommHandler()\n"
)
# Get input
msg = self._get_next_msg()
assert msg['header']['msg_type'] == 'execute_input'
# Open comm
comm = comm_manager.new_comm('test_api', data='open')
msg = self._get_next_msg()
assert msg['header']['msg_type'] == 'stream'
assert msg['content']['text'] == 'open\n'
# Get message
comm.send('message')
msg = self._get_next_msg()
assert msg['header']['msg_type'] == 'stream'
assert msg['content']['text'] == 'message\n'
# Get close
comm.close('close')
msg = self._get_next_msg()
# Received message has a header and parent header. The parent header has
# the info about the close message type in Python 3
if PY3:
assert msg['parent_header']['msg_type'] == 'comm_close'
assert msg['msg_type'] == 'stream'
assert msg['content']['text'] == 'close\n'
else:
# For some reason ipykernel notifies me that it is closing,
# even though I closed the comm
assert msg['header']['msg_type'] == 'comm_close'
assert comm.comm_id == msg['content']['comm_id']
msg = self._get_next_msg()
assert msg['header']['msg_type'] == 'stream'