Python io.StringIO() Examples

The following are 30 code examples for showing how to use io.StringIO(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module io , or try the search function .

Example 1
Project: vergeml   Author: mme   File: help.py    License: MIT License 7 votes vote down vote up
def format_source_or_operation(self, name, group, what):
        op = what.discover(self.plugins.get(group, name))
        buffer = io.StringIO()
        descr = op.long_descr or op.descr
        if descr:
            print(descr, file=buffer)
        else:
            print(name, file=buffer)
        print("", file=buffer)
        options = op.options
        if options:
            print("Options:", file=buffer)
            for opt in options:
                print("", file=buffer)
                tp_descr = opt.human_type()
                if tp_descr:
                    print(f"{opt.name}: {tp_descr}", file=buffer)
                else:
                    print(f"{opt.name}", file=buffer)
                if opt.descr:
                    print(format_info_text(opt.descr, indent=2), file=buffer)
        return buffer.getvalue().strip() 
Example 2
Project: robosuite   Author: StanfordVL   File: base.py    License: MIT License 7 votes vote down vote up
def get_model(self, mode="mujoco_py"):
        """
        Returns a MjModel instance from the current xml tree.
        """

        available_modes = ["mujoco_py"]
        with io.StringIO() as string:
            string.write(ET.tostring(self.root, encoding="unicode"))
            if mode == "mujoco_py":
                from mujoco_py import load_model_from_xml

                model = load_model_from_xml(string.getvalue())
                return model
            raise ValueError(
                "Unkown model mode: {}. Available options are: {}".format(
                    mode, ",".join(available_modes)
                )
            ) 
Example 3
Project: cherrypy   Author: cherrypy   File: profiler.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def stats(self, filename, sortby='cumulative'):
        """:rtype stats(index): output of print_stats() for the given profile.
        """
        sio = io.StringIO()
        if sys.version_info >= (2, 5):
            s = pstats.Stats(os.path.join(self.path, filename), stream=sio)
            s.strip_dirs()
            s.sort_stats(sortby)
            s.print_stats()
        else:
            # pstats.Stats before Python 2.5 didn't take a 'stream' arg,
            # but just printed to stdout. So re-route stdout.
            s = pstats.Stats(os.path.join(self.path, filename))
            s.strip_dirs()
            s.sort_stats(sortby)
            oldout = sys.stdout
            try:
                sys.stdout = sio
                s.print_stats()
            finally:
                sys.stdout = oldout
        response = sio.getvalue()
        sio.close()
        return response 
Example 4
Project: django-template   Author: liip   File: fixturize.py    License: 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 5
Project: django-template   Author: liip   File: fabfile.py    License: 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 6
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: wstokenizer_mod.py    License: 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 7
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: wipe_mod.py    License: 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 8
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: trim_to_max_mod.py    License: 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 9
Project: Turku-neural-parser-pipeline   Author: TurkuNLP   File: regextokenizer_mod.py    License: 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 10
Project: neural-fingerprinting   Author: StephanZheng   File: image_batches.py    License: 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 11
Project: multibootusb   Author: mbusb   File: mbusb_gui.py    License: GNU General Public License v2.0 6 votes vote down vote up
def install_syslinux(self):
        try:
            try:
                self.install_syslinux_impl()
            finally:
                config.process_exist = None
                self.ui_enable_controls()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            uninstall_distro.do_uninstall_distro(
                config.distro, iso_basename(config.image_path))
            o = io.StringIO()
            traceback.print_exc(None, o)
            QtWidgets.QMessageBox.information(
                self, 'install_syslinux() failed',
                o.getvalue())
            log("install_syslinux() failed.")
            log(o.getvalue()) 
Example 12
Project: CAMISIM   Author: CAMI-challenge   File: configparserwrapper.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, logfile=None, verbose=True):
        """
            Wrapper for the SafeConfigParser class for easy use.

            @attention: config_file argument may be file path or stream.

            @param logfile: file handler or file path to a log file
            @type logfile: file | FileIO | StringIO | None
            @param verbose: No stdout or stderr messages. Warnings and errors will be only logged to a file, if one is given
            @type verbose: bool

            @return: None
            @rtype: None
        """
        super(ConfigParserWrapper, self).__init__(
            label="ConfigParserWrapper", logfile=logfile, verbose=verbose)
        self._config = ConfigParser()
        self._config_file_path = None 
