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


Python StringIO.read方法代码示例

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


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

示例1: test_any_sequences_to_fasta

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
    def test_any_sequences_to_fasta(self):
        for fn, obj in ((_sequence_collection_to_fasta, self.seq_coll),
                        (_alignment_to_fasta, self.align)):
            # test writing with default parameters
            fh = StringIO()
            fn(obj, fh)
            obs = fh.getvalue()
            fh.close()

            with open(get_data_path('fasta_3_seqs_defaults'), 'U') as fh:
                exp = fh.read()

            self.assertEqual(obs, exp)

            # test writing with non-defaults
            fasta_fh = StringIO()
            qual_fh = StringIO()
            fn(obj, fasta_fh, id_whitespace_replacement='*',
               description_newline_replacement='+', max_width=3, qual=qual_fh)
            obs_fasta = fasta_fh.getvalue()
            obs_qual = qual_fh.getvalue()
            fasta_fh.close()
            qual_fh.close()

            with open(get_data_path('fasta_3_seqs_non_defaults'), 'U') as fh:
                exp_fasta = fh.read()
            with open(get_data_path('qual_3_seqs_non_defaults'), 'U') as fh:
                exp_qual = fh.read()

            self.assertEqual(obs_fasta, exp_fasta)
            self.assertEqual(obs_qual, exp_qual)
开发者ID:sh4wn,项目名称:scikit-bio,代码行数:33,代码来源:test_fasta.py

示例2: patch

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def patch(old_file_name, new_file_name, patch_file_name):

    patch_file = open(patch_file_name, "rb")
    patch_file.read(8) #magic number

    compressed_control_len = offtin(patch_file.read(8))
    compressed_diff_len = offtin(patch_file.read(8))
    new_file_len = offtin(patch_file.read(8))

    compressed_control_block = patch_file.read(compressed_control_len)
    compressed_diff_block    = patch_file.read(compressed_diff_len)
    compressed_extra_block   = patch_file.read()

    control_stream = StringIO(bz2.decompress(compressed_control_block))
    diff_string    = bz2.decompress(compressed_diff_block)
    extra_string   = bz2.decompress(compressed_extra_block)

    control_tuples_list = []
    while True:
        r = control_stream.read(8)
        if not r:
            break
        x = offtin(r)
        y = offtin(control_stream.read(8))
        z = offtin(control_stream.read(8))
        control_tuples_list.append((x,y,z))

    old_data = open(old_file_name, "rb").read()
    new_data = Patch(old_data, new_file_len, control_tuples_list, diff_string, extra_string)

    new_file = open(new_file_name, "wb")
    new_file.write(new_data)
开发者ID:iOSForensics,项目名称:pymobiledevice,代码行数:34,代码来源:bpatch.py

示例3: compress_string

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def compress_string(s):

    # avg_block_size is acutally the reciporical of the average
    # intended interflush distance.   

    rnd = Random(s)

    flushes_remaining = FLUSH_LIMIT

    if len(s) < AVERAGE_SPAN_BETWEEN_FLUSHES * APPROX_MIN_FLUSHES:
        avg_block_size = APPROX_MIN_FLUSHES / float(len(s) + 1)
    else:
        avg_block_size = 1.0 / AVERAGE_SPAN_BETWEEN_FLUSHES

    s = StringIO(s) if isinstance(s, six.text_type) else BytesIO(s)
    zbuf = BytesIO()
    zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
    chunk = s.read(MIN_INTERFLUSH_INTERVAL + int(rnd.expovariate(avg_block_size)))
    while chunk and flushes_remaining:
        zfile.write(chunk)
        zfile.flush()
        flushes_remaining -= 1
        chunk = s.read(MIN_INTERFLUSH_INTERVAL + int(rnd.expovariate(avg_block_size)))
    zfile.write(chunk)
    zfile.write(s.read())
    zfile.close()
    return zbuf.getvalue()
开发者ID:miki725,项目名称:breach_buster,代码行数:29,代码来源:gzip.py

示例4: csv_iter

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
 def csv_iter():
     rows = iter(table)
     fo = IO()
     csv_writer = csv.writer(fo)
     csv_writer.writerow(converter.header2())
     while True:
         try:
           for _ in range(1000):
             row = next(rows)
             #print row
             csv_writer.writerow(row)
         except StopIteration:
             fo.seek(0)
             yield fo.read().encode('utf-8')
             del fo
             break
         fo.seek(0)
         data = fo.read().encode('utf-8')
         fo.seek(0)
         fo.truncate()
         yield data
     if converter.errors:
         yield 'The following errors were found at unspecified points in processing:\n'
         for error in converter.errors:
             yield str(error)+'\n'
开发者ID:SaraHeydari,项目名称:koota-server,代码行数:27,代码来源:views_data.py

