Python io.StringIO() Examples

The following are code examples for showing how to use io.StringIO(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: fs_image   Author: facebookincubator   File: test_yum_dnf_conf.py    MIT License 6 votes vote down vote up
def setUp(self):
        # More output for easier debugging
        unittest.util._MAX_LENGTH = 12345
        self.maxDiff = 12345

        conf_str = io.StringIO(textwrap.dedent('''\
        # Unfortunately, comments are discarded by ConfigParser, but I don't
        # want to depend on `ConfigObj` or `iniparse` for this.
        [main]
        debuglevel=2
        gpgcheck=1

        [potato]
        baseurl=file:///pot.at/to
        enabled=1

        [oleander]
        baseurl=http://example.com/oleander
        gpgkey=https://example.com/zupa
        \thttps://example.com/super/safe
        enabled=1
        '''))
        self.conf = YumDnfConfParser(self._YUM_DNF, conf_str) 
Example 2
Project: odorik   Author: nijel   File: test_main.py    GNU General Public License v3.0 6 votes vote down vote up
def execute(args, binary=False, settings=None, stdout=None):
    """Execute command and return output."""
    if settings is None:
        settings = ()
    elif not settings:
        settings = None
    if binary and sys.version_info < (3, 0):
        output = BytesIO()
    else:
        output = StringIO()
    backup = sys.stdout
    try:
        sys.stdout = output
        if stdout:
            stdout = output
        main(args=args, settings=settings, stdout=stdout)
    finally:
        sys.stdout = backup
    return output.getvalue() 
Example 3
Project: ubittool   Author: carlosperate   File: cmds.py    MIT License 6 votes vote down vote up
def _bytes_to_intel_hex(data, offset=0x0000):
    """Take a list of bytes and return a string in the Intel Hex format.

    :param data: List of integers, each representing a single byte.
    :param offset: Start address offset.
    :return: A string with the Intel Hex encoded data.
    """
    i_hex = IntelHex()
    i_hex.frombytes(data, offset)

    fake_file = StringIO()
    try:
        i_hex.tofile(fake_file, format="hex")
    except IOError as e:
        sys.stderr.write("ERROR: File write: {}\n{}".format(fake_file, str(e)))
        return

    intel_hex_str = fake_file.getvalue()
    fake_file.close()
    return intel_hex_str 
Example 4
Project: ubittool   Author: carlosperate   File: cmds.py    MIT License 6 votes vote down vote up
def _bytes_to_pretty_hex(data, offset=0x0000):
    """Convert a list of bytes to a nicely formatted ASCII decoded hex string.

    :param data: List of integers, each representing a single byte.
    :param offset: Start address offset.
    :return: A string with the formatted hex data.
    """
    i_hex = IntelHex()
    i_hex.frombytes(data, offset)

    fake_file = StringIO()
    try:
        i_hex.dump(tofile=fake_file, width=16, withpadding=False)
    except IOError as e:
        sys.stderr.write("ERROR: File write: {}\n{}".format(fake_file, str(e)))
        return

    pretty_hex_str = fake_file.getvalue()
    fake_file.close()
    return pretty_hex_str


#
# Reading data commands
# 
Example 5
Project: payroll   Author: andela-sjames   File: utils.py    MIT License 6 votes vote down vote up
def process_input(csv_data):

    data_set = csv_data.read().decode('UTF-8')
    io_string = io.StringIO(data_set)

    # Skip header, we do not need it, b/cos it's guaranteed.
    next(io_string)
    data = []
    # move on to the actual data
    for row in csv.reader(io_string, delimiter=",", quotechar="|"):
        # get the last row to know report_id
        columns = [row[0], row[1], row[2], row[3]]
        data.append(columns)

    last_row = data.pop()
    return data, get_or_create_report(int(last_row[1])) 
Example 6
Project: pyblish-win   Author: pyblish   File: test_memoryio.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_newlines_property(self):
        memio = self.ioclass(newline=None)
        # The C StringIO decodes newlines in write() calls, but the Python
        # implementation only does when reading.  This function forces them to
        # be decoded for testing.
        def force_decode():
            memio.seek(0)
            memio.read()
        self.assertEqual(memio.newlines, None)
        memio.write("a\n")
        force_decode()
        self.assertEqual(memio.newlines, "\n")
        memio.write("b\r\n")
        force_decode()
        self.assertEqual(memio.newlines, ("\n", "\r\n"))
        memio.write("c\rd")
        force_decode()
        self.assertEqual(memio.newlines, ("\r", "\n", "\r\n")) 
Example 7
Project: auto-check-in   Author: zeekvfu   File: v2ex_copper_coin.py    GNU General Public License v3.0 6 votes vote down vote up
def get_login_parameters(self):
        this_func_name = sys._getframe().f_code.co_name
        self.logger.debug("%s(): start ..." % this_func_name)
        response = self.session.get(self.login_url)

        parser = etree.HTMLParser()
        tree = etree.parse(StringIO(response.text), parser)
        elements = tree.xpath('//form[@method="post" and @action="/signin"]/table[@cellpadding="5" and @cellspacing="0" and @border="0" and @width="100%"]//tr[position()<last()]/td[2]/input')
        user_name_key = None
        l = elements[0].xpath('@name')
        if len(l) == 1:
            user_name_key = l[0]
        password_key = None
        l = elements[1].xpath('@name')
        if len(l) == 1:
            password_key = l[0]
        once = None
        l = elements[2].xpath('@value')
        if len(l) == 1:
            once = l[0]
        self.logger.debug("%s(): user_name_key: %s\tpassword_key: %s\tonce: %s" % (this_func_name, user_name_key, password_key, once))
        self.logger.debug("%s(): end ..." % this_func_name)
        return user_name_key, password_key, once 
Example 8
Project: auto-check-in   Author: zeekvfu   File: v2ex_copper_coin.py    GNU General Public License v3.0 6 votes vote down vote up
def get_copper_coin(self, once):
        this_func_name = sys._getframe().f_code.co_name
        self.logger.debug("%s(): start ..." % this_func_name)

        # 重新获取 once 的值
        response = self.session.get(self.check_in_url)
        if "每日登录奖励已领取" in response.text:
            self.logger.debug("%s(): copper coin already fetched ..." % this_func_name)
            return
        parser = etree.HTMLParser()
        tree = etree.parse(StringIO(response.text), parser)
        s = tree.xpath('//input[@type="button" and @class="super normal button" and @value="领取 X 铜币" and contains(@onclick, "location.href")]/@onclick')[0]
        path = s[s.index("'")+1: s.rindex("'")]

        # 领取铜币
        url = self.homepage_url + path
        response = self.session.get(url)
        flag = False
        if "已成功领取每日登录奖励" in response.text:
            flag = True
        self.logger.debug("%s(): url: %s\tcopper_coin: %s" % (this_func_name, url, flag))
        self.logger.debug("%s(): end ..." % this_func_name)
        return 
Example 9
Project: django-template   Author: liip   File: fixturize.py    MIT License 6 votes vote down vote up
def reset_db():
    """
    Reset database to a blank state by removing all the tables and recreating them.
    """
    with connection.cursor() as cursor:
        cursor.execute("select tablename from pg_tables where schemaname = 'public'")
        tables = [row[0] for row in cursor.fetchall()]

        # Can't use query parameters here as they'll add single quotes which are not
        # supported by postgres
        for table in tables:
            cursor.execute('drop table "' + table + '" cascade')

    # Call migrate so that post-migrate hooks such as generating a default Site object
    # are run
    management.call_command("migrate", "--noinput", stdout=StringIO()) 
Example 10
Project: django-template   Author: liip   File: fabfile.py    MIT License 6 votes vote down vote up
def set_setting(self, name, value=None, force: bool = True):
        """
        Set a setting in the environment directory, for use by Django
        """
        envfile_path = os.path.join(self.envdir_path, name)

        will_write = force
        if not force:
            try:
                # Test that it does exist
                self.run_in_project_root("test -r {}".format(envfile_path), hide=True)
            except UnexpectedExit:
                will_write = True

        if will_write:
            if value is None:
                value = input("Value for {}: ".format(name))

            # Convert booleans into values understood as such by Django
            if isinstance(value, bool):
                value = "1" if value else ""
            self.put(StringIO("{}\n".format(value)), envfile_path) 
Example 11
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: wstokenizer_mod.py    Apache License 2.0 6 votes vote down vote up
def launch(args,q_in,q_out):
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            q_out.put((jobid,txt))
            return
        cache=io.StringIO()
        for line in txt.split("\n"):
            line=line.strip()
            if not line:
                continue
            if line.startswith("###C:"):
                print(line,file=cache)
            else:
                words=line.split()
                for idx,w in enumerate(words):
                    print(idx+1,w,*(["_"]*8),sep="\t",file=cache)
                print(file=cache)
        q_out.put((jobid,cache.getvalue())) 
Example 12
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: wipe_mod.py    Apache License 2.0 6 votes vote down vote up
def launch(args,q_in,q_out):
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            q_out.put((jobid,txt))
            return
        cache=io.StringIO()
        for sent,comments in read_conll(txt.split("\n"),drop_tokens=False,drop_nulls=False):
            if comments:
                print("\n".join(comments),file=cache)
            for cols in sent:
                for col in (LEMMA,UPOS,XPOS,FEATS,HEAD,DEPREL,DEPS):
                    cols[col]="_"
                print("\t".join(cols),file=cache)
            print(file=cache)
        q_out.put((jobid,cache.getvalue())) 
Example 13
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: trim_to_max_mod.py    Apache License 2.0 6 votes vote down vote up
def launch(args,q_in,q_out):
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            q_out.put((jobid,txt))
            return
        cache=io.StringIO()
        for sent,comments in read_conll(txt.split("\n"),drop_tokens=False,drop_nulls=False):
            if len(sent)>args.max_sent_len:
                comments.append("###TRIMMED_BY_PARSER FROM ORIGINAL OF {} WORDS".format(len(sent)))
                sent=sent[:args.max_sent_len]
            for i,token in enumerate(sent):
                if len(token[FORM])>args.max_token_len:
                    comments.append("###TOKEN {tid} TRIMMED_BY_PARSER FROM ORIGINAL OF {l} CHARACTERS | ORIG_TOKEN={orig}".format(tid=str(i+1), l=len(token[FORM]), orig=token[FORM]))
                    sent[i][FORM]=token[FORM][:args.max_token_len]
            if comments:
                print("\n".join(comments),file=cache)
            for cols in sent:
                for col in (LEMMA,UPOS,XPOS,FEATS,HEAD,DEPREL,DEPS):
                    cols[col]="_"
                print("\t".join(cols),file=cache)
            print(file=cache)
        q_out.put((jobid,cache.getvalue())) 
Example 14
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: regextokenizer_mod.py    Apache License 2.0 6 votes vote down vote up
def launch(args,q_in,q_out):
    counter=0
    while True:
        jobid,txt=q_in.get()
        if jobid=="FINAL":
            q_out.put((jobid,txt))
            return
        cache=io.StringIO()
        for sent in sentences(txt):
            print("# sent_id =",counter,file=cache)
            counter+=1
            #print("# text =",sent.replace("\n"," "),file=cache)
            for id,token in enumerate(tokens(sent)):
                print(id+1,token,*(["_"]*8),sep="\t",file=cache)
            print(file=cache)
        q_out.put((jobid,cache.getvalue())) 
Example 15
Project: neural-fingerprinting   Author: StephanZheng   File: image_batches.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __str__(self):
    """Returns human readable representation, which is useful for debugging."""
    buf = StringIO()
    for batch_idx, (batch_id, batch_val) in enumerate(iteritems(self.data)):
      if batch_idx >= TO_STR_MAX_BATCHES:
        buf.write(u'...\n')
        break
      buf.write(u'BATCH "{0}"\n'.format(batch_id))
      for k, v in iteritems(batch_val):
        if k != 'images':
          buf.write(u'  {0}: {1}\n'.format(k, v))
      for img_idx, img_id in enumerate(iterkeys(batch_val['images'])):
        if img_idx >= TO_STR_MAX_IMAGES_PER_BATCH:
          buf.write(u'  ...')
          break
        buf.write(u'  IMAGE "{0}" -- {1}\n'.format(img_id,
                                                   batch_val['images'][img_id]))
      buf.write(u'\n')
    return buf.getvalue() 
Example 16
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: __init__.py    MIT License 6 votes vote down vote up
def emit(events, stream=None, Dumper=Dumper,
        canonical=None, indent=None, width=None,
        allow_unicode=None, line_break=None):
    """
    Emit YAML parsing events into a stream.
    If stream is None, return the produced string instead.
    """
    getvalue = None
    if stream is None:
        stream = io.StringIO()
        getvalue = stream.getvalue
    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
            allow_unicode=allow_unicode, line_break=line_break)
    try:
        for event in events:
            dumper.emit(event)
    finally:
        dumper.dispose()
    if getvalue:
        return getvalue() 
