本文整理汇总了Python中django.utils.six.moves.cStringIO类的典型用法代码示例。如果您正苦于以下问题:Python cStringIO类的具体用法?Python cStringIO怎么用?Python cStringIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了cStringIO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _run_command
def _run_command(self, **options):
"""Run the profile command with the given options on the diffsettings command and capture the output"""
output = StringIO()
error = StringIO()
args = []
if django.VERSION[0] == 1 and django.VERSION[1] < 8:
options = options.copy()
options['backend'] = 'cProfile'
options['testing'] = True
for option in ('fraction', 'max_calls', 'path', 'pattern', 'sort'):
if option not in options:
options[option] = None
call_command('profile', 'diffsettings', stderr=error, stdout=output, **options)
expected = 'INSTALLED_APPS'
else:
for option in options:
if option in ('fraction', 'pattern', 'sort'):
args.append('--{}={}'.format(option, options[option]))
elif option == 'max_calls':
args.append('--max-calls={}'.format(options[option]))
elif option == 'path':
args.append('--output={}'.format(options[option]))
args.append('showmigrations')
args.append('--plan')
options = {'backend': 'cProfile', 'testing': True, 'stderr': error, 'stdout': output}
call_command('profile', *args, **options)
expected = '0001_initial'
text = output.getvalue()
assert expected in text
return text
示例2: printer_label
def printer_label(sample):
"""Generate the PDF of a sample for the label printer.
:param sample: the sample the label of which should be generated
:type sample: `samples.models.Sample`
:return:
the PDF as a byte stream
:rtype: str
"""
output = StringIO()
text = sample.name
c = canvas.Canvas(output, pagesize=(width, height))
c.setAuthor("JuliaBase samples database")
c.setTitle(text)
c.setSubject("Label of {0} for the label printer".format(text))
try:
print_line(c, 0, fontsize, text)
except ExcessException:
first, second = best_split(text)
print_line(c, height / 2, fontsize_half, first, force=True)
print_line(c, 0, fontsize_half, second, force=True)
c.drawImage(ImageReader("http://chart.googleapis.com/chart?chs=116x116&cht=qr&chl={0}&chld=H|1".format(sample.id)),
width - height, 0, height, height)
c.showPage()
c.save()
return output.getvalue()
示例3: render_markdown_from_file
def render_markdown_from_file(f, **markdown_kwargs):
"""Render Markdown text from a file stream to HTML."""
s = StringIO()
markdownFromFile(input=f, output=s, **markdown_kwargs)
html = s.getvalue()
s.close()
return html
示例4: process_response
def process_response(self, request, response):
"""
Handler for processing a response. Dumps the profiling information
to the profile log file.
"""
timedloginfo = getattr(request, '_page_timedloginfo', None)
if timedloginfo:
timedloginfo.done()
if ('profiling' in request.GET and
getattr(settings, "LOGGING_ALLOW_PROFILING", False)):
init_profile_logger()
self.profiler.create_stats()
# Capture the stats
out = StringIO()
old_stdout, sys.stdout = sys.stdout, out
self.profiler.print_stats(1)
sys.stdout = old_stdout
profile_log = logging.getLogger("profile")
profile_log.log(logging.INFO,
"Profiling results for %s (HTTP %s):",
request.path, request.method)
profile_log.log(logging.INFO, out.getvalue().strip())
profile_log.log(logging.INFO,
'%d database queries made\n',
len(connection.queries))
queries = {}
for query in connection.queries:
sql = reformat_sql(query['sql'])
stack = ''.join(query['stack'][:-1])
time = query['time']
if sql in queries:
queries[sql].append((time, stack))
else:
queries[sql] = [(time, stack)]
times = {}
for sql, entries in six.iteritems(queries):
time = sum((float(entry[0]) for entry in entries))
tracebacks = '\n\n'.join((entry[1] for entry in entries))
times[time] = \
'SQL Query profile (%d times, %.3fs average)\n%s\n\n%s\n\n' % \
(len(entries), time / len(entries), sql, tracebacks)
sorted_times = sorted(six.iterkeys(times), reverse=1)
for time in sorted_times:
profile_log.log(logging.INFO, times[time])
return response
示例5: output_results
def output_results(profiler, options, stdout):
"""Generate the profiler output in the desired format. Implemented as a
separate function so it can be run as an exit handler (because management
commands often call exit() directly, bypassing the rest of the profile
command's handle() method)."""
profiler.create_stats()
if not options['sort']:
if not which('dot'):
stdout.write('Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation')
return
if not which('gprof2dot.py'):
stdout.write('Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler')
return
with tempfile.NamedTemporaryFile() as stats:
stats.write(marshal.dumps(profiler.stats))
stats.flush()
cmd = ('gprof2dot.py -f pstats {} | dot -Tpdf'.format(stats.name))
process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = process.communicate()[0]
return_code = process.poll()
if return_code:
stdout.write('gprof2dot/dot exited with {}'.format(return_code))
return
path = options['path']
with open(path, 'wb') as pdf_file:
pdf_file.write(output)
stdout.write('Wrote call graph to {}'.format(path))
else:
sort = options['sort']
if sort == 'file':
# Work around bug on Python versions >= 2.7.4
sort = 'fil'
out = StringIO()
stats = pstats.Stats(profiler, stream=out)
with mock.patch('pstats.func_strip_path') as mock_func_strip_path:
mock_func_strip_path.side_effect = func_strip_path
stats.strip_dirs()
restrictions = []
if options['pattern']:
restrictions.append(options['pattern'])
if options['fraction']:
restrictions.append(float(options['fraction']))
elif options['max_calls']:
restrictions.append(int(options['max_calls']))
elif not options['pattern']:
restrictions.append(.2)
stats.sort_stats(sort).print_stats(*restrictions)
if options['path']:
path = options['path']
with open(path, 'w') as text_file:
text_file.write(out.getvalue())
stdout.write('Wrote profiling statistics to {}'.format(path))
else:
stdout.write(out.getvalue())
示例6: test_generate_key
def test_generate_key(self):
stdout = StringIO()
try:
keygen.main(stdout=stdout, argv=[])
except SystemExit as exc:
self.assertEqual(exc.code, 0)
key = stdout.getvalue()
f = Fernet(key)
# Make sure this doesn't raise an error about a bad key.
f.decrypt(f.encrypt(b'whatever'))
示例7: process_response
def process_response(self, request, response):
if settings.YADP_ENABLED and settings.YADP_PROFILE_PARAMETER in request.REQUEST:
if self.error:
return text_response(response, self.error)
self.profiler.create_stats()
mode = request.REQUEST[settings.YADP_PROFILE_PARAMETER]
if mode == 'file':
# Work around bug on Python versions >= 2.7.4
mode = 'fil'
if not mode:
if not which('dot'):
return text_response(response, 'Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation')
if not which('gprof2dot.py'):
return text_response(response, 'Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler')
with tempfile.NamedTemporaryFile() as stats:
stats.write(marshal.dumps(self.profiler.stats))
stats.flush()
cmd = ('gprof2dot.py -f pstats {} | dot -Tpdf'.format(stats.name))
process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
output = process.communicate()[0]
return_code = process.poll()
if return_code:
raise Exception('gprof2dot/dot exited with {}'.format(return_code))
response.content = output
response['Content-Type'] = 'application/pdf'
return response
elif mode == 'help':
return text_response(response, ProfilerMiddleware.__doc__)
else:
out = StringIO()
stats = pstats.Stats(self.profiler, stream=out)
with mock.patch('pstats.func_strip_path') as mock_func_strip_path:
mock_func_strip_path.side_effect = func_strip_path
stats.strip_dirs()
restrictions = []
if settings.YADP_PATTERN_PARAMETER in request.REQUEST:
restrictions.append(request.REQUEST[settings.YADP_PATTERN_PARAMETER])
if settings.YADP_FRACTION_PARAMETER in request.REQUEST:
restrictions.append(float(request.REQUEST[settings.YADP_FRACTION_PARAMETER]))
elif settings.YADP_MAX_CALLS_PARAMETER in request.REQUEST:
restrictions.append(int(request.REQUEST[settings.YADP_MAX_CALLS_PARAMETER]))
elif settings.YADP_PATTERN_PARAMETER not in request.REQUEST:
restrictions.append(.2)
try:
stats.sort_stats(mode).print_stats(*restrictions)
except KeyError:
# Bad parameter for sorting stats
return text_response(response, "Bad parameter passed for sorting statistics.\n" + ProfilerMiddleware.__doc__)
return html_response(request, response, out.getvalue())
return response
示例8: render_markdown_from_file
def render_markdown_from_file(f):
"""Renders Markdown text to HTML.
The Markdown text will be sanitized to prevent injecting custom HTML.
It will also enable a few plugins for code highlighting and sane lists.
"""
s = StringIO()
markdownFromFile(input=f, output=s, **MARKDOWN_KWARGS)
html = s.getvalue()
s.close()
return html
示例9: _run_command
def _run_command(self, **options):
"""Run the profile command with the given options on the diffsettings command and capture the output"""
output = StringIO()
options = options.copy()
options['backend'] = 'cProfile'
options['testing'] = True
for option in ('fraction', 'max_calls', 'path', 'pattern', 'sort'):
if option not in options:
options[option] = None
call_command('profile', 'diffsettings', stdout=output, **options)
text = output.getvalue()
assert 'INSTALLED_APPS' in text
return text
示例10: encode
def encode(self, o, *args, **kwargs):
self.level = 0
self.doIndent = False
stream = StringIO()
self.xml = XMLGenerator(stream, settings.DEFAULT_CHARSET)
self.xml.startDocument()
self.startElement("rsp")
self.__encode(o, *args, **kwargs)
self.endElement("rsp")
self.xml.endDocument()
self.xml = None
return stream.getvalue()
示例11: pop
def pop(self):
"""Return the current contents of the buffer then clear it."""
s = self.buffer.getvalue()
self.buffer.close()
self.buffer = StringIO()
return s
示例12: process_response
def process_response(self, request, response):
if self.profile_parameter is not None:
if self.error:
return text_response(response, self.error)
self.profiler.create_stats()
mode = self.profile_parameter
if mode == 'file':
# Work around bug on Python versions >= 2.7.4
mode = 'fil'
if not mode:
if not which('dot'):
return text_response(response, _('Could not find "dot" from Graphviz; please install Graphviz to enable call graph generation'))
if not which('gprof2dot.py'):
return text_response(response, _('Could not find gprof2dot.py, which should have been installed by yet-another-django-profiler'))
return_code, output = run_gprof2dot(self.profiler)
if return_code:
raise Exception(_('gprof2dot.py exited with {return_code}').format(return_code=return_code))
set_content(response, output)
response['Content-Type'] = 'application/pdf'
return response
elif mode == 'help':
return text_response(response, ProfilerMiddleware.__doc__)
else:
out = StringIO()
stats = pstats.Stats(self.profiler, stream=out)
with mock.patch('pstats.func_strip_path') as mock_func_strip_path:
mock_func_strip_path.side_effect = func_strip_path
stats.strip_dirs()
restrictions = []
if self.pattern_parameter is not None:
restrictions.append(self.pattern_parameter)
if self.fraction_parameter is not None:
restrictions.append(float(self.fraction_parameter))
elif self.max_calls_parameter is not None:
restrictions.append(int(self.max_calls_parameter))
elif self.pattern_parameter is None:
restrictions.append(.2)
stats.sort_stats(mode).print_stats(*restrictions)
return text_response(response, out.getvalue())
return response
示例13: read_solarsimulator_plot_file
def read_solarsimulator_plot_file(filename, position):
"""Read a datafile from a solarsimulator measurement and return the content of
the voltage column and the selected current column.
:param filename: full path to the solarsimulator measurement data file
:param position: the position of the cell the currents of which should be read.
:type filename: str
:type position: str
:return:
all voltages in Volt, then all currents in Ampere
:rtype: list of float, list of float
:raises PlotError: if something wents wrong with interpreting the file (I/O,
unparseble data)
"""
try:
datafile_content = StringIO(open(filename).read())
except IOError:
raise PlotError("Data file could not be read.")
for line in datafile_content:
if line.startswith("# Positions:"):
positions = line.partition(":")[2].split()
break
else:
positions = []
try:
column = positions.index(position) + 1
except ValueError:
raise PlotError("Cell position not found in the datafile.")
datafile_content.seek(0)
try:
return numpy.loadtxt(datafile_content, usecols=(0, column), unpack=True)
except ValueError:
raise PlotError("Data file format was invalid.")
示例14: _cache_store_large_data
def _cache_store_large_data(cache, key, data, expiration, compress_large_data):
# We store large data in the cache broken into chunks that are 1M in size.
# To do this easily, we first pickle the data and compress it with zlib.
# This gives us a string which can be chunked easily. These are then stored
# individually in the cache as single-element lists (so the cache backend
# doesn't try to convert binary data to utf8). The number of chunks needed
# is stored in the cache under the unadorned key
file = StringIO()
pickler = pickle.Pickler(file)
pickler.dump(data)
data = file.getvalue()
if compress_large_data:
data = zlib.compress(data)
i = 0
while len(data) > CACHE_CHUNK_SIZE:
chunk = data[0:CACHE_CHUNK_SIZE]
data = data[CACHE_CHUNK_SIZE:]
cache.set(make_cache_key('%s-%d' % (key, i)), [chunk], expiration)
i += 1
cache.set(make_cache_key('%s-%d' % (key, i)), [data], expiration)
cache.set(make_cache_key(key), '%d' % (i + 1), expiration)
示例15: _migrate_extension_models
def _migrate_extension_models(self, ext_class):
"""Perform database migrations for an extension's models.
This will call out to Django Evolution to handle the migrations.
Args:
ext_class (djblets.extensions.extension.Extension):
The class for the extension to migrate.
"""
try:
from django_evolution.management.commands.evolve import \
Command as Evolution
except ImportError:
raise InstallExtensionError(
"Unable to migrate the extension's database tables. Django "
"Evolution is not installed.")
try:
stream = StringIO()
evolution = Evolution()
evolution.style = no_style()
evolution.execute(verbosity=0, interactive=False,
execute=True, hint=False,
compile_sql=False, purge=False,
database=False,
stdout=stream, stderr=stream)
output = stream.getvalue()
if output:
logging.info('Evolved extension models for %s: %s',
ext_class.id, stream.read())
stream.close()
except CommandError as e:
# Something went wrong while running django-evolution, so
# grab the output. We can't raise right away because we
# still need to put stdout back the way it was
output = stream.getvalue()
stream.close()
logging.error('Error evolving extension models: %s: %s',
e, output, exc_info=1)
load_error = self._store_load_error(ext_class.id, output)
raise InstallExtensionError(six.text_type(e), load_error)