示例5: test_any_sequence_to_fasta

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
    def test_any_sequence_to_fasta(self):
        # store writer function, sequence object to write, expected
        # fasta filepath for default parameters, expected fasta filepath for
        # non-defaults, and expected qual filepath for non-defaults
        id_ = 'f o o'
        desc = 'b\na\nr'
        test_data = (
            (_biological_sequence_to_fasta,
             Sequence('ACGT', id=id_, description=desc,
                      quality=range(1, 5)),
             ('fasta_single_bio_seq_defaults',
              'fasta_single_bio_seq_non_defaults',
              'qual_single_bio_seq_non_defaults')),
            (_dna_sequence_to_fasta,
             DNA('TACG', id=id_, description=desc, quality=range(4)),
             ('fasta_single_dna_seq_defaults',
              'fasta_single_dna_seq_non_defaults',
              'qual_single_dna_seq_non_defaults')),
            (_rna_sequence_to_fasta,
             RNA('UACG', id=id_, description=desc, quality=range(2, 6)),
             ('fasta_single_rna_seq_defaults',
              'fasta_single_rna_seq_non_defaults',
              'qual_single_rna_seq_non_defaults')),
            (_protein_sequence_to_fasta,
             Protein('PQQ', id=id_, description=desc, quality=[42, 41, 40]),
             ('fasta_single_prot_seq_defaults',
              'fasta_single_prot_seq_non_defaults',
              'qual_single_prot_seq_non_defaults')))

        for fn, obj, fps in test_data:
            defaults_fp, non_defaults_fasta_fp, non_defaults_qual_fp = fps

            # test writing with default parameters
            fh = StringIO()
            fn(obj, fh)
            obs = fh.getvalue()
            fh.close()

            with open(get_data_path(defaults_fp), 'U') as fh:
                exp = fh.read()

            self.assertEqual(obs, exp)

            # test writing with non-defaults
            fasta_fh = StringIO()
            qual_fh = StringIO()
            fn(obj, fasta_fh, id_whitespace_replacement='-',
               description_newline_replacement='_', max_width=1, qual=qual_fh)
            obs_fasta = fasta_fh.getvalue()
            obs_qual = qual_fh.getvalue()
            fasta_fh.close()
            qual_fh.close()

            with open(get_data_path(non_defaults_fasta_fp), 'U') as fh:
                exp_fasta = fh.read()
            with open(get_data_path(non_defaults_qual_fp), 'U') as fh:
                exp_qual = fh.read()

            self.assertEqual(obs_fasta, exp_fasta)
            self.assertEqual(obs_qual, exp_qual)
开发者ID:bctaylor,项目名称:scikit-bio,代码行数:62,代码来源:test_fasta.py

示例6: _parse10

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
    def _parse10(self):

        """Messages are delimited by MSG_DELIM. The buffer could have grown by
        a maximum of BUF_SIZE bytes everytime this method is called. Retains
        state across method calls and if a byte has been read it will not be
        considered again."""

        logger.debug("parsing netconf v1.0")
        delim = MSG_DELIM
        n = len(delim) - 1
        expect = self._parsing_state10
        buf = self._buffer
        buf.seek(self._parsing_pos10)
        while True:
            x = buf.read(1)
            if isinstance(x, bytes):
                x = x.decode('UTF-8')
            if not x: # done reading
                break
            elif x == delim[expect]: # what we expected
                expect += 1 # expect the next delim char
            else:
                expect = 0
                continue
            # loop till last delim char expected, break if other char encountered
            for i in range(expect, n):
                x = buf.read(1)
                if isinstance(x, bytes):
                    x = x.decode('UTF-8')
                if not x: # done reading
                    break
                if x == delim[expect]: # what we expected
                    expect += 1 # expect the next delim char
                else:
                    expect = 0 # reset
                    break
            else: # if we didn't break out of the loop, full delim was parsed
                msg_till = buf.tell() - n
                buf.seek(0)
                logger.debug('parsed new message')
                if sys.version < '3':
                    self._dispatch_message(buf.read(msg_till).strip())
                    buf.seek(n+1, os.SEEK_CUR)
                    rest = buf.read()
                    buf = StringIO()
                else:
                    self._dispatch_message(buf.read(msg_till).strip().decode('UTF-8'))
                    buf.seek(n+1, os.SEEK_CUR)
                    rest = buf.read()
                    buf = BytesIO()
                buf.write(rest)
                buf.seek(0)
                expect = 0
        self._buffer = buf
        self._parsing_state10 = expect
        self._parsing_pos10 = self._buffer.tell()
开发者ID:JoProvost,项目名称:ncclient,代码行数:58,代码来源:ssh.py