Example 17
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bigip_ssl_certificate.py    MIT License 6 votes vote down vote up
def create_on_device(self):
        content = StringIO(self.want.content)
        self.client.api.shared.file_transfer.uploads.upload_stringio(
            content, self.want.filename
        )

        resource = self.client.api.tm.sys.file.ssl_certs.ssl_cert.create(
            sourcePath=self.want.source_path,
            name=self.want.filename,
            partition=self.want.partition
        )

        # This needs to be done because of the way that BIG-IP creates certificates.
        #
        # The extra params (such as OCSP and issuer stuff) are not available in the
        # payload. In a nutshell, the available resource attributes *change* after
        # a create so that *more* are available.
        params = self.want.api_params()
        if params:
            resource.update(**params) 
Example 18
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: bigip_iapp_template.py    MIT License 6 votes vote down vote up
def create_on_device(self):
        remote_path = "/var/config/rest/downloads/{0}".format(self.want.name)
        load_command = 'tmsh load sys application template {0}'.format(remote_path)

        template = StringIO(self.want.content)

        upload = self.client.api.shared.file_transfer.uploads
        upload.upload_stringio(template, self.want.name)
        output = self.client.api.tm.util.bash.exec_cmd(
            'run',
            utilCmdArgs='-c "{0}"'.format(load_command)
        )

        if hasattr(output, 'commandResult'):
            result = output.commandResult
            if 'Syntax Error' in result:
                raise F5ModuleError(output.commandResult) 
