def histogram(series, **kwargs):
"""Plot an histogram of the data.
Parameters
----------
series: Series, default None
The data to plot.
Returns
-------
str, The resulting image encoded as a string.
"""
imgdata = BytesIO()
plot = _plot_histogram(series, **kwargs)
plot.figure.subplots_adjust(left=0.15, right=0.95, top=0.9, bottom=0.1, wspace=0, hspace=0)
plot.figure.savefig(imgdata)
imgdata.seek(0)
result_string = 'data:image/png;base64,' + quote(base64.b64encode(imgdata.getvalue()))
# TODO Think about writing this to disk instead of caching them in strings
plt.close(plot.figure)
return result_string
python类BytesIO()的实例源码
def extract_sound(self, start=0, end=None):
if not start and not end:
raise ValueError
start_pos = self.to_index(start)
if end:
end_pos = self.to_index(end)
else:
end_pos = len(self.raw)
_buffer = io.BytesIO()
_output = wave.open(_buffer, "wb")
_output.setnchannels(self.channels)
_output.setsampwidth(self.samplewidth)
_output.setframerate(self.framerate)
raw = self.raw[start_pos:end_pos]
_output.writeframes(self.raw[start_pos:end_pos])
_output.close()
_buffer.seek(0)
return Sound(_buffer)
def mini_histogram(series, **kwargs):
"""Plot a small (mini) histogram of the data.
Parameters
----------
series: Series, default None
The data to plot.
Returns
-------
str, The resulting image encoded as a string.
"""
imgdata = BytesIO()
plot = _plot_histogram(series, figsize=(2, 0.75), **kwargs)
plot.axes.get_yaxis().set_visible(False)
if LooseVersion(matplotlib.__version__) <= '1.5.9':
plot.set_axis_bgcolor("w")
else:
plot.set_facecolor("w")
xticks = plot.xaxis.get_major_ticks()
for tick in xticks[1:-1]:
tick.set_visible(False)
tick.label.set_visible(False)
for tick in (xticks[0], xticks[-1]):
tick.label.set_fontsize(8)
plot.figure.subplots_adjust(left=0.15, right=0.85, top=1, bottom=0.35, wspace=0, hspace=0)
plot.figure.savefig(imgdata)
imgdata.seek(0)
result_string = 'data:image/png;base64,' + quote(base64.b64encode(imgdata.getvalue()))
plt.close(plot.figure)
return result_string
def __init__(self, source, start=0, end=None):
if type(source) is bytes:
in_wav = wave.open(io.BytesIO(source))
else:
in_wav = wave.open(source, "rb")
self.framerate = in_wav.getframerate()
self.channels = in_wav.getnchannels()
self.samplewidth = in_wav.getsampwidth()
in_wav.setpos(int(start * self.framerate))
if end is None:
end = (in_wav.getnframes() - start / self.framerate)
self.raw = in_wav.readframes(int((end - start) * self.framerate))
in_wav.close()
def __init__(self, sound, start=0, end=None):
super(SoundThread, self).__init__()
self.wav_buffer = io.BytesIO()
_output = wave.open(self.wav_buffer, "wb")
_output.setnchannels(self.sound.channels)
_output.setsampwidth(self.sound.samplewidth)
_output.setframerate(self.sound.framerate)
_output.writeframes(self.sound.raw)
_output.close()
self.wav_buffer.seek(0)
def __iter__(self):
environ = self.environ
self.path = environ.get('PATH_INFO', '')
self.client_address = environ.get('REMOTE_ADDR','-'), \
environ.get('REMOTE_PORT','-')
self.command = environ.get('REQUEST_METHOD', '-')
self.wfile = BytesIO() # for capturing error
try:
path = self.translate_path(self.path)
etag = '"%s"' % os.path.getmtime(path)
client_etag = environ.get('HTTP_IF_NONE_MATCH')
self.send_header('ETag', etag)
if etag == client_etag:
self.send_response(304, "Not Modified")
self.start_response(self.status, self.headers)
raise StopIteration()
except OSError:
pass # Probably a 404
f = self.send_head()
self.start_response(self.status, self.headers)
if f:
block_size = 16 * 1024
while True:
buf = f.read(block_size)
if not buf:
break
yield buf
f.close()
else:
value = self.wfile.getvalue()
yield value
def __init__(self, app):
self.app = app
self.format = '%s - - [%s] "%s %s %s" - %s'
f = BytesIO()
class FakeSocket:
def makefile(self, *a):
return f
# take log_date_time_string method from BaseHTTPRequestHandler
self.log_date_time_string = BaseHTTPRequestHandler(FakeSocket(), None, None).log_date_time_string