示例7: test_any_sequences_to_fasta

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
    def test_any_sequences_to_fasta(self):
        for fn, obj in ((_sequence_collection_to_fasta, self.seq_coll),
                        (_alignment_to_fasta, self.align)):
            # test writing with default parameters
            fh = StringIO()
            fn(obj, fh)
            obs = fh.getvalue()
            fh.close()

            with open(get_data_path('fasta_3_seqs_defaults'), 'U') as fh:
                exp = fh.read()

            self.assertEqual(obs, exp)

            # test writing with non-defaults
            fasta_fh = StringIO()
            qual_fh = StringIO()
            fn(obj, fasta_fh, id_whitespace_replacement='*',
               description_newline_replacement='+', max_width=3, qual=qual_fh)
            obs_fasta = fasta_fh.getvalue()
            obs_qual = qual_fh.getvalue()
            fasta_fh.close()
            qual_fh.close()

            with open(get_data_path('fasta_3_seqs_non_defaults'), 'U') as fh:
                exp_fasta = fh.read()
            with open(get_data_path('qual_3_seqs_non_defaults'), 'U') as fh:
                exp_qual = fh.read()

            self.assertEqual(obs_fasta, exp_fasta)
            self.assertEqual(obs_qual, exp_qual)

            fh2 = StringIO()
            with self.assertRaisesRegexp(AttributeError,
                                         "lowercase specified but class "
                                         "Sequence does not support lowercase "
                                         "functionality"):
                fn(obj, fh2, lowercase='introns')
            fh2.close()

            fasta_fh2 = StringIO()
            qual_fh2 = StringIO()
            with self.assertRaisesRegexp(AttributeError,
                                         "lowercase specified but class "
                                         "Sequence does not support lowercase "
                                         "functionality"):
                fn(obj, fasta_fh2, id_whitespace_replacement='*',
                   description_newline_replacement='+', max_width=3,
                   qual=qual_fh2, lowercase='introns')
            fasta_fh2.close()
            qual_fh2.close()
开发者ID:johnchase,项目名称:scikit-bio,代码行数:53,代码来源:test_fasta.py

示例8: test_start_subshell

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
    def test_start_subshell(self, call_mock, tempfile_mock):
        memfile = StringIO()
        memfile.name = 'FILENAME'
        tempfile_mock.return_value = memfile
        credentials = {'AWS_VALID_SECONDS': 600}
        start_subshell(credentials, 'ACCOUNT', 'ROLE')
        call_mock.assert_called_once_with(
            ["bash", "--rcfile", 'FILENAME'],
            stdout=sys.stdout, stderr=sys.stderr, stdin=sys.stdin)
        expected = dedent("""
            # Pretend to be an interactive, non-login shell
            for file in /etc/bash.bashrc ~/.bashrc; do
                [ -f "$file" ] && . "$file"
            done

            function afp_minutes_left {
                if ((SECONDS >= 600)) ; then
                    echo EXPIRED
                else
                    echo $(((600-SECONDS)/60)) Min
                fi
            }

            PS1="(AWS ACCOUNT/ROLE \$(afp_minutes_left)) $PS1"
            export AWS_VALID_SECONDS='600'""")
        memfile.seek(0)
        received = memfile.read()
        self.assertEqual(received, expected)
开发者ID:schlomo,项目名称:afp-cli,代码行数:30,代码来源:exporters_tests.py

示例9: execute_code

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def execute_code(string, state, data=None):
    string = displayhook_hack(string)

    # Now execute the code capturing the output and files that are
    # generated.
    back = os.path.abspath('.')
    tempdir = tempfile.mkdtemp()
    if data is not None:
        # make a symbolic link from the data directory into local tmp directory
        os.symlink(data, os.path.join(tempdir, os.path.split(data)[1]))
    
    s = StringIO()
    saved_stream = sys.stdout
    sys.stdout = s
    try:
        os.chdir(tempdir)
        exec(string, state)
    except Exception:
        traceback.print_exc(file=s)
    finally:
        sys.stdout = saved_stream
        os.chdir(back)
    s.seek(0)
    out = str(s.read())
    files = [os.path.join(tempdir, x) for x in os.listdir(tempdir)]
    return out, files, tempdir
开发者ID:fchapoton,项目名称:sagenb,代码行数:28,代码来源:reference.py

示例10: test_file_output

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def test_file_output():
    """ Tests that output to arbitrary file-like objects works """
    our_file = StringIO()
    for i in tqdm(range(3), file=our_file):
        if i == 1:
            our_file.seek(0)
            assert '0/3' in our_file.read()
开发者ID:obiwanus,项目名称:tqdm,代码行数:9,代码来源:tests.py