Example 19
Project: comport   Author: codeforamerica   File: models.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_denominator_csv(self):
        output = io.StringIO()

        writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)

        writer.writerow(["year", "month", "officers out on service"])

        values = sorted(self.denominator_values,
                        key=lambda x: (x.year, x.month))

        for value in values:
            row = [
                value.year,
                value.month,
                value.officers_out_on_service
            ]
            writer.writerow(row)

        return output.getvalue() 
Example 20
Project: fs_image   Author: facebookincubator   File: snapshot_repos.py    MIT License 5 votes vote down vote up
def _write_confs_get_repos(
    dest: Path, yum_conf_content: str, dnf_conf_content: str,
) -> Iterable[YumDnfConfRepo]:
    yum_dnf_repos = []
    for out_name, content in [
        ('yum.conf', yum_conf_content), ('dnf.conf', dnf_conf_content),
    ]:
        if content is not None:
            with create_ro(dest / out_name, 'w') as out:
                out.write(content)
            yum_dnf_repos.append(set(
                YumDnfConfParser(YumDnf.dnf, StringIO(content)).gen_repos()
            ))
    yum_repos, dnf_repos = yum_dnf_repos
    diff_repos = yum_repos.symmetric_difference(dnf_repos)
    if diff_repos:  # pragma: no cover
        # This is not allowed because `RpmActionItem` needs the package sets
        # to be the same for `yum` or `dnf`, since it uses the
        # `snapshot.sql3` DB to validate package names and determine
        # allowable versions (aka versionlock).
        #
        # We could potentially tag every `rpm` row with "dnf" or "yum" or
        # "both" to resolve this.  In that case, the right logic would be to
        # merge the repo lists here, and to check that `yum_dnf` column in
        # any queries from the compiler.  We really don't need this extra
        # complexity today.
        raise RuntimeError(
            f'`--yum-conf` and `--dnf-conf` had different repos {diff_repos}'
        )
    return dnf_repos 