Example 13
Project: comport   Author: codeforamerica   File: models.py    License: 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 14
Project: comport   Author: codeforamerica   File: test_department_model_lmpd.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_csv_response(self, testapp):
        # create a department and an LMPD uof incident
        department = Department.create(name="LM Police Department", short_name="LMPD", load_defaults=False)

        uof_check = dict(department_id=department.id, opaque_id="Check Opaque ID", occured_date=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), bureau="Check Bureau", division="Check Division", unit="Check Unit", platoon="Check Platoon", disposition="Check Disposition", use_of_force_reason="Check UOF Reason", officer_force_type="Check Officer Force Type", service_type="Check Service Type", arrest_made=False, arrest_charges="Check Arrest Charges", resident_injured=True, resident_hospitalized=False, resident_condition="Check Resident Condition", officer_injured=False, officer_hospitalized=False, officer_condition="Check Officer Condition", resident_identifier="Check Resident Identifier", resident_race="Check Resident Race", resident_sex="Check Resident Sex", resident_age="Check Resident Age", officer_race="Check Officer Race", officer_sex="Check Officer Sex", officer_age="Check Officer Age", officer_years_of_service="Check Officer Years Of Service", officer_identifier="Check Officer Identifier")

        UseOfForceIncidentLMPD.create(**uof_check)

        response = testapp.get("/department/{}/uof.csv".format(department.id))

        incidents = list(csv.DictReader(io.StringIO(response.text)))

        # build a variable to csv header lookup from the csv schema
        csv_schema = UseOfForceIncidentLMPD.get_csv_schema()
        schema_lookup = dict(zip([col[1] for col in csv_schema], [col[0] for col in csv_schema]))

        assert len(incidents) == 1
        for check_key in uof_check.keys():
            if check_key == 'department_id':
                continue
            assert str(uof_check[check_key]) == incidents[0][schema_lookup[check_key]] 
Example 15
Project: comport   Author: codeforamerica   File: test_functional.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_csv_filtered_by_dept(self, testapp):
        # create a department
        department1 = Department.create(name="IM Police Department", short_name="IMPD", load_defaults=False)
        department2 = Department.create(name="B Police Department", short_name="BPD", load_defaults=False)

        incidentclass1 = getattr(importlib.import_module("comport.data.models"), "UseOfForceIncident{}".format(department1.short_name))
        incidentclass2 = getattr(importlib.import_module("comport.data.models"), "UseOfForceIncident{}".format(department2.short_name))

        incidentclass1.create(opaque_id="123ABC", department_id=department1.id)
        incidentclass2.create(opaque_id="123XYZ", department_id=department2.id)

        response1 = testapp.get("/department/{}/uof.csv".format(department1.id))
        response2 = testapp.get("/department/{}/uof.csv".format(department2.id))

        incidents1 = list(csv.DictReader(io.StringIO(response1.text)))
        incidents2 = list(csv.DictReader(io.StringIO(response2.text)))

        assert len(incidents1) == 1 and len(incidents2) == 1
        assert incidents1[0]['id'] == '123ABC' and incidents2[0]['id'] == '123XYZ' 
Example 16
Project: mutatest   Author: EvanKepner   File: conftest.py    License: 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 17
Project: vergeml   Author: mme   File: display.py    License: MIT License 5 votes vote down vote up
def _append_line(self):
        self.lines.append(io.StringIO(str(" " * self.cols))) 
Example 18
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def format_glossary(self):
        buffer = io.StringIO()
        for k, v in sorted(glossary.LONG_DESCR.items()):
            print(f"{k}:", file=buffer)
            print(format_info_text(v, indent=2), file=buffer)
            print("", file=buffer)
        return buffer.getvalue().strip() 
