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


Python NativeStringIO.truncate方法代码示例

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


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

示例1: PythonLoggingObserverTestCase

# 需要导入模块: from twisted.python.compat import NativeStringIO [as 别名]
# 或者: from twisted.python.compat.NativeStringIO import truncate [as 别名]
class PythonLoggingObserverTestCase(unittest.SynchronousTestCase):
    """
    Test the bridge with python logging module.
    """
    def setUp(self):
        self.out = StringIO()

        rootLogger = logging.getLogger("")
        self.originalLevel = rootLogger.getEffectiveLevel()
        rootLogger.setLevel(logging.DEBUG)
        self.hdlr = logging.StreamHandler(self.out)
        fmt = logging.Formatter(logging.BASIC_FORMAT)
        self.hdlr.setFormatter(fmt)
        rootLogger.addHandler(self.hdlr)

        self.lp = log.LogPublisher()
        self.obs = log.PythonLoggingObserver()
        self.lp.addObserver(self.obs.emit)

    def tearDown(self):
        rootLogger = logging.getLogger("")
        rootLogger.removeHandler(self.hdlr)
        rootLogger.setLevel(self.originalLevel)
        logging.shutdown()

    def test_singleString(self):
        """
        Test simple output, and default log level.
        """
        self.lp.msg("Hello, world.")
        self.assertIn("Hello, world.", self.out.getvalue())
        self.assertIn("INFO", self.out.getvalue())

    def test_errorString(self):
        """
        Test error output.
        """
        self.lp.msg(failure=failure.Failure(ValueError("That is bad.")), isError=True)
        self.assertIn("ERROR", self.out.getvalue())

    def test_formatString(self):
        """
        Test logging with a format.
        """
        self.lp.msg(format="%(bar)s oo %(foo)s", bar="Hello", foo="world")
        self.assertIn("Hello oo world", self.out.getvalue())

    def test_customLevel(self):
        """
        Test the logLevel keyword for customizing level used.
        """
        self.lp.msg("Spam egg.", logLevel=logging.DEBUG)
        self.assertIn("Spam egg.", self.out.getvalue())
        self.assertIn("DEBUG", self.out.getvalue())
        self.out.seek(0, 0)
        self.out.truncate()
        self.lp.msg("Foo bar.", logLevel=logging.WARNING)
        self.assertIn("Foo bar.", self.out.getvalue())
        self.assertIn("WARNING", self.out.getvalue())

    def test_strangeEventDict(self):
        """
        Verify that an event dictionary which is not an error and has an empty
        message isn't recorded.
        """
        self.lp.msg(message='', isError=False)
        self.assertEqual(self.out.getvalue(), '')
开发者ID:0004c,项目名称:VTK,代码行数:69,代码来源:test_log.py


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