Example 21
Project: fs_image   Author: facebookincubator   File: test_layer_mount_config.py    MIT License 5 votes vote down vote up
def test_error(self):
        out = StringIO()
        with self.assertRaisesRegex(RuntimeError, '`build_source` must not '):
            main(StringIO('{"build_source": "bad"}'), out, '//layer:path')
        self.assertEqual('', out.getvalue()) 
Example 22
Project: fs_image   Author: facebookincubator   File: test_layer_mount_config.py    MIT License 5 votes vote down vote up
def test_config_merging(self):
        out = StringIO()
        main(StringIO('{"runtime_source": "meow"}'), out, '//layer:path')
        self.assertEqual({
            'runtime_source': 'meow',
            'is_directory': True,
            'build_source': {'source': '//layer:path', 'type': 'layer'},
        }, json.loads(out.getvalue())) 
Example 23
Project: fs_image   Author: facebookincubator   File: test_subvolume_on_disk.py    MIT License 5 votes vote down vote up
def test_from_json_file_errors(self):
        with self.assertRaisesRegex(RuntimeError, 'Parsing subvolume JSON'):
            subvolume_on_disk.SubvolumeOnDisk.from_json_file(
                io.StringIO('invalid json'), '/subvols'
            )
        with self.assertRaisesRegex(RuntimeError, 'Parsed subvolume JSON'):
            subvolume_on_disk.SubvolumeOnDisk.from_json_file(
                io.StringIO('5'), '/subvols'
            ) 