示例11: test_log

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
 def test_log(self):
     # Logger
     logger = logging.getLogger(__name__)
     logger.level = logging.INFO
     stream = StringIO()
     stream_handler = logging.StreamHandler(stream)
     logger.addHandler(stream_handler)
     # Actual test
     request = testing.DummyRequest()
     apply_request_extensions(request)
     self.assertTrue(request.authenticated_userid, 'jane')
     obj = self._cut(request)
     # Patch logger
     obj.logger = logger
     obj.add('uid', 'darth', [ROLE_VIEWER], [ROLE_OWNER])
     payload = obj.prepare()
     formatted = obj.format(payload)
     try:
         obj.log(formatted)
         stream.seek(0)
         output = stream.read()
     finally:
         logger.removeHandler(stream_handler)
     self.assertIn('"+": ["role:Viewer"]', output)
     self.assertIn('"-": ["role:Owner"]', output)
     self.assertIn('"jane"', output)
     # Make sure json can read this
     json_row = loads(output)
     self.assertIn('contexts', json_row)
开发者ID:ArcheProject,项目名称:Arche,代码行数:31,代码来源:test_roles.py

示例12: toXml

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
 def toXml(self, filename='', compress=False):
     xml = StringIO()
     xml.write("<?xml version='1.0' encoding='UTF-8'?>\n")
     xml.write(
         "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd \">\n")
     self.svg.toXml(0, xml)
     if not filename:
         if compress:
             import gzip
             f = StringIO()
             zf = gzip.GzipFile(fileobj=f, mode='wb')
             zf.write(xml.getvalue())
             zf.close()
             f.seek(0)
             return f.read()
         else:
             return xml.getvalue()
     else:
         if filename[-4:] == 'svgz':
             import gzip
             f = gzip.GzipFile(
                 filename=filename, mode="wb", compresslevel=9)
             f.write(xml.getvalue())
             f.close()
         else:
             f = file(filename, 'w')
             f.write(xml.getvalue())
             f.close()
开发者ID:CGATOxford,项目名称:cgat,代码行数:30,代码来源:SVGdraw.py

示例13: to_string

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def to_string(table):
    """
    Returns a list of the maximum width for each column across all rows
    >>> type(to_string([['foo', 'goodbye'], ['llama', 'bar']]))
    <type 'unicode'>
    """
    result = StringIO()

    (columns, rows) = get_dimensions(table)
        
    result.write("     {} columns, {} rows\n".format(columns, rows))
    col_widths = find_column_widths(table)
    table_width = sum(col_widths) + len(col_widths) + 2
    hbar = '    {}\n'.format('-' * table_width)

    result.write("      {}\n".format(' '.join(
        [six.text_type(col_index).rjust(width, ' ') for (col_index, width)
         in enumerate(col_widths)])))

    result.write(hbar)
    for row_index, row in enumerate(table):
        cells = [cell.rjust(width, ' ') for (cell, width)
                 in zip(row, col_widths)]
        result.write("{:>3} | {}|\n".format(row_index, '|'.join(cells)))
    result.write(hbar)
    result.seek(0)
    return six.text_type(result.read())
开发者ID:vnaydionov,项目名称:pdftables,代码行数:29,代码来源:display.py

示例14: test_to_string3

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def test_to_string3():
    # Test printing
    outcomes = ['00', '01', '10', '11']
    pmf = [1/4]*4
    d = Distribution(outcomes, pmf)
    s_ = """Class:          Distribution
Alphabet:       ('0', '1') for all rvs
Base:           linear
Outcome Class:  str
Outcome Length: 2
RV Names:       None

x    p(x)
00   0.25
01   0.25
10   0.25
11   0.25"""

    # context manager?
    import sys
    from six import StringIO
    sio = StringIO()
    try:
        old = sys.stdout
        sys.stdout = sio
        print(d, end='')
    finally:
        sys.stdout = old
    sio.seek(0)
    s = sio.read()
    assert_equal(s, s_)
开发者ID:chebee7i,项目名称:dit,代码行数:33,代码来源:test_distribution.py

示例15: runquery_csv

# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import read [as 别名]
def runquery_csv():
	global out

	q = frappe.form_dict.get('query')

	rep_name = frappe.form_dict.get('report_name')
	if not frappe.form_dict.get('simple_query'):

		# Report Name
		if not rep_name:
			rep_name = get_sql_tables(q)[0]

	if not rep_name: rep_name = 'DataExport'

	rows = [[rep_name], out['colnames']] + out['values']

	from six import StringIO
	import csv

	f = StringIO()
	writer = csv.writer(f)
	for r in rows:
		# encode only unicode type strings and not int, floats etc.
		writer.writerow(map(lambda v: isinstance(v, text_type) and v.encode('utf-8') or v, r))

	f.seek(0)
	out['result'] = text_type(f.read(), 'utf-8')
	out['type'] = 'csv'
	out['doctype'] = rep_name
开发者ID:ESS-LLP,项目名称:frappe,代码行数:31,代码来源:query_builder.py


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