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


Python io.Tee类代码示例

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


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

示例1: AssertPrints

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
        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):
        self.tee.flush()
        setattr(sys, self.channel, self.orig_stream)
        printed = self.buffer.getvalue()
        assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
        return False
开发者ID:BlackEarth,项目名称:portable-python-win32,代码行数:29,代码来源:tools.py

示例2: AssertPrints

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):
            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):
        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:
            assert s in printed, notprinted_msg.format(s, self.channel, printed)
        return False
开发者ID:ArslanRafique,项目名称:dist-packages,代码行数:35,代码来源:tools.py

示例3: tchan

    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,代码行数:17,代码来源:test_io.py

示例4: __enter__

 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)
开发者ID:Carreau,项目名称:ipython,代码行数:5,代码来源:tools.py


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