Python six.moves.StringIO() Examples

The following are 30 code examples of six.moves.StringIO(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves , or try the search function .
Example #1
Source File: test_fastaiter.py    From biocommons.seqrepo with Apache License 2.0 6 votes vote down vote up
def test_multiple():
    data = StringIO(">seq1\nACGT\n>seq2\nTGCA\n\n>seq3\nTTTT")

    iterator = FastaIter(data)

    header, seq = six.next(iterator)
    assert header == "seq1"
    assert seq == "ACGT"

    header, seq = six.next(iterator)
    assert header == "seq2"
    assert seq == "TGCA"

    header, seq = six.next(iterator)
    assert header == "seq3"
    assert seq == "TTTT"

    # should be empty now
    with pytest.raises(StopIteration):
        six.next(iterator) 
Example #2
Source File: log.py    From open_dnsdb with Apache License 2.0 6 votes vote down vote up
def formatException(self, exc_info, record=None):
        """Format exception output with CONF.logging_exception_prefix."""
        if not record:
            return logging.Formatter.formatException(self, exc_info)

        stringbuffer = moves.StringIO()
        traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
                                  None, stringbuffer)
        lines = stringbuffer.getvalue().split('\n')
        stringbuffer.close()

        if CONF.logging_exception_prefix.find('%(asctime)') != -1:
            record.asctime = self.formatTime(record, self.datefmt)

        formatted_lines = []
        for line in lines:
            pl = CONF.logging_exception_prefix % record.__dict__
            fl = '%s%s' % (pl, line)
            formatted_lines.append(fl)
        return '\n'.join(formatted_lines) 
Example #3
Source File: utils.py    From nbodykit with GNU General Public License v3.0 6 votes vote down vote up
def captured_output(comm, root=0):
    """
    Re-direct stdout and stderr to null for every rank but ``root``
    """
    # keep output on root
    if root is not None and comm.rank == root:
        yield sys.stdout, sys.stderr
    else:
        from six.moves import StringIO
        from nbodykit.extern.wurlitzer import sys_pipes

        # redirect stdout and stderr
        old_stdout, sys.stdout = sys.stdout, StringIO()
        old_stderr, sys.stderr = sys.stderr, StringIO()
        try:
            with sys_pipes() as (out, err):
                yield out, err
        finally:
            sys.stdout = old_stdout
            sys.stderr = old_stderr 
Example #4
Source File: test_paramiko_ssh.py    From st2 with Apache License 2.0 6 votes vote down vote up
def test_key_material_argument(self):
        path = os.path.join(get_resources_base_path(),
                            'ssh', 'dummy_rsa')

        with open(path, 'r') as fp:
            private_key = fp.read()

        conn_params = {'hostname': 'dummy.host.org',
                       'username': 'ubuntu',
                       'key_material': private_key}
        mock = ParamikoSSHClient(**conn_params)
        mock.connect()

        pkey = paramiko.RSAKey.from_private_key(StringIO(private_key))
        expected_conn = {'username': 'ubuntu',
                         'allow_agent': False,
                         'hostname': 'dummy.host.org',
                         'look_for_keys': False,
                         'pkey': pkey,
                         'timeout': 60,
                         'port': 22}
        mock.client.connect.assert_called_once_with(**expected_conn) 
Example #5
Source File: debugmode.py    From attention-lvcsr with MIT License 6 votes vote down vote up
def __str__(self):
        sio = StringIO()
        print("  node:", self.node, file=sio)
        print("  node.inputs:", [(str(i), id(i))
                                 for i in self.node.inputs], file=sio)
        print("  node.outputs:", [(str(i), id(i))
                                  for i in self.node.outputs], file=sio)
        print("  view_map:", getattr(self.node.op, 'view_map', {}), file=sio)
        print("  destroy_map:", getattr(self.node.op,
                                        'destroy_map', {}), file=sio)
        print("  aliased output:", self.output_idx, file=sio)
        print("  aliased output storage:", self.out_storage, file=sio)
        if self.in_alias_idx:
            print("  aliased to inputs:", self.in_alias_idx, file=sio)
        if self.out_alias_idx:
            print("  aliased to outputs:", self.out_alias_idx, file=sio)
        return sio.getvalue() 
