本文整理汇总了Python中io.BufferedIOBase方法的典型用法代码示例。如果您正苦于以下问题:Python io.BufferedIOBase方法的具体用法?Python io.BufferedIOBase怎么用?Python io.BufferedIOBase使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io
的用法示例。
在下文中一共展示了io.BufferedIOBase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: chunked_hash
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [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
示例2: copy_byte_range
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def copy_byte_range(
infile: io.BufferedIOBase,
outfile: io.BufferedIOBase,
start: int = None,
stop: int = None,
bufsize: int = 16 * 1024,
):
"""Like shutil.copyfileobj, but only copy a range of the streams.
Both start and stop are inclusive.
"""
if start is not None:
infile.seek(start)
while True:
to_read = min(bufsize, stop + 1 - infile.tell() if stop else bufsize)
buf = infile.read(to_read)
if not buf:
break
outfile.write(buf)
示例3: readline
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def readline(self, size=-1):
"""Read a line of uncompressed bytes from the file.
The terminating newline (if present) is retained. If size is
non-negative, no more than size bytes will be read (in which
case the line may be incomplete). Returns b'' if already at EOF.
"""
if not isinstance(size, int):
if not hasattr(size, "__index__"):
raise TypeError("Integer argument expected")
size = size.__index__()
with self._lock:
self._check_can_read()
# Shortcut for the common case - the whole line is in the buffer.
if size < 0:
end = self._buffer.find(b"\n", self._buffer_offset) + 1
if end > 0:
line = self._buffer[self._buffer_offset : end]
self._buffer_offset = end
self._pos += len(line)
return line
return io.BufferedIOBase.readline(self, size)
示例4: readline
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def readline(self, size=-1):
"""Read a line of uncompressed bytes from the file.
The terminating newline (if present) is retained. If size is
non-negative, no more than size bytes will be read (in which
case the line may be incomplete). Returns b'' if already at EOF.
"""
self._check_can_read()
# Shortcut for the common case - the whole line is in the buffer.
if size < 0:
end = self._buffer.find(b"\n", self._buffer_offset) + 1
if end > 0:
line = self._buffer[self._buffer_offset : end]
self._buffer_offset = end
self._pos += len(line)
return line
return io.BufferedIOBase.readline(self, size)
示例5: tofile
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def tofile(self, dbfile):
"""Save the address set to a file
:param dbfile: an open file object where the set is saved (overwriting it)
:type dbfile: io.FileIO or file
"""
if dbfile.tell() % mmap.ALLOCATIONGRANULARITY != 0:
print("AddressSet: warning: if header position in file isn't a multiple of {}, it probably can't be loaded with fromfile()"
.format(mmap.ALLOCATIONGRANULARITY), file=sys.stderr)
if "b" not in dbfile.mode:
raise ValueError("must open file in binary mode")
# Windows Python 2 file objects can't handle writes >= 4GiB. Objects returned
# by io.open() work around this issue, see https://bugs.python.org/issue9611
if not isinstance(dbfile, io.BufferedIOBase) and self._table_bytes >= 1 << 32:
raise ValueError("must open file with io.open if size >= 4GiB")
dbfile.truncate(dbfile.tell() + self.HEADER_LEN + self._table_bytes)
dbfile.write(self._header())
dbfile.write(self._data)
示例6: _upload_media_py3
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def _upload_media_py3(self, media_type, media_file, extension=''):
if isinstance(media_file, io.IOBase) and hasattr(media_file, 'name'):
extension = media_file.name.split('.')[-1].lower()
if not is_allowed_extension(extension):
raise ValueError('Invalid file type.')
filename = media_file.name
elif isinstance(media_file, io.BytesIO):
extension = extension.lower()
if not is_allowed_extension(extension):
raise ValueError('Please provide \'extension\' parameters when the type of \'media_file\' is \'io.BytesIO\'.')
filename = 'temp.' + extension
else:
raise ValueError('Parameter media_file must be io.BufferedIOBase(open a file with \'rb\') or io.BytesIO object.')
return self.request.post(
url='https://api.weixin.qq.com/cgi-bin/media/upload',
params={
'type': media_type,
},
files={
'media': (filename, media_file, convert_ext_to_mime(extension))
}
)
示例7: _get_writable
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def _get_writable(stream_or_path, mode):
"""This method returns a tuple containing the stream and a flag to indicate
if the stream should be automatically closed.
The `stream_or_path` parameter is returned if it is an open writable stream.
Otherwise, it treats the `stream_or_path` parameter as a file path and
opens it with the given mode.
It is used by the svg and png methods to interpret the file parameter.
:type stream_or_path: str | io.BufferedIOBase
:type mode: str | unicode
:rtype: (io.BufferedIOBase, bool)
"""
is_stream = hasattr(stream_or_path, 'write')
if not is_stream:
# No stream provided, treat "stream_or_path" as path
stream_or_path = open(stream_or_path, mode)
return stream_or_path, not is_stream
示例8: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def __init__(self, fp, strict=True):
if strict:
valid = lambda f: (
isinstance(f, (io.BufferedIOBase, io.RawIOBase))
and f.seekable() and f.readable())
else:
valid = lambda f: all([hasattr(f, 'readinto'), hasattr(f, 'seek')])
if not valid(fp):
raise TypeError("expected file-like, seekable, readable object")
# store file size, assuming it won't change
self._size = fp.seek(0, os.SEEK_END)
if self._size is None:
# handle python2, when `fp.seek()` is `file.seek()`
# note: not a thread-safe way
self._size = fp.tell()
self._fp = fp
# multiple threads will be accessing the underlying file
self._lock = threading.RLock()
示例9: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [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)
示例10: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [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')
示例11: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def __init__(self, filename, mode="rb", compresslevel=9):
# This lock must be recursive, so that BufferedIOBase's
# readline(), readlines() and writelines() don't deadlock.
self._lock = RLock()
self._fp = None
self._closefp = False
self._mode = _MODE_CLOSED
self._pos = 0
self._size = -1
if not isinstance(compresslevel, int) or not (1 <= compresslevel <= 9):
raise ValueError("'compresslevel' must be an integer "
"between 1 and 9. You provided 'compresslevel={}'"
.format(compresslevel))
if mode == "rb":
mode_code = _MODE_READ
self._decompressor = zlib.decompressobj(self.wbits)
self._buffer = b""
self._buffer_offset = 0
elif mode == "wb":
mode_code = _MODE_WRITE
self._compressor = zlib.compressobj(compresslevel,
zlib.DEFLATED,
self.wbits,
zlib.DEF_MEM_LEVEL,
0)
else:
raise ValueError("Invalid mode: %r" % (mode,))
if isinstance(filename, _basestring):
self._fp = io.open(filename, mode)
self._closefp = True
self._mode = mode_code
elif hasattr(filename, "read") or hasattr(filename, "write"):
self._fp = filename
self._mode = mode_code
else:
raise TypeError("filename must be a str or bytes object, "
"or a file")
示例12: readinto
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def readinto(self, b):
"""Read up to len(b) bytes into b.
Returns the number of bytes read (0 for EOF).
"""
with self._lock:
return io.BufferedIOBase.readinto(self, b)
示例13: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [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')
示例14: _is_this_type
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [as 别名]
def _is_this_type(buffer: io.BufferedIOBase) -> bool:
with BufferReader(buffer) as buffer_reader:
# Per the TIFF-6 spec
# (https://www.itu.int/itudoc/itu-t/com16/tiff-fx/docs/tiff6.pdf),
# 'II' is little-endian (Intel format)
# 'MM' is big-endian (Motorola format)
if buffer_reader.endianness not in [
buffer_reader.INTEL_ENDIAN,
buffer_reader.MOTOROLA_ENDIAN,
]:
return False
magic = buffer_reader.read_uint16()
# Per TIFF-6, magic is 42.
if magic == 42:
ifd_offset = buffer_reader.read_uint32()
if ifd_offset == 0:
return False
# Per BigTIFF
# (https://www.awaresystems.be/imaging/tiff/bigtiff.html), magic is 43.
if magic == 43:
# Alex magic here...
if buffer_reader.read_uint16() != 8:
return False
if buffer_reader.read_uint16() != 0:
return False
ifd_offset = buffer_reader.read_uint64()
if ifd_offset == 0:
return False
return True
示例15: _gettextwriter
# 需要导入模块: import io [as 别名]
# 或者: from io import BufferedIOBase [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
if isinstance(out, (codecs.StreamWriter, codecs.StreamReaderWriter)):
# use a codecs stream 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)