Example 24
Project: mutatest   Author: EvanKepner   File: conftest.py    MIT License 5 votes vote down vote up
def stdoutIO():
    """Stdout redirection as a context manager to evaluating code mutations."""

    @contextlib.contextmanager
    def stdoutIO(stdout=None):
        old = sys.stdout
        if stdout is None:
            stdout = StringIO()
        sys.stdout = stdout
        yield stdout
        sys.stdout = old

    return stdoutIO 
Example 25
Project: mlbv   Author: kmac   File: mlbsession.py    GNU General Public License v3.0 5 votes vote down vote up
def is_logged_in(self):
        logged_in_url = "https://secure.mlb.com/account/login_register.jsp?flowId=registration.newsletter&c_id=mlb"
        content = self.session.get(logged_in_url).text
        parser = lxml.etree.HTMLParser()
        data = lxml.etree.parse(io.StringIO(content), parser)
        if "Login/Register" in data.xpath(".//title")[0].text:
            return False
        return True 
Example 26
Project: reelay-codegen   Author: doganulus   File: RegExpParser.py    GNU General Public License v3.0 5 votes vote down vote up
def serializedATN():
    with StringIO() as buf:
        buf.write("\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\30")
        buf.write("h\4\2\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\3\2")
        buf.write("\3\2\5\2\21\n\2\3\2\3\2\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3")
        buf.write("\3\3\3\3\3\3\3\3\3\3\3\3\3\5\3#\n\3\3\3\3\3\3\3\3\3\3")
        buf.write("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3")
        buf.write("\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\3\7")
        buf.write("\3D\n\3\f\3\16\3G\13\3\3\4\3\4\3\4\3\4\5\4M\n\4\3\5\3")
        buf.write("\5\3\5\3\5\3\5\3\5\3\5\5\5V\n\5\3\6\3\6\3\6\7\6[\n\6\f")
        buf.write("\6\16\6^\13\6\3\7\3\7\3\7\7\7c\n\7\f\7\16\7f\13\7\3\7")
        buf.write("\2\3\4\b\2\4\6\b\n\f\2\3\3\2\25\26\2s\2\20\3\2\2\2\4\"")
        buf.write("\3\2\2\2\6L\3\2\2\2\bU\3\2\2\2\nW\3\2\2\2\f_\3\2\2\2\16")
        buf.write("\17\7\25\2\2\17\21\7\3\2\2\20\16\3\2\2\2\20\21\3\2\2\2")
        buf.write("\21\22\3\2\2\2\22\23\5\4\3\2\23\3\3\2\2\2\24\25\b\3\1")
        buf.write("\2\25#\5\6\4\2\26\27\7\4\2\2\27\30\7\5\2\2\30\31\5\f\7")
        buf.write("\2\31\32\7\6\2\2\32\33\5\4\3\16\33#\3\2\2\2\34\35\7\7")
        buf.write("\2\2\35#\5\4\3\r\36\37\7\23\2\2\37 \5\4\3\2 !\7\24\2\2")
        buf.write("!#\3\2\2\2\"\24\3\2\2\2\"\26\3\2\2\2\"\34\3\2\2\2\"\36")
        buf.write("\3\2\2\2#E\3\2\2\2$%\f\t\2\2%&\7\13\2\2&D\5\4\3\n\'(\f")
        buf.write("\b\2\2()\7\f\2\2)D\5\4\3\t*+\f\4\2\2+,\7\22\2\2,D\5\4")
        buf.write("\3\5-.\f\f\2\2.D\7\b\2\2/\60\f\13\2\2\60D\7\t\2\2\61\62")
        buf.write("\f\n\2\2\62D\7\n\2\2\63\64\f\7\2\2\64\65\7\r\2\2\65\66")
        buf.write("\7\16\2\2\66\67\7\27\2\2\67D\7\17\2\289\f\6\2\29:\7\r")
        buf.write("\2\2:;\7\20\2\2;<\7\27\2\2<D\7\17\2\2=>\f\5\2\2>?\7\r")
        buf.write("\2\[email protected]\7\27\2\[email protected]\7\21\2\2AB\7\27\2\2BD\7\17\2\2C$\3\2")
        buf.write("\2\2C\'\3\2\2\2C*\3\2\2\2C-\3\2\2\2C/\3\2\2\2C\61\3\2")
        buf.write("\2\2C\63\3\2\2\2C8\3\2\2\2C=\3\2\2\2DG\3\2\2\2EC\3\2\2")
        buf.write("\2EF\3\2\2\2F\5\3\2\2\2GE\3\2\2\2HM\5\b\5\2IJ\7\25\2\2")
        buf.write("JK\7\16\2\2KM\5\b\5\2LH\3\2\2\2LI\3\2\2\2M\7\3\2\2\2N")
        buf.write("V\t\2\2\2OP\t\2\2\2PQ\7\23\2\2QR\5\n\6\2RS\7\24\2\2SV")
        buf.write("\3\2\2\2TV\7\27\2\2UN\3\2\2\2UO\3\2\2\2UT\3\2\2\2V\t\3")
        buf.write("\2\2\2W\\\5\b\5\2XY\7\21\2\2Y[\5\b\5\2ZX\3\2\2\2[^\3\2")
        buf.write("\2\2\\Z\3\2\2\2\\]\3\2\2\2]\13\3\2\2\2^\\\3\2\2\2_d\7")
        buf.write("\25\2\2`a\7\21\2\2ac\7\25\2\2b`\3\2\2\2cf\3\2\2\2db\3")
        buf.write("\2\2\2de\3\2\2\2e\r\3\2\2\2fd\3\2\2\2\n\20\"CELU\\d")
        return buf.getvalue() 
