Python StringIO.StringIO() Examples
The following are 30
code examples of StringIO.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
StringIO
, or try the search function
.
Example #1
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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: from StringIO import StringIO stream = 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 #2
Source File: loggingwrapper.py From CAMISIM with Apache License 2.0 | 6 votes |
def add_log_stream(self, stream=sys.stderr, level=logging.INFO): """ Add a stream where messages are outputted to. @param stream: stderr/stdout or a file stream @type stream: file | FileIO | StringIO @param level: minimum level of messages to be logged @type level: int | long @return: None @rtype: None """ assert self.is_stream(stream) # assert isinstance(stream, (file, io.FileIO)) assert level in self._levelNames err_handler = logging.StreamHandler(stream) err_handler.setFormatter(self.message_formatter) err_handler.setLevel(level) self._logger.addHandler(err_handler)
Example #3
Source File: fastaanonymizer.py From CAMISIM with Apache License 2.0 | 6 votes |
def __init__(self, logfile=None, verbose=True, debug=False, seed=None, tmp_dir=None): """ Anonymize fasta sequences @attention: 'shuf' is used which loads everything into memory! @param logfile: file handler or file path to a log file @type logfile: file | io.FileIO | StringIO.StringIO | str | unicode @param verbose: Not verbose means that only warnings and errors will be past to stream @type verbose: bool @param debug: more output and files are kept, manual clean up required @type debug: bool @param seed: The seed written to the random_source file used by the 'shuf' command @type seed: long | int | float | str | unicode @param tmp_dir: directory for temporary files, like the random_source file for 'shuf' @type tmp_dir: str | unicode @return: None @rtype: None """ assert isinstance(verbose, bool) assert isinstance(debug, bool) assert seed is None or isinstance(seed, (long, int, float, basestring)) assert tmp_dir is None or isinstance(tmp_dir, basestring) if tmp_dir is not None: assert self.validate_dir(tmp_dir) else: tmp_dir = tempfile.gettempdir() self._tmp_dir = tmp_dir super(FastaAnonymizer, self).__init__(logfile, verbose, debug, label="FastaAnonymizer") if seed is not None: random.seed(seed) script_dir = os.path.dirname(self.get_full_path(__file__)) self._anonymizer = os.path.join(script_dir, "anonymizer.py") self._fastastreamer = os.path.join(script_dir, "fastastreamer.py") assert self.validate_file(self._anonymizer) assert self.validate_file(self._fastastreamer)
Example #4
Source File: test_Completeness.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_str(self): """ Test __str__ method, for full coverage and check that all modules have required attributes. """ atts_list = ['PlanetPopulation', 'PlanetPhysicalModel', 'dMagLim', 'minComp'] for mod in self.allmods: with RedirectStreams(stdout=self.dev_null): obj = mod(**copy.deepcopy(self.spec)) original_stdout = sys.stdout sys.stdout = StringIO() # call __str__ method result = obj.__str__() # examine what was printed contents = sys.stdout.getvalue() self.assertEqual(type(contents), type('')) # attributes from ICD for att in atts_list: self.assertIn(att,contents,'{} missing for {}'.format(att,mod.__name__)) sys.stdout.close() # it also returns a string, which is not necessary self.assertEqual(type(result), type('')) # put stdout back sys.stdout = original_stdout
Example #5
Source File: metadatatable.py From CAMISIM with Apache License 2.0 | 6 votes |
def __init__(self, separator="\t", logfile=None, verbose=True): """ Handle tab separated files @attention: @param separator: default character assumed to separate values in a file @type separator: str | unicode @param logfile: file handler or file path to a log file @type logfile: file | io.FileIO | StringIO.StringIO | basestring @param verbose: Not verbose means that only warnings and errors will be past to stream @type verbose: bool @return: None @rtype: None """ assert logfile is None or isinstance(logfile, basestring) or self.is_stream(logfile) assert isinstance(separator, basestring), "separator must be string" assert isinstance(verbose, bool), "verbose must be true or false" super(MetadataTable, self).__init__(label="MetadataReader", logfile=logfile, verbose=verbose) self._number_of_rows = 0 self._meta_table = {} self._separator = separator self._list_of_column_names = []
Example #6
Source File: mappers.py From bioservices with GNU General Public License v3.0 | 6 votes |
def get_data_from_biodbnet(self, df_hgnc): """keys are unique Gene names input is made of the df based on HGNC data web services uniprot accession are duplicated sometimes. If som this is actually the iprimary accession entry and all secondary ones. e.g. , ABHD11 >>>> Q8N723;Q8NFV2;Q8NFV3;Q6PJU0;Q8NFV4;H7BYM8;Q8N722;Q9HBS8 ABHDB_HUMAN Alpha/beta hydrolase domain-containing protein 11 correspond actually to the primary one : Q8NFV4 """ b = biodbnet.BioDBNet() res2 = b.db2db("Gene Symbol", ["HGNC ID", "UniProt Accession", "UniProt Entry Name", "UniProt Protein Name", "KEGG Gene ID", "Ensembl Gene ID"], res.keys()[0:2000]) import pandas as pd import StringIO c = pd.read_csv(StringIO.StringIO(res2), delimiter="\t", index_col="Gene Symbol") return c
Example #7
Source File: test_build.py From alibuild with GNU General Public License v3.0 | 6 votes |
def dummy_open(x, mode="r"): if mode == "r": known = { "/sw/BUILD/27ce49698e818e8efb56b6eff6dd785e503df341/defaults-release/.build_succeeded": (0, StringIO("0")), "/sw/BUILD/3e90b4e08bad439fa5f25282480d1adb9efb0c0d/zlib/.build_succeeded": (0, StringIO("0")), "/sw/BUILD/%s/ROOT/.build_succeeded" % TEST_ROOT_BUILD_HASH: (0, StringIO("0")), "/sw/osx_x86-64/defaults-release/v1-1/.build-hash": (1, StringIO("27ce49698e818e8efb56b6eff6dd785e503df341")), "/sw/osx_x86-64/zlib/v1.2.3-1/.build-hash": (1, StringIO("3e90b4e08bad439fa5f25282480d1adb9efb0c0d")), "/sw/osx_x86-64/ROOT/v6-08-30-1/.build-hash": (1, StringIO(TEST_ROOT_BUILD_HASH)) } if not x in known: return DEFAULT threshold, result = known[x] if threshold > TIMES_ASKED.get(x, 0): result = None TIMES_ASKED[x] = TIMES_ASKED.get(x, 0) + 1 if not result: raise IOError return result return StringIO()
Example #8
Source File: mx_ide_eclipse.py From mx with GNU General Public License v2.0 | 6 votes |
def _copy_eclipse_settings(p, files=None): processors = p.annotation_processors() settingsDir = join(p.dir, ".settings") mx.ensure_dir_exists(settingsDir) for name, sources in p.eclipse_settings_sources().items(): out = StringIO() print('# GENERATED -- DO NOT EDIT', file=out) for source in sources: print('# Source:', source, file=out) with open(source) as f: print(f.read(), file=out) if p.javaCompliance: jc = p.javaCompliance if p.javaCompliance.value < _max_Eclipse_JavaExecutionEnvironment else mx.JavaCompliance(_max_Eclipse_JavaExecutionEnvironment) content = out.getvalue().replace('${javaCompliance}', str(jc)) else: content = out.getvalue() if processors: content = content.replace('org.eclipse.jdt.core.compiler.processAnnotations=disabled', 'org.eclipse.jdt.core.compiler.processAnnotations=enabled') mx.update_file(join(settingsDir, name), content) if files: files.append(join(settingsDir, name))
Example #9
Source File: select_jdk.py From mx with GNU General Public License v2.0 | 6 votes |
def get_shell_commands(args, jdk, extra_jdks): setvar_format = get_setvar_format(args.shell) shell_commands = StringIO() print(setvar_format % ('JAVA_HOME', jdk), file=shell_commands) if extra_jdks: print(setvar_format % ('EXTRA_JAVA_HOMES', os.pathsep.join(extra_jdks)), file=shell_commands) path = os.environ.get('PATH').split(os.pathsep) if path: jdk_bin = join(jdk, 'bin') old_java_home = os.environ.get('JAVA_HOME') replace = join(old_java_home, 'bin') if old_java_home else None if replace in path: path = [e if e != replace else jdk_bin for e in path] else: path = [jdk_bin] + path print(setvar_format % ('PATH', get_PATH_sep(args.shell).join(path)), file=shell_commands) return shell_commands.getvalue().strip()
Example #10
Source File: test_PlanetPopulation.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_str(self): """ Test __str__ method, for full coverage and check that all modules have required attributes. """ atts_list = ['PlanetPhysicalModel', 'arange', 'erange', 'Irange', 'Orange', 'wrange', 'prange', 'Rprange', 'Mprange', 'rrange', 'scaleOrbits', 'constrainOrbits', 'eta'] for mod in self.allmods: with RedirectStreams(stdout=self.dev_null): obj = mod(**self.spec) original_stdout = sys.stdout sys.stdout = StringIO() # call __str__ method result = obj.__str__() # examine what was printed contents = sys.stdout.getvalue() self.assertEqual(type(contents), type('')) # attributes from ICD for att in atts_list: self.assertIn(att,contents,'{} missing for {}'.format(att,mod.__name__)) sys.stdout.close() # it also returns a string, which is not necessary self.assertEqual(type(result), type('')) # put stdout back sys.stdout = original_stdout
Example #11
Source File: test_KnownRVPlanetsTargetList.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_str(self): r"""Test __str__ method, for full coverage.""" tlist = self.fixture # replace stdout and keep a reference original_stdout = sys.stdout sys.stdout = StringIO() # call __str__ method result = tlist.__str__() # examine what was printed contents = sys.stdout.getvalue() self.assertEqual(type(contents), type('')) self.assertIn('PlanetPhysicalModel', contents) self.assertIn('PlanetPopulation', contents) self.assertIn('Completeness', contents) sys.stdout.close() # it also returns a string, which is not necessary self.assertEqual(type(result), type('')) # put stdout back sys.stdout = original_stdout # @unittest.skip("Skipping populate_target_list.")
Example #12
Source File: test_PlanetPhysicalModel.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_str(self): """ Test __str__ method, for full coverage and check that all modules have required attributes. """ for mod in self.allmods: with RedirectStreams(stdout=self.dev_null): obj = mod() original_stdout = sys.stdout sys.stdout = StringIO() # call __str__ method result = obj.__str__() # examine what was printed contents = sys.stdout.getvalue() self.assertEqual(type(contents), type('')) # attributes from ICD self.assertIn('_outspec', contents,'_outspec missing for %s'%mod.__name__) sys.stdout.close() # it also returns a string, which is not necessary self.assertEqual(type(result), type('')) # put stdout back sys.stdout = original_stdout
Example #13
Source File: test__migrations.py From django-river with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_shouldCreateAllMigrations(self): for f in os.listdir("river/migrations"): if f != "__init__.py" and f != "__pycache__" and not f.endswith(".pyc"): open(os.path.join("river/tests/volatile/river/", f), 'wb').write(open(os.path.join("river/migrations", f), 'rb').read()) self.migrations_before = list(filter(lambda f: f.endswith('.py') and f != '__init__.py', os.listdir('river/tests/volatile/river/'))) out = StringIO() sys.stout = out call_command('makemigrations', 'river', stdout=out) self.migrations_after = list(filter(lambda f: f.endswith('.py') and f != '__init__.py', os.listdir('river/tests/volatile/river/'))) assert_that(out.getvalue(), equal_to("No changes detected in app 'river'\n")) assert_that(self.migrations_after, has_length(len(self.migrations_before)))
Example #14
Source File: cloudpickle.py From pywren-ibm-cloud with Apache License 2.0 | 6 votes |
def dumps(obj, protocol=None): """Serialize obj as a string of bytes allocated in memory protocol defaults to cloudpickle.DEFAULT_PROTOCOL which is an alias to pickle.HIGHEST_PROTOCOL. This setting favors maximum communication speed between processes running the same Python version. Set protocol=pickle.DEFAULT_PROTOCOL instead if you need to ensure compatibility with older versions of Python. """ file = StringIO() try: cp = CloudPickler(file, protocol=protocol) cp.dump(obj) return file.getvalue() finally: file.close() # including pickles unloading functions in this namespace
Example #15
Source File: urllib2.py From jawfish with MIT License | 6 votes |
def open_local_file(self, req): host = req.get_host() file = req.get_selector() localfile = url2pathname(file) stats = os.stat(localfile) size = stats[stat.ST_SIZE] modified = rfc822.formatdate(stats[stat.ST_MTIME]) mtype = mimetypes.guess_type(file)[0] stats = os.stat(localfile) headers = mimetools.Message(StringIO( 'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) if host: host, port = splitport(host) if not host or \ (not port and socket.gethostbyname(host) in self.get_names()): return addinfourl(open(localfile, 'rb'), headers, 'file:'+file) raise URLError('file not on local host')
Example #16
Source File: test_fileholders.py From me-ica with GNU Lesser General Public License v2.1 | 6 votes |
def test_same_file_as(): fh = FileHolder('a_fname') assert_true(fh.same_file_as(fh)) fh2 = FileHolder('a_test') assert_false(fh.same_file_as(fh2)) sio0 = StringIO() fh3 = FileHolder('a_fname', sio0) fh4 = FileHolder('a_fname', sio0) assert_true(fh3.same_file_as(fh4)) assert_false(fh3.same_file_as(fh)) fh5 = FileHolder(fileobj=sio0) fh6 = FileHolder(fileobj=sio0) assert_true(fh5.same_file_as(fh6)) # Not if the filename is the same assert_false(fh5.same_file_as(fh3)) # pos doesn't matter fh4_again = FileHolder('a_fname', sio0, pos=4) assert_true(fh3.same_file_as(fh4_again))
Example #17
Source File: __init__.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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: from StringIO import StringIO stream = 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 #18
Source File: draw.py From rate.sx with MIT License | 5 votes |
def _make_diagram(self): class Option(object): #pylint: disable=too-many-instance-attributes,too-few-public-methods """Diagram configuration.""" def __init__(self): self.axis = False self.batch = None self.color = True self.encoding = None self.function = None self.height = None self.keys = None self.legend = None self.palette = 'spectrum-reversed' self.reverse = None self.sleep = None data = self.data istream = [str(x) for x in data['ticks']] ostream = StringIO.StringIO() size = diagram.Point((self.width, self.height)) option = Option() engine = diagram.AxisGraph(size, option) engine.consume(istream, ostream) meta = data['meta'] high_line = self._align_label(meta['time_max'], _format_value(meta['max'])) low_line = self._align_label(meta['time_min'], _format_value(meta['min'])) lines = [high_line] + ostream.getvalue().splitlines() + [low_line] output = "" output += "\n".join([u" │ %s" % x.decode('utf-8') for x in lines]) output += u"\n └" + u"─" * 80 return output
Example #19
Source File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py From L.E.S.M.A with Apache License 2.0 | 5 votes |
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 #20
Source File: urllib2.py From jawfish with MIT License | 5 votes |
def ftp_open(self, req): host = req.get_host() if not host: raise IOError('ftp error', 'no host given') # XXX handle custom username & password try: host = socket.gethostbyname(host) except socket.error(msg): raise URLError(msg) host, port = splitport(host) if port is None: port = ftplib.FTP_PORT path, attrs = splitattr(req.get_selector()) path = unquote(path) dirs = path.split('/') dirs, file = dirs[:-1], dirs[-1] if dirs and not dirs[0]: dirs = dirs[1:] user = passwd = '' # XXX try: fw = self.connect_ftp(user, passwd, host, port, dirs) type = file and 'I' or 'D' for attr in attrs: attr, value = splitattr(attr) if attr.lower() == 'type' and \ value in ('a', 'A', 'i', 'I', 'd', 'D'): type = value.upper() fp, retrlen = fw.retrfile(file, type) headers = "" mtype = mimetypes.guess_type(req.get_full_url())[0] if mtype: headers += "Content-Type: %s\n" % mtype if retrlen is not None and retrlen >= 0: headers += "Content-Length: %d\n" % retrlen sf = StringIO(headers) headers = mimetools.Message(sf) return addinfourl(fp, headers, req.get_full_url()) except ftplib.all_errors(msg): raise IOError(('ftp error', msg), sys.exc_info()[2])
Example #21
Source File: cloudpickle.py From pywren-ibm-cloud with Apache License 2.0 | 5 votes |
def save_file(self, obj): """Save a file""" try: import StringIO as pystringIO # we can't use cStringIO as it lacks the name attribute except ImportError: import io as pystringIO if not hasattr(obj, 'name') or not hasattr(obj, 'mode'): raise pickle.PicklingError("Cannot pickle files that do not map to an actual file") if obj is sys.stdout: return self.save_reduce(getattr, (sys, 'stdout'), obj=obj) if obj is sys.stderr: return self.save_reduce(getattr, (sys, 'stderr'), obj=obj) if obj is sys.stdin: raise pickle.PicklingError("Cannot pickle standard input") if obj.closed: raise pickle.PicklingError("Cannot pickle closed files") if hasattr(obj, 'isatty') and obj.isatty(): raise pickle.PicklingError("Cannot pickle files that map to tty objects") if 'r' not in obj.mode and '+' not in obj.mode: raise pickle.PicklingError("Cannot pickle files that are not opened for reading: %s" % obj.mode) name = obj.name retval = pystringIO.StringIO() try: # Read the whole file curloc = obj.tell() obj.seek(0) contents = obj.read() obj.seek(curloc) except IOError: raise pickle.PicklingError("Cannot pickle file %s as it cannot be read" % name) retval.write(contents) retval.seek(curloc) retval.name = name self.save(retval) self.memoize(obj)
Example #22
Source File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py From L.E.S.M.A with Apache License 2.0 | 5 votes |
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 #23
Source File: L.E.S.M.A. - Fabrica de Noobs Speedtest.py From L.E.S.M.A with Apache License 2.0 | 5 votes |
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 #24
Source File: cgi.py From jawfish with MIT License | 5 votes |
def __init__(self, name, value): """Constructor from field name and value.""" self.name = name self.value = value # self.file = StringIO(value)
Example #25
Source File: cgi.py From jawfish with MIT License | 5 votes |
def read_lines(self): """Internal: read lines until EOF or outerboundary.""" self.file = self.__file = StringIO() if self.outerboundary: self.read_lines_to_outerboundary() else: self.read_lines_to_eof()
Example #26
Source File: cgi.py From jawfish with MIT License | 5 votes |
def __init__(self, name, value): """Constructor from field name and value.""" self.name = name self.value = value # self.file = StringIO(value)
Example #27
Source File: classifier.py From oabot with MIT License | 5 votes |
def check_nb_pages(self, data): """ Does this PDF contain enough pages? """ try: s_io = StringIO(data) reader = PyPDF2.PdfFileReader(s_io) num_pages = reader.getNumPages() print("num pages: %d" % num_pages) return num_pages > 2 except PyPdfError as e: return False
Example #28
Source File: toolbox.py From razzy-spinner with GNU General Public License v3.0 | 5 votes |
def open_string(self, s): """Open a standard format marker string for sequential reading. @param s: string to parse as a standard format marker input file @type s: string """ self._file = StringIO(s)
Example #29
Source File: rid.py From razzy-spinner with GNU General Public License v3.0 | 5 votes |
def load_exclusion_list_from_string(self, string): exc_in = StringIO.StringIO(string) self.load_exclusion_list(exc_in)
Example #30
Source File: rid.py From razzy-spinner with GNU General Public License v3.0 | 5 votes |
def load_dictionary_from_string(self, string): rid_in = StringIO.StringIO(string) self.load_dictionary(rid_in)