当前位置: 首页>>代码示例>>Python>>正文


Python Tee.close方法代码示例

本文整理汇总了Python中IPython.utils.io.Tee.close方法的典型用法代码示例。如果您正苦于以下问题:Python Tee.close方法的具体用法?Python Tee.close怎么用?Python Tee.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPython.utils.io.Tee的用法示例。


在下文中一共展示了Tee.close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: tchan

# 需要导入模块: from IPython.utils.io import Tee [as 别名]
# 或者: from IPython.utils.io.Tee import close [as 别名]
    def tchan(self, channel, check='close'):
        trap = StringIO()
        chan = StringIO()
        text = 'Hello'
        
        std_ori = getattr(sys, channel)
        setattr(sys, channel, trap)

        tee = Tee(chan, channel=channel)
        print(text, end='', file=chan)
        setattr(sys, channel, std_ori)
        trap_val = trap.getvalue()
        nt.assert_equals(chan.getvalue(), text)
        if check=='close':
            tee.close()
        else:
            del tee
开发者ID:dlsun,项目名称:ipython,代码行数:19,代码来源:test_io.py

示例2: AssertPrints

# 需要导入模块: from IPython.utils.io import Tee [as 别名]
# 或者: from IPython.utils.io.Tee import close [as 别名]
class AssertPrints(object):

    """Context manager for testing that code prints certain text.

    Examples
    --------
    >>> with AssertPrints("abc", suppress=False):
    ...     print("abcd")
    ...     print("def")
    ... 
    abcd
    def
    """

    def __init__(self, s, channel='stdout', suppress=True):
        self.s = s
        if isinstance(self.s, (py3compat.string_types, _re_type)):
            self.s = [self.s]
        self.channel = channel
        self.suppress = suppress

    def __enter__(self):
        self.orig_stream = getattr(sys, self.channel)
        self.buffer = MyStringIO()
        self.tee = Tee(self.buffer, channel=self.channel)
        setattr(sys, self.channel, self.buffer if self.suppress else self.tee)

    def __exit__(self, etype, value, traceback):
        try:
            if value is not None:
                # If an error was raised, don't check anything else
                return False
            self.tee.flush()
            setattr(sys, self.channel, self.orig_stream)
            printed = self.buffer.getvalue()
            for s in self.s:
                if isinstance(s, _re_type):
                    assert s.search(printed), notprinted_msg.format(
                        s.pattern, self.channel, printed)
                else:
                    assert s in printed, notprinted_msg.format(
                        s, self.channel, printed)
            return False
        finally:
            self.tee.close()
开发者ID:mattvonrocketstein,项目名称:smash,代码行数:47,代码来源:tools.py


注:本文中的IPython.utils.io.Tee.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。