Example #6
Source File: test_reverse_words.py    From blocks-examples with MIT License 6 votes vote down vote up
def test_reverse_words():
    old_limit = config.recursion_limit
    config.recursion_limit = 100000
    with tempfile.NamedTemporaryFile() as f_save,\
            tempfile.NamedTemporaryFile() as f_data:
        with open(f_data.name, 'wt') as data:
            for i in range(10):
                print("A line.", file=data)
        main("train", f_save.name, 1, [f_data.name])

        real_stdin = sys.stdin
        sys.stdin = StringIO('abc\n10\n')
        main("beam_search", f_save.name, 10)
        sys.stdin = real_stdin


    config.recursion_limit = old_limit 
Example #7
Source File: py.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def preview_sql(self, url, step, **args):
        """Mocks SQLAlchemy Engine to store all executed calls in a string
        and runs :meth:`PythonScript.run <migrate.versioning.script.py.PythonScript.run>`

        :returns: SQL file
        """
        buf = StringIO()
        args['engine_arg_strategy'] = 'mock'
        args['engine_arg_executor'] = lambda s, p = '': buf.write(str(s) + p)

        @with_engine
        def go(url, step, **kw):
            engine = kw.pop('engine')
            self.run(engine, step)
            return buf.getvalue()

        return go(url, step, **args) 
Example #8
Source File: debugmode.py    From D-VAE with MIT License 6 votes vote down vote up
def __str__(self):
        sio = StringIO()
        print("  node:", self.node, file=sio)
        print("  node.inputs:", [(str(i), id(i))
                                 for i in self.node.inputs], file=sio)
        print("  node.outputs:", [(str(i), id(i))
                                  for i in self.node.outputs], file=sio)
        print("  view_map:", getattr(self.node.op, 'view_map', {}), file=sio)
        print("  destroy_map:", getattr(self.node.op,
                                        'destroy_map', {}), file=sio)
        print("  aliased output:", self.output_idx, file=sio)
        print("  aliased output storage:", self.out_storage, file=sio)
        if self.in_alias_idx:
            print("  aliased to inputs:", self.in_alias_idx, file=sio)
        if self.out_alias_idx:
            print("  aliased to outputs:", self.out_alias_idx, file=sio)
        return sio.getvalue() 
Example #9
Source File: log.py    From avos with Apache License 2.0 6 votes vote down vote up
def formatException(self, exc_info, record=None):
        """Format exception output with CONF.logging_exception_prefix."""
        if not record:
            return logging.Formatter.formatException(self, exc_info)

        stringbuffer = moves.StringIO()
        traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
                                  None, stringbuffer)
        lines = stringbuffer.getvalue().split('\n')
        stringbuffer.close()

        if CONF.logging_exception_prefix.find('%(asctime)') != -1:
            record.asctime = self.formatTime(record, self.datefmt)

        formatted_lines = []
        for line in lines:
            pl = CONF.logging_exception_prefix % record.__dict__
            fl = '%s%s' % (pl, line)
            formatted_lines.append(fl)
        return '\n'.join(formatted_lines) 
Example #10
Source File: test_acceptance.py    From aws_role_credentials with ISC License 6 votes vote down vote up
def test_credentials_are_generated_from_saml(self, mock_sts):
        mock_conn = MagicMock()
        mock_conn.assume_role_with_saml.return_value = Struct({'credentials':
                                                               Struct({'expiration': 'SAML_TOKEN_EXPIRATION',
                                                                       'access_key': 'SAML_ACCESS_KEY',
                                                                       'secret_key': 'SAML_SECRET_KEY',
                                                                       'session_token': 'SAML_TOKEN'})})
        mock_sts.connect_to_region.return_value = mock_conn

        sys.stdin = StringIO(saml_assertion(['arn:aws:iam::1111:role/DevRole,arn:aws:iam::1111:saml-provider/IDP']))
        cli.main(['test.py', 'saml',
                  '--profile', 'test-profile',
                  '--region', 'un-south-1'])

        six.assertCountEqual(self,
                             read_config_file(self.TEST_FILE),
                             ['[test-profile]',
                              'output = json',
                              'region = un-south-1',
                              'aws_access_key_id = SAML_ACCESS_KEY',
                              'aws_secret_access_key = SAML_SECRET_KEY',
                              'aws_security_token = SAML_TOKEN',
                              'aws_session_token = SAML_TOKEN',
                              '']) 