Example 19
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def format_general_help(self, env=None, short=False):
        buffer = io.StringIO()
        print(USAGE, file=buffer)
        print("", file=buffer)

        terms = set(glossary.LONG_DESCR.keys())
        terms.update(self.plugins.keys('vergeml.cmd'))
        rng = random.Random()
        rng.seed(datetime.datetime.now())
        random_term = rng.choice(list(terms))

        if env and env.model_plugin:
            print(f"Current Model: {env.get('model')}", file=buffer)
            print("", file=buffer)

        print("General Help:", file=buffer)
        for topic, descr in _GENERAL_HELP:
            print("  {:<16} {}".format(topic, descr), file=buffer)
        print("", file=buffer)

        print("Commands:", file=buffer)
        for cmd_name in self.plugins.keys('vergeml.cmd'):
            descr = Command.discover(self.plugins.get('vergeml.cmd', cmd_name)).descr
            print("  {:<16} {}".format(cmd_name, descr), file=buffer)
        print("", file=buffer)

        if env and env.model_plugin:
            print("Model Commands:", file=buffer)
            for fn in Command.find_functions(env.model_plugin):
                cmd = Command.discover(fn)
                print("  {:<16} {}".format(cmd.name, cmd.descr), file=buffer)
            print("", file=buffer)

        if not short:
            print("See 'ml help <command>' or 'ml help <topic>' to read about a specific subcommand or topic.", file=buffer)
            print(f"For example, try 'ml help {random_term}'", file=buffer)

        return buffer.getvalue().strip() 
Example 20
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def format_options(self):
        buffer = io.StringIO()
        print(USAGE, file=buffer)
        print("", file=buffer)
        for k, v in HELP_OPTIONS:
            print("{:<18} {}".format(k, v), file=buffer)
        return buffer.getvalue().strip() 
Example 21
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def format_input_list(self):
        buffer = io.StringIO()
        print("Controls how your data is loaded. Possible values for data.input are:", file=buffer)
        print("", file=buffer)
        inputs = []
        for k in self.plugins.keys('vergeml.io'):
            plugin = self.plugins.get('vergeml.io', k)
            source = Source.discover(plugin)
            inputs.append((k, source.descr))

        print(_get_table(inputs), file=buffer)
        print("", file=buffer)
        print("For more help on a particular input, see 'ml help <input-name>.'", file=buffer)
        return buffer.getvalue().strip() 
Example 22
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def format_preprocessing_list(self):
        buffer = io.StringIO()

        cmd = Command.discover(PreprocessCommand)
        print(cmd.usage(), file=buffer)
        print("", file=buffer)
        print("Configuration:", file=buffer)

        print(format_info_text(_PREPROCESS_HELP.strip(), indent=2), file=buffer)
        print("", file=buffer)

        ops = {}
        for k in self.plugins.keys('vergeml.operation'):
            plugin = self.plugins.get('vergeml.operation', k)
            op = Operation.discover(plugin)
            topic = op.topic or "general"
            descr = op.descr
            ops.setdefault(topic, [])
            ops[topic].append((k, descr))

        for k, v in sorted(ops.items()):
            topic = k.capitalize()
            print(f"{topic} Operations:", file=buffer)
            print(format_info_text(_get_table(v), indent=2), file=buffer)
            print("", file=buffer)

        return buffer.getvalue().strip() 
Example 23
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def _get_table(table, SPACE=4, IND=0, colon=True):
    buffer = io.StringIO()
    max_name = max(map(lambda o: len(o[0]), table))
    for name, descr in sorted(table):
        if descr:
            nspace = (max_name + SPACE) - len(name)
            space = str(nspace * ' ')
            col = ":" if colon else ""
            ind = str(" "*IND)
            print(f"{ind}{name}{col}{space}{descr}", file=buffer)
        else:
            print(name, file=buffer)
    return buffer.getvalue().rstrip() 
