Python io.StringIO.StringIO() Examples
The following are 30
code examples of io.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
io.StringIO
, or try the search function
.
Example #1
Source File: corridor.py From deep-rl-tensorflow with MIT License | 6 votes |
def _render(self, mode='human', close=False): if close: return outfile = StringIO.StringIO() if mode == 'ansi' else sys.stdout row, col = self.s // self.ncol, self.s % self.ncol desc = self.desc.tolist() desc[row][col] = utils.colorize(desc[row][col], "red", highlight=True) outfile.write("\n".join("".join(row) for row in desc)+"\n") if self.lastaction is not None: outfile.write(" ({})\n".format(self.get_action_meanings()[self.lastaction])) else: outfile.write("\n") return outfile
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: db.py From clonedigger with GNU General Public License v3.0 | 6 votes |
def _init_psycopg2(self): """initialize psycopg2 to use mx.DateTime for date and timestamps instead for datetime.datetime""" psycopg2 = self._native_module if hasattr(psycopg2, '_lc_initialized'): return psycopg2._lc_initialized = 1 # use mxDateTime instead of datetime if available if HAS_MX_DATETIME: from psycopg2 import extensions extensions.register_type(psycopg2._psycopg.MXDATETIME) extensions.register_type(psycopg2._psycopg.MXINTERVAL) extensions.register_type(psycopg2._psycopg.MXDATE) extensions.register_type(psycopg2._psycopg.MXTIME) # StringIO/cStringIO adaptation # XXX (syt) todo, see my december discussion on the psycopg2 list # for a working solution #def adapt_stringio(stringio): # print 'ADAPTING', stringio # return psycopg2.Binary(stringio.getvalue()) #import StringIO #extensions.register_adapter(StringIO.StringIO, adapt_stringio) #import cStringIO #extensions.register_adapter(cStringIO.StringIO, adapt_stringio)
Example #4
Source File: test_BackgroundSources.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.StringIO() sys.stdout = StringIO() # call __str__ method result = obj.__str__() # examine what was printed contents = sys.stdout.getvalue() self.assertEqual(type(contents), type('')) 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: M2x.py From redeem with GNU General Public License v3.0 | 6 votes |
def process_gcode(self, g): profile = cProfile.Profile() self.printer.sd_card_manager.set_status(True) profile.enable() for line in self.printer.sd_card_manager: line = line.strip() if not line or line.startswith(';'): continue file_g = Gcode({"message": line}) self.printer.processor.enqueue(file_g) if self.printer.sd_card_manager.get_status(): logging.info("M24: Print from file complete") self.printer.sd_card_manager.set_status(False) self.printer.send_message(g.prot, "Done printing file") profile.disable() s = StringIO.StringIO() sortby = 'cumulative' ps = pstats.Stats(profile, stream=s).sort_stats(sortby) ps.print_stats() logging.debug(s.getvalue()) self.printer.sd_card_manager.reset()
Example #6
Source File: test_dal.py From termite-visualizations with BSD 3-Clause "New" or "Revised" License | 6 votes |
def testRun(self): db = DAL(DEFAULT_URI, check_reserved=['all']) db.define_table('person', Field('name'),Field('uuid')) db.define_table('pet',Field('friend',db.person),Field('name')) for n in range(2): db(db.pet).delete() db(db.person).delete() for k in range(10): id = db.person.insert(name=str(k),uuid=str(k)) db.pet.insert(friend=id,name=str(k)) db.commit() stream = StringIO.StringIO() db.export_to_csv_file(stream) stream = StringIO.StringIO(stream.getvalue()) db.import_from_csv_file(stream) assert db(db.person).count()==10 assert db(db.person.id==db.pet.friend)(db.person.name==db.pet.name).count()==20 db.pet.drop() db.person.drop() db.commit()
Example #7
Source File: test_dal.py From termite-visualizations with BSD 3-Clause "New" or "Revised" License | 6 votes |
def testRun(self): db = DAL(DEFAULT_URI, check_reserved=['all']) db.define_table('person', Field('name')) db.define_table('pet',Field('friend',db.person),Field('name')) for n in range(2): db(db.pet).delete() db(db.person).delete() for k in range(10): id = db.person.insert(name=str(k)) db.pet.insert(friend=id,name=str(k)) db.commit() stream = StringIO.StringIO() db.export_to_csv_file(stream) db(db.pet).delete() db(db.person).delete() stream = StringIO.StringIO(stream.getvalue()) db.import_from_csv_file(stream) assert db(db.person.id==db.pet.friend)(db.person.name==db.pet.name).count()==10 db.pet.drop() db.person.drop() db.commit()
Example #8
Source File: setup.py From MutatorMath with BSD 3-Clause "New" or "Revised" License | 6 votes |
def capture_logger(name): """ Context manager to capture a logger output with a StringIO stream. """ import logging logger = logging.getLogger(name) try: import StringIO stream = StringIO.StringIO() except ImportError: from io import StringIO stream = StringIO() handler = logging.StreamHandler(stream) logger.addHandler(handler) try: yield stream finally: logger.removeHandler(handler)
Example #9
Source File: agent.py From backnet with GNU General Public License v3.0 | 6 votes |
def python(self, command_or_file): """ Runs a python command or a python file and returns the output """ new_stdout = StringIO.StringIO() old_stdout = sys.stdout sys.stdout = new_stdout new_stderr = StringIO.StringIO() old_stderr = sys.stderr sys.stderr = new_stderr if os.path.exists(command_or_file): self.send_output("[*] Running python file...") with open(command_or_file, 'r') as f: python_code = f.read() try: exec(python_code) except Exception as exc: self.send_output(traceback.format_exc()) else: self.send_output("[*] Running python command...") try: exec(command_or_file) except Exception as exc: self.send_output(traceback.format_exc()) sys.stdout = old_stdout sys.stderr = old_stderr self.send_output(new_stdout.getvalue() + new_stderr.getvalue())
Example #10
Source File: TokenStreamRewriter.py From compilers-cin with MIT License | 5 votes |
def execute(self, buf): """ :type buf: StringIO.StringIO :param buf: :return: """ return self.index
Example #11
Source File: decode.py From pappy-proxy with MIT License | 5 votes |
def gzip_decode_helper(s): dec_data = gzip.GzipFile('', 'rb', 9, StringIO.StringIO(s)) dec_data = dec_data.read() return dec_data
Example #12
Source File: controllers.py From ai-chatbot-framework with MIT License | 5 votes |
def export_intents(): """ Deserialize and export Mongoengines as jsonfile :return: """ try: strIO = StringIO.StringIO() except AttributeError: strIO = StringIO() strIO.write(Intent.objects.to_json()) strIO.seek(0) return send_file(strIO, attachment_filename="iky_intents.json", as_attachment=True)
Example #13
Source File: debug.py From ubuntu-cleaner with GNU General Public License v3.0 | 5 votes |
def run_traceback(level, textview_only=False, text_only=False): '''Two level: fatal and error''' from ubuntucleaner.gui import GuiBuilder output = StringIO.StringIO() exc = traceback.print_exc(file=output) worker = GuiBuilder('traceback.xml') textview = worker.get_object('%s_view' % level) buffer = textview.get_buffer() iter = buffer.get_start_iter() anchor = buffer.create_child_anchor(iter) button = Gtk.Button(label=_('Copy Error Message')) button.show() textview.add_child_at_anchor(button, anchor) error_text = "\nDistribution: %s\nApplication: %s\nDesktop:%s\n\n%s" % (system.DISTRO, system.APP, system.DESKTOP, output.getvalue()) buffer.insert(iter, error_text) button.connect('clicked', on_copy_button_clicked, error_text) if text_only: return error_text if textview_only: return textview else: dialog = worker.get_object('%sDialog' % level.capitalize()) dialog.destroy() output.close()
Example #14
Source File: AlarmDb.py From LightUpPi-Alarm with MIT License | 5 votes |
def export_alarms_json(self): """ Exports all the alarm data into a JSON string. The dataset.freeze() method exports the table into a file object, but it is going to be "tricked" into getting an string object to send back. Because it also closes the object file we need to overwrite the close function to retrieve the data and, then restore it back to normal. :return: String containing all the alarm data """ def fake_close(): pass out_iostr = StringIO.StringIO() original_close = out_iostr.close alarms_table = self.__connect_alarms() # Retrieve the db as a json StringIO without the close method out_iostr.close = fake_close dataset.freeze(alarms_table.all(), format='json', fileobj=out_iostr) out_str = out_iostr.getvalue() out_iostr.close = original_close out_iostr.close() # Get only the required data and format it alarms_dict = {'alarms': json.loads(out_str)['results']} # This commented out line would prettify the string #json.dumps(alarms_dict, indent=4, separators=(',', ': ')) return json.dumps(alarms_dict) # # member functions to add alarm data #
Example #15
Source File: png.py From convis with GNU General Public License v3.0 | 5 votes |
def png_client(images,info={},port=10000,host='localhost',compress_level=0,resize=(1.0,1.0)): if len(images.shape) == 2: images = [images] # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Connect the socket to the port where the server is listening server_address = (host, port) sock.connect(server_address) try: for A in images: im = Image.fromarray(256.0*A).convert('RGB') if resize != (1.0,1.0) and resize is not None: if not type(resize) == tuple: resize = (resize,resize) im = im.resize((int(resize[0]*im.size[0]), int(resize[1]*im.size[1])), PIL.Image.ANTIALIAS) output = StringIO.StringIO() meta = PngImagePlugin.PngInfo() reserved = ('interlace', 'gamma', 'dpi', 'transparency', 'aspect') for k,v in _iteritems(info): if k in reserved: continue meta.add_text(str(k), str(v), 0) im.save(output, format="PNG", pnginfo=meta, compress_level=compress_level) message = output.getvalue() output.close() sock.sendall(message) finally: sock.close()
Example #16
Source File: png.py From convis with GNU General Public License v3.0 | 5 votes |
def __init__(self, port = 10000, size=(0,0)): from PIL import Image, ImageTk self.port = port self.size = size self.buffer = StringIO() self.image = Image.fromarray(np.zeros((200,200))).convert('RGB') self.closed = False self.dirty = False self.refresh_rate = 10 self.last_buffer = [] self.debug = False thread.start_new_thread(self.wait_for_connection,tuple())
Example #17
Source File: png.py From convis with GNU General Public License v3.0 | 5 votes |
def serve(self,connection, client_address): try: if self.debug: print("Recieving Data.") dirty = False all_data = StringIO() last_data = b"" while True: data = connection.recv(16) dirty = True all_data.write(data) if b"IEND" in last_data+data: if self.debug: print("Recieved End of Image.") #print (last_data+data) #print 'found IEND!' self.last_buffer.append(all_data) all_data = StringIO() all_data.write((last_data+data)[(last_data+data).find(b"IEND")+8:]) dirty = False last_data = (last_data+data)[(last_data+data).find(b"IEND")+8:] else: last_data = data #self.buffer.write(data) #all_data += data if not data: break if dirty: #print 'Cleaning up unclosed image...' self.last_buffer.append(all_data) #self.dirty = True # redraw the image finally: if self.debug: print("Closing connection.") connection.close()
Example #18
Source File: TokenStreamRewriter.py From compilers-cin with MIT License | 5 votes |
def getText(self, program_name, interval): """ :type interval: Interval.Interval :param program_name: :param interval: :return: """ rewrites = self.programs.get(program_name) start = interval.start stop = interval.stop # ensure start/end are in range if stop > len(self.tokens.tokens) - 1: stop = len(self.tokens.tokens) - 1 if start < 0: start = 0 # if no instructions to execute if not rewrites: return self.tokens.getText(interval) buf = StringIO() indexToOp = self._reduceToSingleOperationPerIndex(rewrites) i = start while all((i <= stop, i < len(self.tokens.tokens))): op = indexToOp.get(i) token = self.tokens.get(i) if op is None: if token.type != Token.EOF: buf.write(token.text) i += 1 else: i = op.execute(buf) if stop == len(self.tokens.tokens)-1: for op in indexToOp.values(): if op.index >= len(self.tokens.tokens)-1: buf.write(op.text) return buf.getvalue()
Example #19
Source File: loggingwrapper.py From CAMISIM with Apache License 2.0 | 5 votes |
def is_stream(stream): """ Test for streams @param stream: Any kind of stream type @type stream: file | io.FileIO | StringIO.StringIO @return: True if stream @rtype: bool """ return hasattr(stream, 'read') and hasattr(stream, 'write')
Example #20
Source File: TokenStreamRewriter.py From compilers-cin with MIT License | 5 votes |
def getText(self, program_name, interval): """ :type interval: Interval.Interval :param program_name: :param interval: :return: """ rewrites = self.programs.get(program_name) start = interval.start stop = interval.stop # ensure start/end are in range if stop > len(self.tokens.tokens) - 1: stop = len(self.tokens.tokens) - 1 if start < 0: start = 0 # if no instructions to execute if not rewrites: return self.tokens.getText(interval) buf = StringIO() indexToOp = self._reduceToSingleOperationPerIndex(rewrites) i = start while all((i <= stop, i < len(self.tokens.tokens))): op = indexToOp.get(i) token = self.tokens.get(i) if op is None: if token.type != Token.EOF: buf.write(token.text) i += 1 else: i = op.execute(buf) if stop == len(self.tokens.tokens)-1: for op in indexToOp.values(): if op.index >= len(self.tokens.tokens)-1: buf.write(op.text) return buf.getvalue()
Example #21
Source File: TokenStreamRewriter.py From compilers-cin with MIT License | 5 votes |
def execute(self, buf): """ :type buf: StringIO.StringIO :param buf: :return: """ return self.index
Example #22
Source File: tasks.py From loonflow with MIT License | 5 votes |
def stdoutIO(stdout=None): old = sys.stdout if stdout is None: try: # for python2 stdout = StringIO.StringIO() except Exception: stdout = StringIO() sys.stdout = stdout yield stdout sys.stdout = old
Example #23
Source File: template.py From termite-visualizations with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self): self.body = StringIO.StringIO()
Example #24
Source File: program_utils.py From g-tensorflow-models with Apache License 2.0 | 5 votes |
def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = StringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old
Example #25
Source File: program_utils.py From models with Apache License 2.0 | 5 votes |
def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = StringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old
Example #26
Source File: mxdoc.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def _string_io(): oldout = sys.stdout olderr = sys.stderr strio = StringIO.StringIO() sys.stdout = strio sys.stderr = strio yield strio sys.stdout = oldout sys.stderr = olderr
Example #27
Source File: program_utils.py From multilabel-image-classification-tensorflow with MIT License | 5 votes |
def stdoutIO(stdout=None): old = sys.stdout if stdout is None: stdout = StringIO.StringIO() sys.stdout = stdout yield stdout sys.stdout = old
Example #28
Source File: TokenStreamRewriter.py From Graphvizer with GNU General Public License v2.0 | 5 votes |
def execute(self, buf): """ :type buf: StringIO.StringIO :param buf: :return: """ return self.index
Example #29
Source File: __init__.py From sphinx-execute-code with MIT License | 5 votes |
def execute_code(cls, code): """ Executes supplied code as pure python and returns a list of stdout, stderr Args: code (string): Python code to execute Results: (list): stdout, stderr of executed python code Raises: ExecutionError when supplied python is incorrect Examples: >>> execute_code('print "foobar"') 'foobar' """ output = StringIO.StringIO() err = StringIO.StringIO() sys.stdout = output sys.stderr = err try: # pylint: disable=exec-used exec(code) # If the code is invalid, just skip the block - any actual code errors # will be raised properly except TypeError: pass sys.stdout = sys.__stdout__ sys.stderr = sys.__stderr__ results = list() results.append(output.getvalue()) results.append(err.getvalue()) results = ''.join(results) return results
Example #30
Source File: densepose_cocoeval.py From Parsing-R-CNN with MIT License | 5 votes |
def _decodeUvData(self, dt): from PIL import Image from io import StringIO uvData = dt['uv_data'] uvShape = dt['uv_shape'] fStream = StringIO.StringIO(uvData.decode('base64')) im = Image.open(fStream) data = np.rollaxis(np.array(im.getdata(), dtype=np.uint8), -1, 0) dt['uv'] = data.reshape(uvShape) del dt['uv_data'] del dt['uv_shape']