Example #11
Source File: test_parser.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def testParseStream(self):
        dstr = StringIO('2014 January 19')

        self.assertEqual(parse(dstr), datetime(2014, 1, 19)) 
Example #12
Source File: test_parser.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def testDuckTyping(self):
        # We want to support arbitrary classes that implement the stream
        # interface.

        class StringPassThrough(object):
            def __init__(self, stream):
                self.stream = stream

            def read(self, *args, **kwargs):
                return self.stream.read(*args, **kwargs)


        dstr = StringPassThrough(StringIO('2014 January 19'))

        self.assertEqual(parse(dstr), datetime(2014, 1, 19)) 
Example #13
Source File: estimator_test.py    From object_detection_with_tensorflow with MIT License 5 votes vote down vote up
def four_lines_data():
  text = StringIO(FOUR_LINES)

  df = pd.read_csv(text, names=iris_data.CSV_COLUMN_NAMES)

  xy = (df, df.pop("Species"))
  return xy, xy 
Example #14
Source File: execution.py    From http-prompt with MIT License 5 votes vote down vote up
def _colorize(self, text, token_type):
        if not self.formatter:
            return text

        out = StringIO()
        self.formatter.format([(token_type, text)], out)
        return out.getvalue() 
Example #15
Source File: utils.py    From klusta with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def captured_output():
    new_out, new_err = StringIO(), StringIO()
    old_out, old_err = sys.stdout, sys.stderr
    try:
        sys.stdout, sys.stderr = new_out, new_err
        yield sys.stdout, sys.stderr
    finally:
        sys.stdout, sys.stderr = old_out, old_err 
Example #16
Source File: regression_test.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def four_lines_dataframe():
  text = StringIO(FOUR_LINES)
  return pd.read_csv(text, names=automobile_data.COLUMN_TYPES.keys(),
                     dtype=automobile_data.COLUMN_TYPES, na_values="?") 
Example #17
Source File: parser.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def parse(self, html):
        try:
            tree = etree.parse(StringIO(html), etree.HTMLParser())
            status_tables = tree.xpath(self.STATUS_TABLE_XPATH)

            return self._parse_services(status_tables)

        except Exception:
            LOG.exception('Failed to get nagios services.') 
Example #18
Source File: estimator_test.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
def four_lines_data():
  text = StringIO(FOUR_LINES)

  df = pd.read_csv(text, names=iris_data.CSV_COLUMN_NAMES)

  xy = (df, df.pop("Species"))
  return xy, xy 
Example #19
Source File: paramiko_ssh.py    From st2 with Apache License 2.0 5 votes vote down vote up
def _get_pkey_object(self, key_material, passphrase):
        """
        Try to detect private key type and return paramiko.PKey object.
        """

        for cls in [paramiko.RSAKey, paramiko.DSSKey, paramiko.ECDSAKey]:
            try:
                key = cls.from_private_key(StringIO(key_material), password=passphrase)
            except paramiko.ssh_exception.SSHException:
                # Invalid key, try other key type
                pass
            else:
                return key

        # If a user passes in something which looks like file path we throw a more friendly
        # exception letting the user know we expect the contents a not a path.
        # Note: We do it here and not up the stack to avoid false positives.
        contains_header = REMOTE_RUNNER_PRIVATE_KEY_HEADER in key_material.lower()
        if not contains_header and (key_material.count('/') >= 1 or key_material.count('\\') >= 1):
            msg = ('"private_key" parameter needs to contain private key data / content and not '
                   'a path')
        elif passphrase:
            msg = 'Invalid passphrase or invalid/unsupported key type'
        else:
            msg = 'Invalid or unsupported key type'

        raise paramiko.ssh_exception.SSHException(msg) 
Example #20
Source File: ipython.py    From OpenModes with GNU General Public License v3.0 5 votes vote down vote up
def init_3d():
    """Initialise 3D plots within the IPython notebook, by injecting the
    required javascript libraries.
    """

    library_javascript = StringIO()

    library_javascript.write("""
    <script type="text/javascript">
    /* Beginning of javascript injected by OpenModes */
    var openmodes_javascript_injected = true;
    """)

    three_js_libraries = ("three.min.js", "OrbitControls.js",
                          "Lut.js", "Detector.js", "CanvasRenderer.js",
                          "Projector.js")

    # Include required parts of three.js inline
    for library in three_js_libraries:
        with open(osp.join(three_js_dir, library)) as infile:
            library_javascript.write(infile.read())

    # include my custom javascript inline
    with open(osp.join(static_dir, "three_js_plot.js")) as infile:
        library_javascript.write(infile.read())

    library_javascript.write(
                "/* End of javascript injected by OpenModes */\n</script>\n")

    display(HTML(library_javascript.getvalue()))
    logging.info("Javascript injected for 3D interactive WebGL plots") 