Example 24
Project: vergeml   Author: mme   File: help.py    License: MIT License 5 votes vote down vote up
def _with_header(content, help, topic):
    buffer = io.StringIO()
    print(f"HELP:  *{help}*", file=buffer)
    print(f"TOPIC: {topic}", file=buffer)
    print("", file=buffer)
    print(content, file=buffer)
    return buffer.getvalue().strip() 
Example 25
Project: vergeml   Author: mme   File: ls.py    License: MIT License 5 votes vote down vote up
def _output_table(output, theader, tdata, left_align):

    if not tdata:
        print("No matching trained models found.", file=sys.stderr)

    if output == 'table':
        if not tdata:
            return
        tdata.insert(0, theader)
        print(DISPLAY.table(tdata, left_align=left_align).getvalue(fit=True))

    elif output == 'json':
        res = []
        for row in tdata:
            res.append(dict(zip(theader, row)))
        print(json.dumps(res))

    elif output == 'csv':
        buffer = io.StringIO()

        writer = csv.writer(buffer)
        writer.writerow(theader)
        for row in tdata:
            writer.writerow(row)
        val = buffer.getvalue()
        val = val.replace('\r', '')
        print(val.strip()) 
Example 26
Project: vergeml   Author: mme   File: libraries.py    License: MIT License 5 votes vote down vote up
def devices_info():
        res = []
        devices = CudaLibrary.devices()
        for device in devices:
            buffer = io.StringIO()
            print("Device ID:            ", device['id'], file=buffer)
            if 'name' in device:
                print("Device Name:          ", device['name'], file=buffer)
            if 'compute_capability' in device:
                print("Compute Capability:   ", "%d.%d" % device['compute_capability'], file=buffer)
            if 'multiprocessors' in device:
                print("Multiprocessors:      ", device['multiprocessors'], file=buffer)
            if 'cuda_cores' in device:
                print("CUDA Cores:           ", device['cuda_cores'], file=buffer)
            if 'concurrent_threads' in device:
                print("Concurrent Threads:   ", device['concurrent_threads'], file=buffer)
            if 'gpu_clock_mhz' in device:
                print("GPU Clock:            ", "%g MHz" % device['gpu_clock_mhz'], file=buffer)
            if 'memory_clock_mhz' in device:
                print("Memory Clock:         ", "%g MHz" % device['memory_clock_mhz'], file=buffer)
            if 'total_memory_mib' in device:
                print("Total Memory:         ", "%ld MiB" % device['total_memory_mib'], file=buffer)
            if 'free_memory_mib' in device:
                print("Free Memory:          ", "%ld MiB" % device['free_memory_mib'], file=buffer)
            res.append(buffer.getvalue())
        return res 
Example 27
Project: mlbv   Author: kmac   File: mlbsession.py    License: 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 28
Project: ALF   Author: blackberry   File: _qemu.py    License: Apache License 2.0 5 votes vote down vote up
def loads(data, from_module=None):
    up = pickle.Unpickler(StringIO(data))
    def find_global(module, cls):
        if module == "copy_reg" and cls == "_reconstructor":
            return copy_reg._reconstructor
        if module == "__builtin__":
            return getattr(__builtins__, cls)
        if from_module is not None:
            return getattr(from_module, cls)
        return globals()[cls]
    up.find_global = find_global
    return up.load() 
Example 29
Project: wechatpy   Author: wechatpy   File: test_work_client.py    License: MIT License 5 votes vote down vote up
def test_upload_media(self):
        media_file = io.StringIO("nothing")
        with HTTMock(wechat_api_mock):
            media = self.client.media.upload("image", media_file)
            self.assertEqual("image", media["type"])
            self.assertEqual("12345678", media["media_id"]) 
Example 30
Project: wechatpy   Author: wechatpy   File: test_client.py    License: MIT License 5 votes vote down vote up
def test_upload_media(self):
        media_file = io.StringIO("nothing")
        with HTTMock(wechat_api_mock):
            media = self.client.media.upload("image", media_file)
            self.assertEqual("image", media["type"])
            self.assertEqual("12345678", media["media_id"])