本文整理匯總了Python中StringIO.StringIO.StringIO方法的典型用法代碼示例。如果您正苦於以下問題:Python StringIO.StringIO方法的具體用法?Python StringIO.StringIO怎麽用?Python StringIO.StringIO使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類StringIO.StringIO
的用法示例。
在下文中一共展示了StringIO.StringIO方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_str
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
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
示例2: _render
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
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
示例3: execute_code
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
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
示例4: __init__
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def __init__(self):
self.boundary = None
self.current_chunk = StringIO.StringIO()
# Return: mjpeg chunk if found
# None: in the middle of the chunk
示例5: test_stdout_encoding_None
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def test_stdout_encoding_None():
instance = PdbTest()
instance.stdout = BytesIO()
instance.stdout.encoding = None
instance.ensure_file_can_write_unicode(instance.stdout)
try:
import cStringIO
except ImportError:
pass
else:
instance.stdout = cStringIO.StringIO()
instance.ensure_file_can_write_unicode(instance.stdout)
示例6: test_py2_ensure_file_can_write_unicode
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def test_py2_ensure_file_can_write_unicode():
import StringIO
stdout = StringIO.StringIO()
stdout.encoding = 'ascii'
p = Pdb(Config=DefaultConfig, stdout=stdout)
assert p.stdout.stream is stdout
p.stdout.write(u"test äöüß")
stdout.seek(0)
assert stdout.read().decode('utf-8') == u"test äöüß"
示例7: __init__
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def __init__(self, code, msg, headers, data, url=None):
StringIO.StringIO.__init__(self, data)
self.code, self.msg, self.headers, self.url = code, msg, headers, url
示例8: http_open
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def http_open(self, req):
import mimetools, copy
from StringIO import StringIO
self.requests.append(copy.deepcopy(req))
if self._count == 0:
self._count = self._count + 1
name = httplib.responses[self.code]
msg = mimetools.Message(StringIO(self.headers))
return self.parent.error(
"http", req, MockFile(), self.code, name, msg)
else:
self.req = req
msg = mimetools.Message(StringIO("\r\n\r\n"))
return MockResponse(200, "OK", msg, "", req.get_full_url())
示例9: read_ascii_string
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def read_ascii_string(data):
"""Read geometry from a :py:class:`str` containing data in the STL *ASCII* format.
This is just a wrapper around :py:func:`read_ascii_file` that first wraps
the provided string in a :py:class:`StringIO.StringIO` object.
"""
from StringIO import StringIO
return read_ascii_file(StringIO(data))
示例10: read_binary_string
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def read_binary_string(data):
"""Read geometry from a :py:class:`str` containing data in the STL *binary* format.
This is just a wrapper around :py:func:`read_binary_file` that first wraps
the provided string in a :py:class:`StringIO.StringIO` object.
"""
from StringIO import StringIO
return read_binary_file(StringIO(data))
示例11: http_open
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def http_open(self, req):
import mimetools, httplib, copy
from StringIO import StringIO
self.requests.append(copy.deepcopy(req))
if self._count == 0:
self._count = self._count + 1
name = httplib.responses[self.code]
msg = mimetools.Message(StringIO(self.headers))
return self.parent.error(
"http", req, MockFile(), self.code, name, msg)
else:
self.req = req
msg = mimetools.Message(StringIO("\r\n\r\n"))
return MockResponse(200, "OK", msg, "", req.get_full_url())
示例12: run_request
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def run_request(request):
pid = subprocess.Popen(run_command, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results = pid.communicate(input=request.encode())
tmp_file = StringIO(results[0].decode())
results = read_rpc_messages(tmp_file)
parsed_results = []
for result in results:
if "method" in result:
continue
parsed_results.append(result['result'])
errcode = pid.poll()
return errcode, parsed_results
示例13: getFileType
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
def getFileType(self):
try:
from StringIO import StringIO
except ImportError:
raise SkipTest("StringIO.StringIO is not available.")
else:
return StringIO
示例14: png_client
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
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()
示例15: __init__
# 需要導入模塊: from StringIO import StringIO [as 別名]
# 或者: from StringIO.StringIO import StringIO [as 別名]
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())