Example 27
Project: pyblish-win   Author: pyblish   File: test_memoryio.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_issue5265(self):
        # StringIO can duplicate newlines in universal newlines mode
        memio = self.ioclass("a\r\nb\r\n", newline=None)
        self.assertEqual(memio.read(5), "a\nb\n")
        self.assertEqual(memio.getvalue(), "a\nb\n") 
Example 28
Project: pyblish-win   Author: pyblish   File: test_memoryio.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __new__(cls, *args, **kwargs):
            return pickle.loads(pickle.dumps(pyio.StringIO(*args, **kwargs))) 
Example 29
Project: pyblish-win   Author: pyblish   File: test_memoryio.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __new__(cls, *args, **kwargs):
            return pickle.loads(pickle.dumps(io.StringIO(*args, **kwargs),
                                             protocol=2)) 
Example 30
Project: xadmin_bugfix   Author: vip68   File: export.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_xml_export(self, context):
        results = self._get_objects(context)
        stream = io.StringIO()

        xml = SimplerXMLGenerator(stream, "utf-8")
        xml.startDocument()
        xml.startElement("objects", {})

        self._to_xml(xml, results)

        xml.endElement("objects")
        xml.endDocument()

        return stream.getvalue().split('\n')[1] 
Example 31
Project: logging-test-case   Author: chadrosenquist   File: capturelogs_test.py    MIT License 5 votes vote down vote up
def _set_stream_handler(cls):
        foo_logger = logging.getLogger('foo')
        stream = StringIO()
        stream_handler = logging.StreamHandler(stream)
        stream_formatter = logging.Formatter("%(levelname)s:%(name)s:%(message)s")
        stream_handler.setFormatter(stream_formatter)
        foo_logger.addHandler(stream_handler)

        return stream 
Example 32
Project: sanctuary   Author: bzamecnik   File: capture.py    MIT License 5 votes vote down vote up
def __enter__(self):
        self.orig_stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self 
Example 33
Project: Mzo-Cli   Author: jamesstidard   File: csv.py    MIT License 5 votes vote down vote up
def dumps(dicts, header=True):
    output = io.StringIO()
    headers = OrderedSet(k for d in dicts for k in d.keys())

    writer = csv.DictWriter(output, fieldnames=headers)
    if header:
        writer.writeheader()
    writer.writerows(dicts)

    return output.getvalue() 
Example 34
Project: django-xadmin   Author: MarkHoo   File: export.py    MIT License 5 votes vote down vote up
def get_xml_export(self, context):
        results = self._get_objects(context)
        stream = io.StringIO()

        xml = SimplerXMLGenerator(stream, "utf-8")
        xml.startDocument()
        xml.startElement("objects", {})

        self._to_xml(xml, results)

        xml.endElement("objects")
        xml.endDocument()

        return stream.getvalue().split('\n')[1] 
Example 35
Project: django-xadmin   Author: MarkHoo   File: export.py    MIT License 5 votes vote down vote up
def get_xml_export(self, context):
        results = self._get_objects(context)
        stream = io.StringIO()

        xml = SimplerXMLGenerator(stream, "utf-8")
        xml.startDocument()
        xml.startElement("objects", {})

        self._to_xml(xml, results)

        xml.endElement("objects")
        xml.endDocument()

        return stream.getvalue().split('\n')[1] 
