本文整理汇总了Python中six.BytesIO.getvalue方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.getvalue方法的具体用法?Python BytesIO.getvalue怎么用?Python BytesIO.getvalue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.BytesIO
的用法示例。
在下文中一共展示了BytesIO.getvalue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_write
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_write(self):
r = StringIO('ab')
n = FrenchNormalizer(r)
m = Matrix()
w = BytesIO()
m.feed(n)
m.dump(w)
expected = {
(None,): {
False: 1,
},
(False,): {
'a': 1,
},
('a',): {
'b': 1,
},
('b',): {
True: 1
}
}
print(pickle.loads(w.getvalue()))
print(expected)
assert pickle.loads(w.getvalue()) == expected
w.close()
示例2: TFramedTransport
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
class TFramedTransport(TTransportBase, CReadableTransport):
"""Class that wraps another transport and frames its I/O when writing."""
def __init__(self, trans,):
self.__trans = trans
self.__rbuf = BytesIO()
self.__wbuf = BytesIO()
def isOpen(self):
return self.__trans.isOpen()
def open(self):
return self.__trans.open()
def close(self):
return self.__trans.close()
def read(self, sz):
ret = self.__rbuf.read(sz)
if len(ret) != 0:
return ret
self.readFrame()
return self.__rbuf.read(sz)
def readFrame(self):
buff = self.__trans.readAll(4)
sz, = unpack('!i', buff)
self.__rbuf = BytesIO(self.__trans.readAll(sz))
def write(self, buf):
self.__wbuf.write(buf)
def flush(self):
wout = self.__wbuf.getvalue()
wsz = len(wout)
# reset wbuf before write/flush to preserve state on underlying failure
self.__wbuf = BytesIO()
# N.B.: Doing this string concatenation is WAY cheaper than making
# two separate calls to the underlying socket object. Socket writes in
# Python turn out to be REALLY expensive, but it seems to do a pretty
# good job of managing string buffer operations without excessive copies
buf = pack("!i", wsz) + wout
self.__trans.write(buf)
self.__trans.flush()
# Implement the CReadableTransport interface.
@property
def cstringio_buf(self):
return self.__rbuf
def cstringio_refill(self, prefix, reqlen):
# self.__rbuf will already be empty here because fastbinary doesn't
# ask for a refill until the previous buffer is empty. Therefore,
# we can start reading new frames immediately.
while len(prefix) < reqlen:
self.readFrame()
prefix += self.__rbuf.getvalue()
self.__rbuf = BytesIO(prefix)
return self.__rbuf
示例3: test_save_model_with_writable_caches
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_save_model_with_writable_caches(self):
# If one or both cache elements are read-only, no saving.
expected_mean_vec = numpy.array([1, 2, 3])
expected_rotation = numpy.eye(3)
expected_mean_vec_bytes = BytesIO()
# noinspection PyTypeChecker
numpy.save(expected_mean_vec_bytes, expected_mean_vec)
expected_mean_vec_bytes = expected_mean_vec_bytes.getvalue()
expected_rotation_bytes = BytesIO()
# noinspection PyTypeChecker
numpy.save(expected_rotation_bytes, expected_rotation)
expected_rotation_bytes = expected_rotation_bytes.getvalue()
itq = ItqFunctor()
itq.mean_vec = expected_mean_vec
itq.rotation = expected_rotation
itq.mean_vec_cache_elem = DataMemoryElement(readonly=False)
itq.rotation_cache_elem = DataMemoryElement(readonly=False)
itq.save_model()
self.assertEqual(itq.mean_vec_cache_elem.get_bytes(),
expected_mean_vec_bytes)
self.assertEqual(itq.rotation_cache_elem.get_bytes(),
expected_rotation_bytes)
示例4: figure2base64
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def figure2base64(fig):
io = BytesIO()
fig.savefig(io, format='png')
try:
fig_base64 = base64.encodebytes(io.getvalue()) # py3
except:
fig_base64 = base64.encodestring(io.getvalue()) # py2
return fig_base64
示例5: test_50_get
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_50_get(self):
io = BytesIO()
self.webdav.download('handler.py', io)
self.assertEqual(inspect.getsource(data_handler), io.getvalue())
io.close()
io = BytesIO()
self.webdav.download('sample_handler.py', io)
self.assertEqual(inspect.getsource(data_sample_handler), io.getvalue())
io.close()
示例6: testExportDoesntIncludeParent
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def testExportDoesntIncludeParent(self):
self.app = self.layer['app']
self.app._setOb('foo', DummyFolder('foo'))
foo = self.app.foo
foo['bar'] = DummyFolder('bar')
savepoint(optimistic=True) # savepoint assigns oids
# now let's export to a buffer and check the objects...
exp = BytesIO()
self.app._p_jar.exportFile(foo.bar._p_oid, exp)
self.assertTrue(b'bar' in exp.getvalue())
self.assertFalse(b'foo' in exp.getvalue())
示例7: test_send_chunk
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_send_chunk():
v = b"foobarfoobar"
for bs in range(1, len(v) + 2):
s = BytesIO()
writer.send_chunk(s, v, bs, 0, len(v))
assert s.getvalue() == v
for start in range(len(v)):
for end in range(len(v)):
s = BytesIO()
writer.send_chunk(s, v, bs, start, end)
assert s.getvalue() == v[start:end]
示例8: UniversalBytesIO
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
class UniversalBytesIO(object):
def __init__(self, container=None, charset=None):
self.charset = charset or settings.DEFAULT_CHARSET
self._container = BytesIO() if container is None else container
# These methods partially implement the file-like object interface.
# See https://docs.python.org/3/library/io.html#io.IOBase
def close(self):
self._container.close()
def write(self, content):
self._container.write(self.make_bytes(content))
def flush(self):
self._container.flush()
def tell(self):
return self._container.tell()
def readable(self):
return False
def seekable(self):
return False
def writable(self):
return True
def writelines(self, lines):
for line in lines:
self.write(line)
def make_bytes(self, value):
"""Turn a value into a bytestring encoded in the output charset."""
if isinstance(value, bytes):
return bytes(value)
if isinstance(value, six.text_type):
return bytes(value.encode(self.charset))
# Handle non-string types
return force_bytes(value, self.charset)
def get_string_value(self):
return self._container.getvalue().decode(self.charset)
def getvalue(self):
return self._container.getvalue()
if sys.version_info[0:2] < (3, 5):
def seek(self, *args, **kwargs):
pass
示例9: test_raw
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_raw(self):
s = BytesIO()
r = next(language.parse_pathod("400:b'foo'"))
language.serve(r, s, {})
v = s.getvalue()
assert b"Content-Length" in v
s = BytesIO()
r = next(language.parse_pathod("400:b'foo':r"))
language.serve(r, s, {})
v = s.getvalue()
assert b"Content-Length" not in v
示例10: test_write_values
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_write_values():
tst = b"foobarvoing"
s = BytesIO()
writer.write_values(s, [tst], [])
assert s.getvalue() == tst
for bs in range(1, len(tst) + 2):
for off in range(len(tst)):
s = BytesIO()
writer.write_values(
s, [tst], [(off, "disconnect")], blocksize=bs
)
assert s.getvalue() == tst[:off]
示例11: test_write_values_inject
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_write_values_inject():
tst = b"foo"
s = BytesIO()
writer.write_values(s, [tst], [(0, "inject", b"aaa")], blocksize=5)
assert s.getvalue() == b"aaafoo"
s = BytesIO()
writer.write_values(s, [tst], [(1, "inject", b"aaa")], blocksize=5)
assert s.getvalue() == b"faaaoo"
s = BytesIO()
writer.write_values(s, [tst], [(1, "inject", b"aaa")], blocksize=5)
assert s.getvalue() == b"faaaoo"
示例12: test_disable_compression
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_disable_compression(self, *args):
c = self.make_connection()
c._callbacks = {0: c._handle_options_response}
c.defunct = Mock()
# disable compression
c.compression = False
locally_supported_compressions.pop('lz4', None)
locally_supported_compressions.pop('snappy', None)
locally_supported_compressions['lz4'] = ('lz4compress', 'lz4decompress')
locally_supported_compressions['snappy'] = ('snappycompress', 'snappydecompress')
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage)
# the server only supports snappy
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.3'],
'COMPRESSION': ['snappy', 'lz4']
})
options = options_buf.getvalue()
message = self.make_msg(header, options)
c.process_msg(message, len(message) - 8)
self.assertEqual(c.decompressor, None)
示例13: test_requested_compression_not_available
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_requested_compression_not_available(self, *args):
c = self.make_connection()
c._callbacks = {0: c._handle_options_response}
c.defunct = Mock()
# request lz4 compression
c.compression = "lz4"
locally_supported_compressions.pop('lz4', None)
locally_supported_compressions.pop('snappy', None)
locally_supported_compressions['lz4'] = ('lz4compress', 'lz4decompress')
locally_supported_compressions['snappy'] = ('snappycompress', 'snappydecompress')
# read in a SupportedMessage response
header = self.make_header_prefix(SupportedMessage)
# the server only supports snappy
options_buf = BytesIO()
write_stringmultimap(options_buf, {
'CQL_VERSION': ['3.0.3'],
'COMPRESSION': ['snappy']
})
options = options_buf.getvalue()
message = self.make_msg(header, options)
c.process_msg(message, len(message) - 8)
# make sure it errored correctly
c.defunct.assert_called_once_with(ANY)
args, kwargs = c.defunct.call_args
self.assertIsInstance(args[0], ProtocolError)
示例14: test_encode_decode_empty_string
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_encode_decode_empty_string():
# This is a regression test for
# https://github.com/luispedro/jug/issues/39
s = BytesIO()
jug.backends.encode.encode_to('', s)
val = jug.backends.encode.decode_from(BytesIO(s.getvalue()))
assert val == ''
示例15: test_get
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import getvalue [as 别名]
def test_get(self):
image = Image.new('RGBA', (8, 8))
image.paste((255, 0, 0, 0), (0, 0, 4, 4))
image.paste((0, 255, 0, 0), (0, 4, 4, 8))
image.paste((0, 0, 255, 0), (4, 0, 8, 4))
image.paste((0, 0, 0, 255), (4, 4, 8, 8))
string_io = StringIO()
image.save(string_io, 'PNG')
tile = Tile(TileCoord(1, 0, 0, 2), data=string_io.getvalue())
tiles = list(self.mtsts.get([tile]))
self.assertEqual(len(tiles), 4)
self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0))
image = Image.open(StringIO(tiles[0].data))
self.assertEqual(image.size, (2, 2))
self.assertEqual(image.getcolors(), [(4, (255, 0, 0, 0))])
self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1))
image = Image.open(StringIO(tiles[1].data))
self.assertEqual(image.size, (2, 2))
self.assertEqual(image.getcolors(), [(4, (0, 255, 0, 0))])
self.assertEqual(tiles[2].tilecoord, TileCoord(1, 1, 0))
image = Image.open(StringIO(tiles[2].data))
self.assertEqual(image.size, (2, 2))
self.assertEqual(image.getcolors(), [(4, (0, 0, 255, 0))])
self.assertEqual(tiles[3].tilecoord, TileCoord(1, 1, 1))
image = Image.open(StringIO(tiles[3].data))
self.assertEqual(image.size, (2, 2))
self.assertEqual(image.getcolors(), [(4, (0, 0, 0, 255))])