本文整理匯總了Python中_io.TextIOWrapper方法的典型用法代碼示例。如果您正苦於以下問題:Python _io.TextIOWrapper方法的具體用法?Python _io.TextIOWrapper怎麽用?Python _io.TextIOWrapper使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類_io
的用法示例。
在下文中一共展示了_io.TextIOWrapper方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_logger
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def get_logger(name: str, level: int = logging.INFO, stream: TextIOWrapper = sys.stdout, file: str = None,
formatter: str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s') -> Logger:
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
logger.propagate = False
formatter = logging.Formatter(formatter)
stream_handler = logging.StreamHandler(stream)
stream_handler.setLevel(level)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
if file is not None:
file_handler = logging.FileHandler(file)
file_handler.setLevel(level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
示例2: mock_open
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def mock_open(mock=None, read_data=''):
"""
A helper function to create a mock to replace the use of `open`. It works
for `open` called directly or used as a context manager.
The `mock` argument is the mock object to configure. If `None` (the
default) then a `MagicMock` will be created for you, with the API limited
to methods or attributes available on standard file handles.
`read_data` is a string for the `read` method of the file handle to return.
This is an empty string by default.
"""
global file_spec
if file_spec is None:
import _io
file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
if mock is None:
mock = MagicMock(name='open', spec=open)
handle = MagicMock(spec=file_spec)
handle.write.return_value = None
handle.__enter__.return_value = handle
handle.read.return_value = read_data
mock.return_value = handle
return mock
示例3: unicode_csv_reader
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def unicode_csv_reader(unicode_csv_data: TextIOWrapper, **kwargs: Any) -> Any:
r"""Since the standard csv library does not handle unicode in Python 2, we need a wrapper.
Borrowed and slightly modified from the Python docs:
https://docs.python.org/2/library/csv.html#csv-examples
Args:
unicode_csv_data (TextIOWrapper): unicode csv data (see example below)
Examples:
>>> from torchaudio.datasets.utils import unicode_csv_reader
>>> import io
>>> with io.open(data_path, encoding="utf8") as f:
>>> reader = unicode_csv_reader(f)
"""
# Fix field larger than field limit error
maxInt = sys.maxsize
while True:
# decrease the maxInt value by factor 10
# as long as the OverflowError occurs.
try:
csv.field_size_limit(maxInt)
break
except OverflowError:
maxInt = int(maxInt / 10)
csv.field_size_limit(maxInt)
for line in csv.reader(unicode_csv_data, **kwargs):
yield line
示例4: mock_open
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def mock_open(mock=None, read_data=''):
"""
A helper function to create a mock to replace the use of `open`. It works
for `open` called directly or used as a context manager.
The `mock` argument is the mock object to configure. If `None` (the
default) then a `MagicMock` will be created for you, with the API limited
to methods or attributes available on standard file handles.
`read_data` is a string for the `read` method of the file handle to return.
This is an empty string by default.
"""
global file_spec
if file_spec is None:
# set on first use
if inPy3k:
import _io
file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
else:
file_spec = file
if mock is None:
mock = MagicMock(name='open', spec=open)
handle = MagicMock(spec=file_spec)
handle.write.return_value = None
handle.__enter__.return_value = handle
handle.read.return_value = read_data
mock.return_value = handle
return mock
示例5: __init__
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def __init__(self, terminal: _io.TextIOWrapper, logfile: pathlib.Path):
""" initialize BiLogger
Args:
terminal (_io.TextIOWrapper): original terminal IO wrapper
logfile (pathlib.Path): target log file path object
"""
self.terminal = terminal
self.log = logfile.open(mode='a+')
示例6: test_fake_stdout
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def test_fake_stdout(self):
""" test that the fake stdout is from the same type as python sys.stdout (in cmdline context) """
self.assertEqual(type(self.fake_stdout), _io.TextIOWrapper)
示例7: mock_open
# 需要導入模塊: import _io [as 別名]
# 或者: from _io import TextIOWrapper [as 別名]
def mock_open(mock=None, read_data=''):
"""
A helper function to create a mock to replace the use of `open`. It works
for `open` called directly or used as a context manager.
The `mock` argument is the mock object to configure. If `None` (the
default) then a `MagicMock` will be created for you, with the API limited
to methods or attributes available on standard file handles.
`read_data` is a string for the `read` methoddline`, and `readlines` of the
file handle to return. This is an empty string by default.
"""
def _readlines_side_effect(*args, **kwargs):
if handle.readlines.return_value is not None:
return handle.readlines.return_value
return list(_data)
def _read_side_effect(*args, **kwargs):
if handle.read.return_value is not None:
return handle.read.return_value
return ''.join(_data)
def _readline_side_effect():
if handle.readline.return_value is not None:
while True:
yield handle.readline.return_value
for line in _data:
yield line
global file_spec
if file_spec is None:
import _io
file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
if mock is None:
mock = MagicMock(name='open', spec=open)
handle = MagicMock(spec=file_spec)
handle.__enter__.return_value = handle
_data = _iterate_read_data(read_data)
handle.write.return_value = None
handle.read.return_value = None
handle.readline.return_value = None
handle.readlines.return_value = None
handle.read.side_effect = _read_side_effect
handle.readline.side_effect = _readline_side_effect()
handle.readlines.side_effect = _readlines_side_effect
mock.return_value = handle
return mock