本文整理汇总了Python中io.RawIOBase方法的典型用法代码示例。如果您正苦于以下问题:Python io.RawIOBase方法的具体用法?Python io.RawIOBase怎么用?Python io.RawIOBase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io
的用法示例。
在下文中一共展示了io.RawIOBase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def compute(self, stream_in: RawIOBase, stream_out: RawIOBase = None):
"""Compute and return the hash for the given stream.
The data is read from stream_in until EOF, given to the hasher, and
written unmodified to the stream_out (unless it is None).
:param stream_in: input stream.
:type stream_in: io.RawIOBase
:param stream_out: output stream.
:type stream_out: io.RawIOBase
"""
self._init_hashers()
read_bytes = self.chunk_size
while read_bytes == self.chunk_size:
data = stream_in.read(self.chunk_size)
read_bytes = len(data)
for hasher in self._hashers.values():
hasher.update(data)
if stream_out is not None:
stream_out.write(data)
示例2: _gen_file
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def _gen_file(self, filename, file_location=None, file=None, content_type=None):
"""Yields the entire contents of a file.
Parameters
----------
filename : str
Filename of the file being opened and added to the HTTP body
file_location : str
Full path to the file being added, including the filename
file : io.RawIOBase
The binary file-like object whose contents should be streamed
No contents will be streamed if this is ``None``.
content_type : str
The Content-Type of the file; if not set a value will be guessed
"""
yield from self._gen_file_start(filename, file_location, content_type)
if file:
yield from self._gen_file_chunks(file)
yield from self._gen_file_end()
示例3: setUp
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def setUp(self):
self.loop = self.new_test_loop()
self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
self.pipe = mock.Mock(spec_set=io.RawIOBase)
self.pipe.fileno.return_value = 5
blocking_patcher = mock.patch('asyncio.unix_events._set_nonblocking')
blocking_patcher.start()
self.addCleanup(blocking_patcher.stop)
fstat_patcher = mock.patch('os.fstat')
m_fstat = fstat_patcher.start()
st = mock.Mock()
st.st_mode = stat.S_IFIFO
m_fstat.return_value = st
self.addCleanup(fstat_patcher.stop)
示例4: chunked_hash
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def chunked_hash(stream: Union[io.RawIOBase, io.BufferedIOBase], chunk_size: int = DEFAULT_MD5_CHUNK_SIZE) -> List[bytes]:
"""Create a list of hashes of chunk_size size in bytes.
Args:
stream (Union[io.RawIOBase, io.BufferedIOBase]): The steam containing the bytes to be hashed.
chunk_size (int): The md5 chunk size. Default is 10485760 (which is required for InstallApplication).
Returns:
List[str]: A list of md5 hashes calculated for each chunk
"""
chunk = stream.read(chunk_size)
hashes = []
while chunk is not None:
h = hashlib.md5()
h.update(chunk)
md5 = h.digest()
hashes.append(md5)
chunk = stream.read(chunk_size)
return hashes
示例5: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def _gettextwriter(out, encoding):
if out is None:
import sys
return sys.stdout
if isinstance(out, io.TextIOBase):
# use a text writer as is
return out
# wrap a binary writer with TextIOWrapper
if isinstance(out, io.RawIOBase):
# Keep the original file open when the TextIOWrapper is
# destroyed
class _wrapper:
__class__ = out.__class__
def __getattr__(self, name):
return getattr(out, name)
buffer = _wrapper()
buffer.close = lambda: None
else:
# This is to handle passed objects that aren't in the
# IOBase hierarchy, but just have a write method
buffer = io.BufferedIOBase()
buffer.writable = lambda: True
buffer.write = out.write
try:
# TextIOWrapper uses this methods to determine
# if BOM (for UTF-16, etc) should be added
buffer.seekable = out.seekable
buffer.tell = out.tell
except AttributeError:
pass
return io.TextIOWrapper(buffer, encoding=encoding,
errors='xmlcharrefreplace',
newline='\n',
write_through=True)
示例6: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def __init__(self, sock, mode):
if mode not in ("r", "w", "rw", "rb", "wb", "rwb"):
raise ValueError("invalid mode: %r" % mode)
io.RawIOBase.__init__(self)
self._sock = sock
if "b" not in mode:
mode += "b"
self._mode = mode
self._reading = "r" in mode
self._writing = "w" in mode
self._timeout_occurred = False
示例7: close
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def close(self):
"""Close the SocketIO object. This doesn't close the underlying
socket, except if all references to it have disappeared.
"""
if self.closed:
return
io.RawIOBase.close(self)
self._sock._decref_socketios()
self._sock = None
示例8: isatty
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def isatty(self):
io.RawIOBase.isatty(self)
return True
示例9: test_type_error_on_non_seekable_handle
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def test_type_error_on_non_seekable_handle(self):
with pytest.raises(TypeError):
create_reader(io.RawIOBase())
示例10: open
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def open(self, fname, mode='r', psw=None):
"""Returns file-like object (:class:`RarExtFile`) from where the data can be read.
The object implements :class:`io.RawIOBase` interface, so it can
be further wrapped with :class:`io.BufferedReader`
and :class:`io.TextIOWrapper`.
On older Python where io module is not available, it implements
only .read(), .seek(), .tell() and .close() methods.
The object is seekable, although the seeking is fast only on
uncompressed files, on compressed files the seeking is implemented
by reading ahead and/or restarting the decompression.
Parameters:
fname
file name or RarInfo instance.
mode
must be 'r'
psw
password to use for extracting.
"""
if mode != 'r':
raise NotImplementedError("RarFile.open() supports only mode=r")
# entry lookup
inf = self.getinfo(fname)
if inf.isdir():
raise TypeError("Directory does not have any data: " + inf.filename)
# check password
if inf.needs_password():
psw = psw or self._password
if psw is None:
raise PasswordRequired("File %s requires password" % inf.filename)
else:
psw = None
return self._file_parser.open(inf, psw)
示例11: readall
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def readall(self):
"""Read all remaining data"""
# avoid RawIOBase default impl
return self.read()
示例12: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def _gettextwriter(out, encoding):
if out is None:
import sys
out = sys.stdout
if isinstance(out, io.RawIOBase):
buffer = io.BufferedIOBase(out)
# Keep the original file open when the TextIOWrapper is
# destroyed
buffer.close = lambda: None
else:
# This is to handle passed objects that aren't in the
# IOBase hierarchy, but just have a write method
buffer = io.BufferedIOBase()
buffer.writable = lambda: True
buffer.write = out.write
try:
# TextIOWrapper uses this methods to determine
# if BOM (for UTF-16, etc) should be added
buffer.seekable = out.seekable
buffer.tell = out.tell
except AttributeError:
pass
# wrap a binary writer with TextIOWrapper
return _UnbufferedTextIOWrapper(buffer, encoding=encoding,
errors='xmlcharrefreplace',
newline='\n')
示例13: _gen_file_chunks
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def _gen_file_chunks(self, file):
"""Yields chunks of a file.
Parameters
----------
fp : io.RawIOBase
The file to break into chunks
(must be an open file or have the ``readinto`` method)
"""
while True:
buf = file.read(self.chunk_size)
if len(buf) < 1:
break
yield buf
示例14: write
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def write(self, path, file, offset=0, create=False, truncate=False, count=None, **kwargs):
"""Writes to a mutable file in the MFS.
.. code-block:: python
>>> client.files.write("/test/file", io.BytesIO(b"hi"), create=True)
Parameters
----------
path : str
Filepath within the MFS
file : Union[str, bytes, os.PathLike, io.RawIOBase, int]
IO stream object with data that should be written
offset : int
Byte offset at which to begin writing at
create : bool
Create the file if it does not exist
truncate : bool
Truncate the file to size zero before writing
count : int
Maximum number of bytes to read from the source ``file``
"""
opts = {"offset": offset, "create": create, "truncate": truncate}
if count is not None:
opts["count"] = count
kwargs.setdefault("opts", {}).update(opts)
args = (path,)
body, headers = multipart.stream_files(file, chunk_size=self.chunk_size)
return self._client.request('/files/write', args, data=body, headers=headers, **kwargs)
示例15: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import RawIOBase [as 别名]
def _gettextwriter(out, encoding):
if out is None:
import sys
out = sys.stdout
if isinstance(out, io.RawIOBase):
buffer = io.BufferedIOBase(out)
# Keep the original file open when the TextIOWrapper is
# destroyed
buffer.close = lambda: None
else:
# This is to handle passed objects that aren't in the
# IOBase hierarchy, but just have a write method
buffer = io.BufferedIOBase()
buffer.writable = lambda: True
buffer.write = out.write
try:
# TextIOWrapper uses this methods to determine
# if BOM (for UTF-16, etc) should be added
buffer.seekable = out.seekable
buffer.tell = out.tell
except AttributeError:
pass
# wrap a binary writer with TextIOWrapper
class UnbufferedTextIOWrapper(io.TextIOWrapper):
def write(self, s):
super(UnbufferedTextIOWrapper, self).write(s)
self.flush()
return UnbufferedTextIOWrapper(buffer, encoding=encoding,
errors='xmlcharrefreplace',
newline='\n')