Example #21
Source File: jobStoreTest.py    From toil with Apache License 2.0 5 votes vote down vote up
def _prepareTestFile(self, bucket, size=None):
        from toil.jobStores.googleJobStore import GoogleJobStore
        fileName = 'testfile_%s' % uuid.uuid4()
        url = 'gs://%s/%s' % (bucket.name, fileName)
        if size is None:
            return url
        read_type = 'r' if USING_PYTHON2 else 'rb'
        with open('/dev/urandom', read_type) as readable:
            if USING_PYTHON2:
                contents = readable.read(size)
            else:
                contents = str(readable.read(size))
        GoogleJobStore._writeToUrl(StringIO(contents), urlparse.urlparse(url))
        return url, hashlib.md5(contents).hexdigest() 
Example #22
Source File: googleJobStore.py    From toil with Apache License 2.0 5 votes vote down vote up
def _writeString(self, jobStoreID, stringToUpload, **kwarg):
        self._writeFile(jobStoreID, StringIO(stringToUpload), **kwarg) 
Example #23
Source File: googleJobStore.py    From toil with Apache License 2.0 5 votes vote down vote up
def getEmptyFileStoreID(self, jobStoreID=None, cleanup=False):
        fileID = self._newID(isFile=True, jobStoreID=jobStoreID if cleanup else None)
        self._writeFile(fileID, StringIO(""))
        return fileID 
Example #24
Source File: result.py    From intel-iot-refkit with MIT License 5 votes vote down vote up
def _save_output_data(self):
        # Only try to get sys.stdout and sys.sterr as they not be
        # StringIO yet, e.g. when test fails during __call__
        try:
            self._stdout_data = sys.stdout.getvalue()
            self._stderr_data = sys.stderr.getvalue()
        except AttributeError:
            pass 
Example #25
Source File: test_fastaiter.py    From biocommons.seqrepo with Apache License 2.0 5 votes vote down vote up
def test_multiline():
    data = StringIO(">seq1\nACGT\nTGCA")

    iterator = FastaIter(data)

    header, seq = six.next(iterator)
    assert header == "seq1"
    assert seq == "ACGTTGCA"

    # should be empty now
    with pytest.raises(StopIteration):
        six.next(iterator) 
Example #26
Source File: test_fastaiter.py    From biocommons.seqrepo with Apache License 2.0 5 votes vote down vote up
def test_single():
    data = StringIO(">seq1\nACGT\n")

    iterator = FastaIter(data)

    header, seq = six.next(iterator)
    assert header == "seq1"
    assert seq == "ACGT"

    # should be empty now
    with pytest.raises(StopIteration):
        six.next(iterator) 
Example #27
Source File: test_fastaiter.py    From biocommons.seqrepo with Apache License 2.0 5 votes vote down vote up
def test_noheader():
    data = StringIO("ACGT\n")

    iterator = FastaIter(data)

    # should return an empty generator
    with pytest.raises(StopIteration):
        six.next(iterator) 
Example #28
Source File: test_fastaiter.py    From biocommons.seqrepo with Apache License 2.0 5 votes vote down vote up
def test_empty():
    data = StringIO("")

    iterator = FastaIter(data)

    # should return an empty generator
    with pytest.raises(StopIteration):
        six.next(iterator) 
Example #29
Source File: query.py    From insights-core with Apache License 2.0 5 votes vote down vote up
def get_pydoc(spec):
    obj = load_obj(spec)
    if obj:
        output = StringIO()
        pydoc.Helper(output=output).help(obj)
        output.seek(0)
        return output.read() 
Example #30
Source File: utils_test.py    From loaner with Apache License 2.0 5 votes vote down vote up
def setUp(self):
    super(UtilTest, self).setUp()
    self.stdout = StringIO()
    self.stubs = mox3_stubout.StubOutForTesting()
    self.stubs.SmartSet(sys, 'stdout', self.stdout)