Python io.StringIO() Examples
The following are 30
code examples of io.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
io
, or try the search function
.
Example #1
Source File: models.py From comport with BSD 3-Clause "New" or "Revised" License | 9 votes |
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 #2
Source File: help.py From vergeml with MIT License | 7 votes |
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 #3
Source File: base.py From robosuite with MIT License | 7 votes |
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 #4
Source File: trim_to_max_mod.py From Turku-neural-parser-pipeline with Apache License 2.0 | 6 votes |
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 #5
Source File: profiler.py From cherrypy with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #6
Source File: test_department_model_lmpd.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #7
Source File: test_functional.py From comport with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #8
Source File: wipe_mod.py From Turku-neural-parser-pipeline with Apache License 2.0 | 6 votes |
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 #9
Source File: mbusb_gui.py From multibootusb with GNU General Public License v2.0 | 6 votes |
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 #10
Source File: regextokenizer_mod.py From Turku-neural-parser-pipeline with Apache License 2.0 | 6 votes |
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 #11
Source File: wstokenizer_mod.py From Turku-neural-parser-pipeline with Apache License 2.0 | 6 votes |
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
Source File: fabfile.py From django-template with MIT License | 6 votes |
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 #13
Source File: fixturize.py From django-template with MIT License | 6 votes |
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 #14
Source File: image_batches.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
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 #15
Source File: configparserwrapper.py From CAMISIM with Apache License 2.0 | 6 votes |
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 #16
Source File: base.py From robosuite with MIT License | 5 votes |
def get_xml(self): """ Returns a string of the MJCF XML file. """ with io.StringIO() as string: string.write(ET.tostring(self.root, encoding="unicode")) return string.getvalue()
Example #17
Source File: configparserwrapper.py From CAMISIM with Apache License 2.0 | 5 votes |
def read(self, config_file): """ Read a configuration file in ini format @attention: config_file argument may be file path or stream. @param config_file: file handler or file path to a config file @type config_file: file | FileIO | StringIO @rtype: None """ assert isinstance(config_file, str) or self.is_stream(config_file), "Invalid config file path: {}".format(config_file) if isinstance(config_file, str) and not os.path.isfile(config_file): self._logger.error("Config file does not exist: '{}'".format(config_file)) raise Exception("File does not exist") if isinstance(config_file, str): self._config.read(config_file) self._config_file_path = config_file elif self.is_stream(config_file): if sys.version_info < (3,): self._config.readfp(config_file) else: self._config.read_file(config_file) self._config_file_path = config_file.name else: self._logger.error("Invalid config file argument '{}'".format(config_file)) raise Exception("Unknown argument")
Example #18
Source File: repl.py From Dumb-Cogs with MIT License | 5 votes |
def _call_catch_fmt(call, limit=None): stdout = io.StringIO() try: with redirect_stdout(stdout): result = call() except Exception as e: value = stdout.getvalue() fmt = '{}{}'.format(value, traceback.format_exc(limit)) else: value = stdout.getvalue() fmt = (value or '') + ('' if result is None else result) return fmt
Example #19
Source File: rnaseq_ebi.py From bioservices with GNU General Public License v3.0 | 5 votes |
def tsv_parser(data): return pd.read_csv(StringIO(data), sep='\t')
Example #20
Source File: uniprot.py From bioservices with GNU General Public License v3.0 | 5 votes |
def uniref(self, query): """Calls UniRef service :return: if you have Pandas installed, returns a dataframe (see example) :: >>> u = UniProt() >>> df = u.uniref("member:Q03063") # of just A03063 >>> df.Size Another example from https://github.com/cokelaer/bioservices/issues/121 is the combination of uniprot and uniref filters:: u.uniref("uniprot:(ec:1.1.1.282 taxonomy:bacteria reviewed:yes) AND identity:0.5") """ try: import pandas as pd except: print("uniref method requires Pandas", file=sys.stderr) return res = self.http_get("uniref/", params={"query": query, 'format': 'tab'}, frmt="txt") try: # python 2.X res = pd.read_csv(io.StringIO(unicode(res)), sep="\t") except: res = pd.read_csv(io.StringIO(str(res.strip())), sep="\t") return res
Example #21
Source File: biodbnet.py From bioservices with GNU General Public License v3.0 | 5 votes |
def dbReport(self, input_db, input_values, taxon=9606): """Same as :meth:`db2db` but returns results for all possible outputs. :param input_db: input database :param input_values: list of identifiers to retrieve :return: dataframe where index correspond to the input database identifiers. The columns contains the identifiers for each output database (see example here below) :: df = s.dbReport("Ensembl Gene ID", ['ENSG00000121410', 'ENSG00000171428']) """ self._check_db(input_db) # This also check that the outputs exist and are compatible with the # input. url = self.url + "?method=dbreport" url += "&input={}".format(input_db) url += "&inputValues={}".format(self._list_to_string(input_values)) url += "&taxonId={}".format(taxon) url += "&format={}".format("row") request = self.http_get(url) try: # TODO can be removed in v2 df = pd.DataFrame(request) df.set_index("InputValue", inplace=True) df.index.name = input_db return df except Exception as err: self.logging.error(err) return request inputValues = self._interpret_input_db(inputValues) # df = pd.readcsv(io.StringIO(res.strip()), sep="\t")
Example #22
Source File: BitVector.py From knob with MIT License | 5 votes |
def write_bits_to_stream_object_old( self, fp ): ''' You can write a bitvector directly to a stream object, as illustrated by: fp_write = io.StringIO() bitvec.write_bits_to_stream_object(fp_write) print(fp_write.getvalue()) This method does not return anything. This function is meant to write a bitvector directly to a file like object. Note that whereas 'write_to_file' method creates a memory footprint that corresponds exactly to the bitvector, the 'write_bits_to_stream_object' actually writes out the 1's and 0's as individual items to the file object. That makes this method convenient for creating a string representation of a bitvector, especially if you use the StringIO class, as shown in the test code. ''' for bit_index in range(self.size): if sys.version_info[0] == 3: if self[bit_index] == 0: fp.write( str('0') ) else: fp.write( str('1') ) else: if self[bit_index] == 0: fp.write( unicode('0') ) else: fp.write( unicode('1') )
Example #23
Source File: test_csv2json.py From csv2json with Apache License 2.0 | 5 votes |
def test_insert_headers(self): expected_output = [{'column1': 'value1', 'column2': 'value2'}, {'column1': 'value3', 'column2': 'value4'}] csv_fp = io.StringIO(self.csv_input) json_fp = io.StringIO() convert(csv_fp, json_fp, delimiter=self.delimiter, custom_headers=self.custom_headers) converted_json_dct = json.loads(json_fp.getvalue()) self.assertEqual(expected_output, converted_json_dct) json_fp.close() csv_fp.close()
Example #24
Source File: test_csv2json.py From csv2json with Apache License 2.0 | 5 votes |
def test_simple(self): expected_output = [{'column1': 'not_empty', 'column2': ''}] csv_fp = io.StringIO(self.csv_input) json_fp = io.StringIO() convert(csv_fp, json_fp, delimiter=self.delimiter) converted_json_dct = json.loads(json_fp.getvalue()) self.assertEqual(expected_output, converted_json_dct) json_fp.close() csv_fp.close()
Example #25
Source File: tasks.py From sphinx-quant with MIT License | 5 votes |
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 #26
Source File: mbusb_gui.py From multibootusb with GNU General Public License v2.0 | 5 votes |
def onFsckClick(self, fsck_func): try: self.onFsckClick_impl(fsck_func) except (KeyboardInterrupt, SystemExit): raise except: o = io.StringIO() traceback.print_exc(None, o) QtWidgets.QMessageBox.information( self, 'Failed to run fsck', o.getvalue())
Example #27
Source File: mbusb_gui.py From multibootusb with GNU General Public License v2.0 | 5 votes |
def onComboChange(self): """ Detects and updates GUI with populated USB device details. :return: """ self.ui.installed_distros.clear() config.usb_disk = osdriver.listbox_entry_to_device( self.ui.combo_drives.currentText()) if config.usb_disk == 0 or config.usb_disk: # Get the GPT status of the disk and store it on a variable try: usb.gpt_device(config.usb_disk) config.imager_usb_disk \ = self.ui.combo_drives.currentText() config.usb_details \ = usb.details(config.usb_disk) except Exception as e: o = io.StringIO() traceback.print_exc(None, o) log(o.getvalue()) QtWidgets.QMessageBox.critical( self, "The disk/partition is not usable.", str(e)) self.ui.combo_drives.setCurrentIndex(0) # Above statement triggers call to this method. return log("Selected device " + osdriver.usb_disk_desc(config.usb_disk)) self.update_target_info() self.update_list_box(config.usb_disk) self.ui_update_persistence() else: self.ui.usb_dev.clear() self.ui.usb_vendor.clear() self.ui.usb_model.clear() self.ui.usb_size.clear() self.ui.usb_mount.clear() self.ui.usb_type.clear() self.ui.usb_fs.clear() log("No USB disk found...")
Example #28
Source File: imager.py From multibootusb with GNU General Public License v2.0 | 5 votes |
def dd_iso_image(dd_progress_thread): try: dd_progress_thread.set_error(None) _dd_iso_image(dd_progress_thread) except: # config.imager_return = False o = io.StringIO() traceback.print_exc(None, o) log(o.getvalue()) dd_progress_thread.set_error(o.getvalue())
Example #29
Source File: fake_cloud_client.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 #30
Source File: classification_results.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
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()