Example 36
Project: django-xadmin   Author: MarkHoo   File: export.py    MIT License 5 votes vote down vote up
def get_xml_export(self, context):
        results = self._get_objects(context)
        stream = io.StringIO()

        xml = SimplerXMLGenerator(stream, "utf-8")
        xml.startDocument()
        xml.startElement("objects", {})

        self._to_xml(xml, results)

        xml.endElement("objects")
        xml.endDocument()

        return stream.getvalue().split('\n')[1] 
Example 37
Project: fortran_input_reader   Author: miroi   File: update.py    MIT License 5 votes vote down vote up
def parse_cmake_module(s_in, defaults={}):

    parsed_config = collections.defaultdict(lambda: None)

    if 'autocmake.cfg configuration::' not in s_in:
        return parsed_config

    s_out = []
    is_rst_line = False
    for line in s_in.split('\n'):
        if is_rst_line:
            if len(line) > 0:
                if line[0] != '#':
                    is_rst_line = False
            else:
                is_rst_line = False
        if is_rst_line:
            s_out.append(line[2:])
        if '#.rst:' in line:
            is_rst_line = True

    autocmake_entry = '\n'.join(s_out).split('autocmake.cfg configuration::')[1]
    autocmake_entry = autocmake_entry.replace('\n  ', '\n')

    # we prepend a fake section heading so that we can parse it with configparser
    autocmake_entry = '[foo]\n' + autocmake_entry

    buf = StringIO(autocmake_entry)
    config = ConfigParser(dict_type=collections.OrderedDict)
    config.readfp(buf)

    for section in config.sections():
        for s in ['docopt', 'define', 'export', 'fetch', 'warning']:
            if config.has_option(section, s):
                parsed_config[s] = config.get(section, s, 0, defaults)

    return parsed_config

# ------------------------------------------------------------------------------ 
Example 38
Project: auto-check-in   Author: zeekvfu   File: taobao_taojinbi.py    GNU General Public License v3.0 5 votes vote down vote up
def parse_shop_url_list(self, content):
        this_func_name = sys._getframe().f_code.co_name
        self.logger.debug("%s(): start ..." % this_func_name)
        parser = etree.HTMLParser()
        tree = etree.parse(StringIO(content), parser)
        urls = tree.xpath('//div[@class="content"]/div[@class="item"]/a[@class="item-link"]/@href')
        urls = [ 'https:' + url for url in  urls ]
        self.logger.debug("%s(): urls\t%s" % (this_func_name, urls))
        self.logger.debug("%s(): end ..." % this_func_name)
        return urls


    # 店铺签到 
Example 39
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: parser_lib.py    Apache License 2.0 5 votes vote down vote up
def parse_text(self,conllu):
    self.network.current_input=io.StringIO(self.network.dummy_sents_hack()+conllu)
    return self.parse_generator.__next__()

#  -------------------- 
Example 40
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: parser_lib.py    Apache License 2.0 5 votes vote down vote up
def txt_to_conllu(sentences):
  """Just a quick dummy to turn list of whitespace-tokenized sentences into conllu"""
  buff=io.StringIO()
  for s in sentences:
    for idx,wrd in enumerate(s.split()):
      print(idx+1,wrd,"_","_","_","_","_","_","_","_",sep="\t",file=buff)
    print(file=buff)
  print("ENTERS PARSER",repr(buff.getvalue()),file=sys.stderr)
  return buff.getvalue() 
Example 41
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: conll18_ud_eval.py    Apache License 2.0 5 votes vote down vote up
def _load_words(words):
        """Prepare fake CoNLL-U files with fake HEAD to prevent multiple roots errors."""
        lines, num_words = [], 0
        for w in words:
            parts = w.split(" ")
            if len(parts) == 1:
                num_words += 1
                lines.append("{}\t{}\t_\t_\t_\t_\t{}\t_\t_\t_".format(num_words, parts[0], int(num_words>1)))
            else:
                lines.append("{}-{}\t{}\t_\t_\t_\t_\t_\t_\t_\t_".format(num_words + 1, num_words + len(parts) - 1, parts[0]))
                for part in parts[1:]:
                    num_words += 1
                    lines.append("{}\t{}\t_\t_\t_\t_\t{}\t_\t_\t_".format(num_words, part, int(num_words>1)))
        return load_conllu((io.StringIO if sys.version_info >= (3, 0) else io.BytesIO)("\n".join(lines+["\n"]))) 
