Python _io 模块,StringIO() 实例源码

我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用_io.StringIO()

项目:hwtLib    作者:Nic30    | 项目源码 | 文件源码
def test_pprintAgents(self):
        u = ExampleWithArrayAxiLite()
        pointerRe = re.compile("0x[a-f0-9]*")
        self.prepareUnit(u)
        self.doSim(1)

        o = StringIO()
        pprintAgents(u.clk, file=o)
        self.assertEmpty(pointerRe.sub(o.getvalue(), ""), pointerRe.sub(clk_ag_str, ""))

        o = StringIO()
        pprintAgents(u.axi, file=o)
        self.assertEmpty(pointerRe.sub(o.getvalue(), ""), pointerRe.sub(axi_ag_str, ""))

        o = StringIO()
        pprintAgents(u, file=o)
        self.assertEmpty(pointerRe.sub(o.getvalue(), ""), pointerRe.sub(u_ag_str, ""))
项目:datatest    作者:shawnbrown    | 项目源码 | 文件源码
def test_explain2(self):
        query = DataQuery(['label1'])

        expected = """
            Data Source:
              <none given> (assuming DataSource object)
            Execution Plan:
              getattr, (<RESULT>, '_select'), {}
              <RESULT>, (['label1']), {}
        """
        expected = textwrap.dedent(expected).strip()

        # Defaults to stdout (redirected to StringIO for testing).
        string_io = io.StringIO()
        returned_value = query._explain(file=string_io)
        self.assertIsNone(returned_value)

        printed_value = string_io.getvalue().strip()
        self.assertEqual(printed_value, expected)

        # Get result as string.
        returned_value = query._explain(file=None)
        self.assertEqual(returned_value, expected)
项目:Sublime-uroboroSQL-formatter    作者:future-architect    | 项目源码 | 文件源码
def get_tokens(self, text, unfiltered=False):
        """
        Return an iterable of (tokentype, value) pairs generated from
        `text`. If `unfiltered` is set to `True`, the filtering mechanism
        is bypassed even if filters are defined.

        Also preprocess the text, i.e. expand tabs and strip it if
        wanted and applies registered filters.
        """
        if isinstance(text, str):
            if self.stripall:
                text = text.strip()
            elif self.stripnl:
                text = text.strip('\n')

            if sys.version_info[0] < 3 and isinstance(text, str):
                # for jython
                import cStringIO
                text = cStringIO.StringIO(text)
                self.encoding = 'utf-8'
            else:
                # for jython move import section
                from _io import StringIO
                text = StringIO(text)

        def streamer():
            for i, t, v in self.get_tokens_unprocessed(text):
                # for jython encode
                if sys.version_info[0] < 3 and isinstance(v, unicode):
                    v = v.encode('utf-8')
                yield t, v
        stream = streamer()
        if not unfiltered:
            stream = apply_filters(stream, self.filters, self)
        return stream
项目:hwtLib    作者:Nic30    | 项目源码 | 文件源码
def test_pprintInterface(self):
        u = ExampleWithArrayAxiLite()
        o = StringIO()
        self.prepareUnit(u)
        pprintInterface(u.clk, file=o)
        self.assertEqual(o.getvalue(), "'clk'\n")

        o = StringIO()
        pprintInterface(u.axi, file=o)
        self.assertEqual(o.getvalue(), axi_str)
项目:uroboroSQL-formatter    作者:future-architect    | 项目源码 | 文件源码
def get_tokens(self, text, unfiltered=False):
        """
        Return an iterable of (tokentype, value) pairs generated from
        `text`. If `unfiltered` is set to `True`, the filtering mechanism
        is bypassed even if filters are defined.

        Also preprocess the text, i.e. expand tabs and strip it if
        wanted and applies registered filters.
        """
        if isinstance(text, str):
            if self.stripall:
                text = text.strip()
            elif self.stripnl:
                text = text.strip('\n')

            if sys.version_info[0] < 3 and isinstance(text, str):
                # for jython
                import cStringIO
                text = cStringIO.StringIO(text)
                self.encoding = 'utf-8'
            else:
                # for jython move import section
                from _io import StringIO
                text = StringIO(text)

        def streamer():
            for i, t, v in self.get_tokens_unprocessed(text):
                # for jython encode
                if sys.version_info[0] < 3 and isinstance(v, unicode):
                    v = v.encode('utf-8')
                yield t, v
        stream = streamer()
        if not unfiltered:
            stream = apply_filters(stream, self.filters, self)
        return stream
项目:inmanta    作者:inmanta    | 项目源码 | 文件源码
def setUp(self):
        self.stream = StringIO()
        self.handler = logging.StreamHandler(self.stream)
        self.log = logging.getLogger(module.__name__)

        for handler in self.log.handlers:
            self.log.removeHandler(handler)

        self.log.addHandler(self.handler)

        Project._project = None
项目:inmanta    作者:inmanta    | 项目源码 | 文件源码
def setUp(self):
        self.stream = StringIO()
        self.handler = logging.StreamHandler(self.stream)
        self.log = logging.getLogger(module.__name__)

        for handler in self.log.handlers:
            self.log.removeHandler(handler)

        self.log.addHandler(self.handler)