本文整理汇总了Python中io.IOBase方法的典型用法代码示例。如果您正苦于以下问题:Python io.IOBase方法的具体用法?Python io.IOBase怎么用?Python io.IOBase使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io
的用法示例。
在下文中一共展示了io.IOBase方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: prepare_iter
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def prepare_iter(value):
"""
Ensure response body is iterable and resolves to False when empty.
"""
if isinstance(value, text_or_bytes):
# strings get wrapped in a list because iterating over a single
# item list is much faster than iterating over every character
# in a long string.
if value:
value = [value]
else:
# [''] doesn't evaluate to False, so replace it with [].
value = []
# Don't use isinstance here; io.IOBase which has an ABC takes
# 1000 times as long as, say, isinstance(value, str)
elif hasattr(value, 'read'):
value = file_generator(value)
elif value is None:
value = []
return value
# GZIP
示例2: files_upload_ex
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def files_upload_ex(
self,
file: Union[str, IOBase] = None,
content: str = None,
channels: [str] = None,
title: str = None,
initial_comment: str = None,
file_type: str = None,
):
args = {}
if channels:
args["channels"] = ",".join(channels)
if title:
args["title"] = title
if initial_comment:
args["initial_comment"] = initial_comment
if file_type:
args["filetype"] = file_type
return await self.files_upload(file=file, content=content, **args)
示例3: test_title_changed_to_include_image_name_when_title_given
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def test_title_changed_to_include_image_name_when_title_given(self,
mock_open, mock_add_header, mock_add_img_subj, mock_index,
mock_exists, mock_writable):
mock_file = MagicMock(spec=io.IOBase)
mock_open.return_value.__enter__.return_value = mock_file
mock_exists.return_value = False
mock_writable.return_value = True
qc_config = self.get_config_stub()
html.write_index_pages(self.qc_dir, qc_config, self.subject,
title='QC mode for image {}')
for image in qc_config.images:
name = image.name
found = False
for call in mock_index.call_args_list:
if name in call[1]['title']:
found = True
assert found
示例4: _get_extension
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def _get_extension(file):
"""
Gets the extension for the given file, which can be either a
str or an ``open()``'ed file (which has a ``.name`` attribute).
"""
if isinstance(file, str):
return os.path.splitext(file)[-1]
elif isinstance(file, pathlib.Path):
return file.suffix
elif isinstance(file, bytes):
kind = imghdr.what(io.BytesIO(file))
return ('.' + kind) if kind else ''
elif isinstance(file, io.IOBase) and not isinstance(file, io.TextIOBase) and file.seekable():
kind = imghdr.what(file)
return ('.' + kind) if kind is not None else ''
elif getattr(file, 'name', None):
# Note: ``file.name`` works for :tl:`InputFile` and some `IOBase`
return _get_extension(file.name)
else:
# Maybe it's a Telegram media
return get_extension(file)
示例5: temporary_offset
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def temporary_offset(self, offset=None, whence=0):
"""Context manager for temporarily seeking to another file position.
To be used as part of a ``with`` statement::
with fh_raw.temporary_offset() [as fh_raw]:
with-block
On exiting the ``with-block``, the file pointer is moved back to its
original position. As a convenience, one can pass on the offset
to seek to when entering the context manager. Parameters are as
for :meth:`io.IOBase.seek`.
"""
oldpos = self.tell()
try:
if offset is not None:
self.seek(offset, whence)
yield self
finally:
self.seek(oldpos)
示例6: __getstate__
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def __getstate__(self):
if self.writable():
raise TypeError('cannot pickle file opened for writing')
state = self.__dict__.copy()
# IOBase instances cannot be pickled, but we can just reopen them
# when we are unpickled. Anything else may have internal state that
# needs preserving (e.g., SequentialFile), so we will assume
# it takes care of this itself.
if isinstance(self.fh_raw, io.IOBase):
fh = state.pop('fh_raw')
state['fh_info'] = {
'offset': 'closed' if fh.closed else fh.tell(),
'filename': fh.name,
'mode': fh.mode}
return state
示例7: __getstate__
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def __getstate__(self):
state = super().__getstate__()
fh_raw = state.pop('fh_raw')
if self.header0.mode == 'rawdump':
fh_raw = [[fh_raw]]
fh_info = []
for fh_pair in fh_raw:
pair_info = []
for fh in fh_pair:
if isinstance(fh, io.IOBase):
pair_info.append({
'offset': 'closed' if fh.closed else fh.tell(),
'filename': fh.name,
'mode': fh.mode})
else:
pair_info.append(fh)
fh_info.append(pair_info)
state['fh_info'] = fh_info
return state
示例8: _body2str
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def _body2str(self, body):
try:
from aiohttp.payload import Payload
except ImportError:
Payload = None
if Payload is not None and isinstance(body, Payload):
body = body._value
if isinstance(body, io.IOBase):
return _FILE
if not isinstance(body, str):
try:
body = str(body, "utf8")
except UnicodeDecodeError:
return _UNREADABLE
return body
示例9: __init__
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def __init__(self, filepath_or_object, mode="wb", compresslevel=6):
"""Initialize the class."""
if isinstance(filepath_or_object, io.IOBase):
handle = filepath_or_object
else:
handle = open(filepath_or_object, mode=mode)
"""
if fileobj:
assert filename is None
handle = fileobj
else:
if "w" not in mode.lower() and "a" not in mode.lower():
raise ValueError("Must use write or append mode, not %r" % mode)
if "a" in mode.lower():
handle = open(filename, "ab")
else:
handle = open(filename, "wb")
"""
self._text = "b" not in mode.lower()
self._handle = handle
self._buffer = b""
self.compresslevel = compresslevel
示例10: cmd_stmt_execute
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def cmd_stmt_execute(self, statement_id, data=(), parameters=(), flags=0):
"""Execute a prepared MySQL statement"""
parameters = list(parameters)
long_data_used = {}
if data:
for param_id, _ in enumerate(parameters):
if isinstance(data[param_id], IOBase):
binary = True
try:
binary = 'b' not in data[param_id].mode
except AttributeError:
pass
self.cmd_stmt_send_long_data(statement_id, param_id,
data[param_id])
long_data_used[param_id] = (binary,)
execute_packet = self._protocol.make_stmt_execute(
statement_id, data, tuple(parameters), flags,
long_data_used, self.charset)
packet = self._send_cmd(ServerCmd.STMT_EXECUTE, packet=execute_packet)
result = self._handle_binary_result(packet)
return result
示例11: close
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def close(self):
"""Delete the IO object and close the input file and the output file"""
if self.__closed:
# avoid double close
return
deleted = False
try:
# on posix, one can remove a file while it's opend by a process
# the file then will be not visable to others, but process still have the file descriptor
# it is recommand to remove temp file before close it on posix to avoid race
# on nt, it will just fail and raise OSError so that after closing remove it again
self.__del_files()
deleted = True
except OSError:
pass
if isinstance(self.input_file, IOBase):
self.input_file.close()
if isinstance(self.output_file, IOBase):
self.output_file.close()
if not deleted:
self.__del_files()
self.__closed = True
示例12: copy_sparse_data
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def copy_sparse_data(input_stream, output_stream, sparse_map):
'''Copy data blocks from input to output according to sparse_map
:param input_stream: io.IOBase input instance
:param output_stream: io.IOBase output instance
:param sparse_map: iterable of (offset, size)
'''
buf = bytearray(BUF_SIZE)
for chunk in sparse_map:
input_stream.seek(chunk[0])
left = chunk[1]
while left:
if left > BUF_SIZE:
read = input_stream.readinto(buf)
output_stream.write(buf[:read])
else:
buf_trailer = input_stream.read(left)
read = len(buf_trailer)
output_stream.write(buf_trailer)
left -= read
if not read:
raise Exception('premature EOF')
示例13: load
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def load(self, content):
"""Loads Command Definitions from the given YAML content into
into this Command Dictionary. Content may be either a
filename containing YAML content or a YAML string.
Load has no effect if this Command Dictionary was already
instantiated with a filename or YAML content.
"""
if self.filename is None:
if os.path.isfile(content):
self.filename = content
stream = open(self.filename, 'rb')
else:
stream = content
cmds = yaml.load(stream, Loader=yaml.Loader)
cmds = handle_includes(cmds)
for cmd in cmds:
self.add(cmd)
if isinstance(stream, IOBase):
stream.close()
示例14: load
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def load(self, content):
"""Loads Limit Definitions from the given YAML content into this
Telemetry Dictionary. Content may be either a filename
containing YAML content or a YAML string.
Load has no effect if this Limits Dictionary was already
instantiated with a filename or YAML content.
"""
if self.filename is None:
if os.path.isfile(content):
self.filename = content
stream = open(self.filename, 'rb')
else:
stream = content
limits = yaml.load(stream, Loader=yaml.Loader)
for lmt in limits:
self.add(lmt)
if isinstance(stream, IOBase):
stream.close()
示例15: loadYAML
# 需要导入模块: import io [as 别名]
# 或者: from io import IOBase [as 别名]
def loadYAML (filename=None, data=None):
"""Loads either the given YAML configuration file or YAML data.
Returns None if there was an error reading from the configuration
file and logs an error message via ait.core.log.error().
"""
config = None
try:
if filename:
data = open(filename, 'rt')
config = yaml.load(data, Loader=yaml.Loader)
if isinstance(data, IOBase):
data.close()
except IOError as e:
msg = 'Could not read AIT configuration file "%s": %s'
log.error(msg, filename, str(e))
return config