Example 42
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, response):
		# response doesn't support tell() and read(), required by
		# GzipFile
		if not gzip:
			raise SpeedtestHTTPError('HTTP response body is gzip encoded, '
									 'but gzip support is not available')
		IO = BytesIO or StringIO
		self.io = IO()
		while 1:
			chunk = response.read(1024)
			if len(chunk) == 0:
				break
			self.io.write(chunk)
		self.io.seek(0)
		gzip.GzipFile.__init__(self, mode='rb', fileobj=self.io) 
Example 43
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py    Apache License 2.0 5 votes vote down vote up
def _create_data(self):
		chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
		multiplier = int(round(int(self.length) / 36.0))
		IO = BytesIO or StringIO
		self._data = IO(
			('content1=%s' %
			 (chars * multiplier)[0:int(self.length) - 9]
			 ).encode()
		) 
Example 44
Project: L.E.S.M.A   Author: NatanaelAntonioli   File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py    Apache License 2.0 5 votes vote down vote up
def csv(self, delimiter=','):
		"""Return data in CSV format"""

		data = self.dict()
		out = StringIO()
		writer = csv.writer(out, delimiter=delimiter, lineterminator='')
		writer.writerow([data['server']['id'], data['server']['sponsor'],
						 data['server']['name'], data['timestamp'],
						 data['server']['d'], data['ping'], data['download'],
						 data['upload']])
		return out.getvalue() 
Example 45
Project: neural-fingerprinting   Author: StephanZheng   File: work_data.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __str__(self):
    buf = StringIO()
    buf.write(u'WorkType "{0}"\n'.format(self._work_type_entity_id))
    for idx, (work_id, work_val) in enumerate(iteritems(self.work)):
      if idx >= TO_STR_MAX_WORK:
        buf.write(u'  ...\n')
        break
      buf.write(u'  Work "{0}"\n'.format(work_id))
      buf.write(u'    {0}\n'.format(str(work_val)))
    return buf.getvalue() 
Example 46
Project: neural-fingerprinting   Author: StephanZheng   File: classification_results.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __str__(self):
    """Returns human readable string representation, useful for debugging."""
    buf = StringIO()
    for idx, (class_batch_id, class_val) in enumerate(iteritems(self.data)):
      if idx >= TO_STR_MAX_BATCHES:
        buf.write(u'  ...\n')
        break
      buf.write(u'  ClassBatch "{0}"\n'.format(class_batch_id))
      buf.write(u'    {0}\n'.format(str(class_val)))
    return buf.getvalue() 
Example 47
Project: neural-fingerprinting   Author: StephanZheng   File: fake_cloud_client.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __str__(self):
    """Returns string representation of all stored entities."""
    buf = StringIO()
    for entity in self.entities.values():
      buf.write(u'Entity {0}:\n'.format(entity.key.flat_path))
      buf.write(u'    {0}\n'.format(dict(entity)))
    return buf.getvalue() 
Example 48
Project: ticket_universe   Author: lotify   File: cli_test.py    MIT License 5 votes vote down vote up
def capture(command, *args, **kwargs):
    out, sys.stdout = sys.stdout, io.StringIO()
    try:
        command(*args, **kwargs)
        sys.stdout.seek(0)
        yield sys.stdout.read()
    finally:
        sys.stdout = out 
Example 49
Project: sphinx-quant   Author: sphinx-quant   File: tasks.py    MIT License 5 votes vote down vote up
def capture():
    import sys
    from io import StringIO

    oldout, olderr = sys.stdout, sys.stderr
    try:
        out = [StringIO(), StringIO()]
        sys.stdout, sys.stderr = out
        yield out
    finally:
        sys.stdout, sys.stderr = oldout, olderr
        out[0] = out[0].getvalue()
        out[1] = out[1].getvalue() 
Example 50
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: __init__.py    MIT License 5 votes vote down vote up
def serialize_all(nodes, stream=None, Dumper=Dumper,
        canonical=None, indent=None, width=None,
        allow_unicode=None, line_break=None,
        encoding=None, explicit_start=None, explicit_end=None,
        version=None, tags=None):
    """
    Serialize a sequence of representation trees into a YAML stream.
    If stream is None, return the produced string instead.
    """
    getvalue = None
    if stream is None:
        if encoding is None:
            stream = io.StringIO()
        else:
            stream = io.BytesIO()
        getvalue = stream.getvalue
    dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
            allow_unicode=allow_unicode, line_break=line_break,
            encoding=encoding, version=version, tags=tags,
            explicit_start=explicit_start, explicit_end=explicit_end)
    try:
        dumper.open()
        for node in nodes:
            dumper.serialize(node)
        dumper.close()
    finally:
        dumper.dispose()
    if getvalue:
        return getvalue()