本文整理汇总了Python中six.BytesIO.read方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.read方法的具体用法?Python BytesIO.read怎么用?Python BytesIO.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.BytesIO
的用法示例。
在下文中一共展示了BytesIO.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: output_properties
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def output_properties(path=None, content=None, basename=None, pseduo_location=False):
checksum = hashlib.sha1()
properties = {
"class": "File",
}
if path is not None:
properties["path"] = path
f = open(path, "rb")
else:
f = BytesIO(content)
try:
contents = f.read(1024 * 1024)
filesize = 0
while contents:
checksum.update(contents)
filesize += len(contents)
contents = f.read(1024 * 1024)
finally:
f.close()
properties["checksum"] = "sha1$%s" % checksum.hexdigest()
properties["size"] = filesize
set_basename_and_derived_properties(properties, basename)
_handle_pseudo_location(properties, pseduo_location)
return properties
示例2: _onReceive
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def _onReceive(self, data):
"""Event handler when data from the display is received
Formats a packet of four bytes in a packet type and three arguments.
If the packet is known to have a payload, this is also fetched and the checksum is verified.
The constructed packet is handed off to L{_handlePacket}.
"""
if self.isUsb:
data = BytesIO(data)
packetType = data.read(1)
else:
packetType = data
data = self._dev
arg1 = data.read(1)
arg2 = data.read(1)
arg3 = data.read(1)
log.debug("Got packet of type %r with args: %r %r %r", packetType, arg1, arg2, arg3)
# Info and extended key responses are the only packets with payload and checksum
if packetType in (FS_PKT_INFO, FS_PKT_EXT_KEY):
length = ord(arg1)
payload = data.read(length)
checksum = ord(data.read(1))
calculatedChecksum = BrailleDisplayDriver._calculateChecksum(packetType + arg1 + arg2 + arg3 + payload)
assert calculatedChecksum == checksum, "Checksum mismatch, expected %s but got %s" % (checksum, payload[-1])
else:
payload = FS_DATA_EMPTY
self._handlePacket(packetType, arg1, arg2, arg3, payload)
示例3: test_create_file_chunked
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def test_create_file_chunked(self):
source = BytesIO(b'0123456789' * 1024 * 10) # 100k bytes
source.seek(0)
self.client.folder(self.root_folder.path).create()
f = self.client.file(self.filepath)
f.upload_chunk_size = 40000
f.upload(source)
dest = BytesIO()
self.client.file(self.filepath).download().write_to(dest)
dest.seek(0)
source.seek(0)
self.assertEqual(source.read(), dest.read(), "Uploaded and downloaded file's contents do not match")
partial_start = 5009
partial_size = 104
partial = f.download((partial_start, partial_start + partial_size - 1))
source.seek(partial_start)
source_content = source.read(partial_size)
partial_content = partial.read()
self.assertEqual(source_content, partial_content, "Partial download content does not match")
示例4: TFramedTransport
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [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
示例5: TBufferedTransport
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
class TBufferedTransport(TTransportBase, CReadableTransport):
"""Class that wraps another transport and buffers its I/O.
The implementation uses a (configurable) fixed-size read buffer
but buffers all writes until a flush is performed.
"""
DEFAULT_BUFFER = 4096
def __init__(self, trans, rbuf_size=DEFAULT_BUFFER):
self.__trans = trans
self.__wbuf = BytesIO()
self.__rbuf = BytesIO("")
self.__rbuf_size = rbuf_size
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.__rbuf = BytesIO(self.__trans.read(max(sz, self.__rbuf_size)))
return self.__rbuf.read(sz)
def write(self, buf):
self.__wbuf.write(buf)
def flush(self):
out = self.__wbuf.getvalue()
# reset wbuf before write/flush to preserve state on underlying failure
self.__wbuf = BytesIO()
self.__trans.write(out)
self.__trans.flush()
# Implement the CReadableTransport interface.
@property
def cstringio_buf(self):
return self.__rbuf
def cstringio_refill(self, partialread, reqlen):
retstring = partialread
if reqlen < self.__rbuf_size:
# try to make a read of as much as we can.
retstring += self.__trans.read(self.__rbuf_size)
# but make sure we do read reqlen bytes.
if len(retstring) < reqlen:
retstring += self.__trans.readAll(reqlen - len(retstring))
self.__rbuf = BytesIO(retstring)
return self.__rbuf
示例6: QiniuFile
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
class QiniuFile(File):
def __init__(self, name, storage, mode):
self._storage = storage
if name.startswith(self._storage.location):
name = name[len(self._storage.location):]
self._name = name.lstrip('/')
self._mode = mode
self.file = BytesIO()
self._is_dirty = False
self._is_read = False
@property
def size(self):
if self._is_dirty or self._is_read:
# Get the size of a file like object
# Check http://stackoverflow.com/a/19079887
old_file_position = self.file.tell()
self.file.seek(0, os.SEEK_END)
self._size = self.file.tell()
self.file.seek(old_file_position, os.SEEK_SET)
if not hasattr(self, '_size'):
self._size = self._storage.size(self._name)
return self._size
def read(self, num_bytes=None):
if not self._is_read:
content = self._storage._read(self._name)
self.file = BytesIO(content)
self._is_read = True
if num_bytes is None:
data = self.file.read()
else:
data = self.file.read(num_bytes)
if 'b' in self._mode:
return data
else:
return force_text(data)
def write(self, content):
if 'w' not in self._mode:
raise AttributeError("File was opened for read-only access.")
self.file.write(force_bytes(content))
self._is_dirty = True
self._is_read = True
def close(self):
if self._is_dirty:
self.file.seek(0)
self._storage._save(self._name, self.file)
self.file.close()
示例7: test_create_file_bytesio
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def test_create_file_bytesio(self):
source = BytesIO(b'vijayendra')
source.seek(0)
self.client.folder(self.root_folder.path).create()
self.client.file(self.filepath).upload(source)
dest = BytesIO()
self.client.file(self.filepath).download().write_to(dest)
dest.seek(0)
source.seek(0)
self.assertEqual(source.read(), dest.read(), "Uploaded and downloaded file's contents do not match")
示例8: FakeResponse
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
class FakeResponse(object):
"""A fake HTTPResponse object for testing."""
def __init__(self, code, body, headers=None):
self.code = code
self.msg = str(code)
if headers is None:
headers = {}
self.headers = headers
self.info = lambda: self.headers
if isinstance(body, six.text_type):
body = body.encode('utf-8')
self.body_file = BytesIO(body)
def read(self):
"""Read the entire response body."""
return self.body_file.read()
def readline(self):
"""Read a single line from the response body."""
return self.body_file.readline()
def close(self):
"""Close the connection."""
pass
示例9: _get_header
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def _get_header(self):
chunk = self._transport.chunk_read(0, HEADER_SIZE_GUESS)
stream = BytesIO(chunk)
magic = read_n(stream, len(MAGIC))
if magic == INCOMPLETE_MAGIC:
raise ZSCorrupt("%s: looks like this ZS file was only "
"partially written" % (self._transport.name,))
if magic != MAGIC:
raise ZSCorrupt("%s: bad magic number (are you sure this is "
"a ZS file?)" % (self._transport.name))
header_data_length, = read_format(stream, header_data_length_format)
needed = header_data_length + CRC_LENGTH
header_end = stream.tell() + needed
remaining = len(chunk) - stream.tell()
if remaining < needed:
rest = self._transport.chunk_read(len(chunk), needed - remaining)
stream = BytesIO(stream.read() + rest)
header_encoded = read_n(stream, header_data_length)
header_crc = read_n(stream, CRC_LENGTH)
if encoded_crc64xz(header_encoded) != header_crc:
raise ZSCorrupt("%s: header checksum mismatch"
% (self._transport.name,))
return _decode_header_data(header_encoded), header_end
示例10: default
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def default(self, obj):
try:
return super(ObjectJSONEncoder, self).default(obj)
except TypeError as e:
if "not JSON serializable" not in str(e):
raise
if isinstance(obj, datetime.datetime):
return {'ISO8601_datetime': obj.strftime('%Y-%m-%dT%H:%M:%S.%f%z')}
if isinstance(obj, datetime.date):
return {'ISO8601_date': obj.isoformat()}
if numpy is not None and isinstance(obj, numpy.ndarray) and obj.ndim == 1:
memfile = BytesIO()
numpy.save(memfile, obj)
memfile.seek(0)
serialized = json.dumps(memfile.read().decode('latin-1'))
d = {
'__ndarray__': serialized,
}
return d
else:
d = {
'__class__': obj.__class__.__qualname__,
'__module__': obj.__module__,
}
return d
示例11: test_xmlrunner_check_for_valid_xml_streamout
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def test_xmlrunner_check_for_valid_xml_streamout(self):
"""
This test checks if the xml document is valid if there are more than
one testsuite and the output of the report is a single stream.
"""
class DummyTestA(unittest.TestCase):
def test_pass(self):
pass
class DummyTestB(unittest.TestCase):
def test_pass(self):
pass
suite = unittest.TestSuite()
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DummyTestA))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DummyTestB))
outdir = BytesIO()
runner = xmlrunner.XMLTestRunner(
stream=self.stream, output=outdir, verbosity=self.verbosity,
**self.runner_kwargs)
runner.run(suite)
outdir.seek(0)
output = outdir.read()
# Finally check if we have a valid XML document or not.
try:
minidom.parseString(output)
except Exception as e: # pragma: no cover
# note: we could remove the try/except, but it's more crude.
self.fail(e)
示例12: generate_glassbrain_image
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def generate_glassbrain_image(image_pk):
from neurovault.apps.statmaps.models import Image
import neurovault
import matplotlib as mpl
mpl.rcParams['savefig.format'] = 'jpg'
my_dpi = 50
fig = plt.figure(figsize=(330.0/my_dpi, 130.0/my_dpi), dpi=my_dpi)
img = Image.objects.get(pk=image_pk)
f = BytesIO()
try:
glass_brain = plot_glass_brain(img.file.path, figure=fig)
glass_brain.savefig(f, dpi=my_dpi)
except:
# Glass brains that do not produce will be given dummy image
this_path = os.path.abspath(os.path.dirname(__file__))
f = open(os.path.abspath(os.path.join(this_path,
"static","images","glass_brain_empty.jpg")))
raise
finally:
plt.close('all')
f.seek(0)
content_file = ContentFile(f.read())
img.thumbnail.save("glass_brain_%s.jpg" % img.pk, content_file)
img.save()
示例13: test_load_write_file
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def test_load_write_file(self):
for fname in [DATA['empty_tck_fname'],
DATA['simple_tck_fname']]:
for lazy_load in [False, True]:
tck = TckFile.load(fname, lazy_load=lazy_load)
tck_file = BytesIO()
tck.save(tck_file)
loaded_tck = TckFile.load(fname, lazy_load=False)
assert_tractogram_equal(loaded_tck.tractogram, tck.tractogram)
# Check that the written file is the same as the one read.
tck_file.seek(0, os.SEEK_SET)
assert_equal(tck_file.read(), open(fname, 'rb').read())
# Save tractogram that has an affine_to_rasmm.
for lazy_load in [False, True]:
tck = TckFile.load(DATA['simple_tck_fname'], lazy_load=lazy_load)
affine = np.eye(4)
affine[0, 0] *= -1 # Flip in X
tractogram = Tractogram(tck.streamlines, affine_to_rasmm=affine)
new_tck = TckFile(tractogram, tck.header)
tck_file = BytesIO()
new_tck.save(tck_file)
tck_file.seek(0, os.SEEK_SET)
loaded_tck = TckFile.load(tck_file, lazy_load=False)
assert_tractogram_equal(loaded_tck.tractogram,
tractogram.to_world(lazy=True))
示例14: test_write_simple_file
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def test_write_simple_file(self):
tractogram = Tractogram(DATA['streamlines'],
affine_to_rasmm=np.eye(4))
tck_file = BytesIO()
tck = TckFile(tractogram)
tck.save(tck_file)
tck_file.seek(0, os.SEEK_SET)
new_tck = TckFile.load(tck_file)
assert_tractogram_equal(new_tck.tractogram, tractogram)
new_tck_orig = TckFile.load(DATA['simple_tck_fname'])
assert_tractogram_equal(new_tck.tractogram, new_tck_orig.tractogram)
tck_file.seek(0, os.SEEK_SET)
assert_equal(tck_file.read(),
open(DATA['simple_tck_fname'], 'rb').read())
# TCK file containing not well formatted entries in its header.
tck_file = BytesIO()
tck = TckFile(tractogram)
tck.header['new_entry'] = 'value\n' # \n not allowed
assert_raises(HeaderError, tck.save, tck_file)
tck.header['new_entry'] = 'val:ue' # : not allowed
assert_raises(HeaderError, tck.save, tck_file)
示例15: tostring
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import read [as 别名]
def tostring(self):
"""
Return a string representation of the entire XML document.
"""
out = BytesIO()
self.write(out)
out.seek(0)
return out.read()