项目作者: FFY00

项目描述 :
IO buffer multiplexer
高级语言: Python
项目地址: git://github.com/FFY00/python-iomux.git
创建时间: 2021-06-01T15:55:10Z
项目社区:https://github.com/FFY00/python-iomux

开源协议:MIT License

下载


iomux

checks
tests
codecov
Documentation Status

IO buffer multiplexer.

Currently, the only use-case that is supported is if the IO object user does not need to read from it.
IO objects will only be able to read the last data segment. With time, we want to grow this package to
support use-cases where the IO object user also wants to perform read operations. Right now, read
operations only work as expected in the multiplexer object itself.
As-is, this is useful for working with output streams, but not normal file-descriptors.

  1. import sys
  2. from contextlib import redirect_stdout, redirect_stderr
  3. import iomux
  4. capture = iomux.StringMux()
  5. with redirect_stdout(capture.out), redirect_stderr(capture.err):
  6. print('aaa')
  7. print('bbb', file=sys.stderr)
  8. print('aaa')
  9. print('bbb', file=sys.stderr)
  10. assert capture.getvalue() == 'aaa\nbbb\naaa\nbbb\n'
  11. assert capture.getvalue('out') == 'aaa\naaa\n'
  12. assert capture.getvalue('err') == 'bbb\nbbb\n'
  13. assert list(capture.values()) == [
  14. ('out', 'aaa\n'),
  15. ('err', 'bbb\n'),
  16. ('out', 'aaa\n'),
  17. ('err', 'bbb\n'),
  18. ]