本文整理汇总了Python中six.moves.cStringIO.write方法的典型用法代码示例。如果您正苦于以下问题:Python cStringIO.write方法的具体用法?Python cStringIO.write怎么用?Python cStringIO.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.cStringIO
的用法示例。
在下文中一共展示了cStringIO.write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: IncludedResponse
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
class IncludedResponse(object):
def __init__(self):
self.headers = None
self.status = None
self.output = StringIO()
self.str = None
def close(self):
self.str = self.output.getvalue()
self.output.close()
self.output = None
def write(self, s):
assert self.output is not None, (
"This response has already been closed and no further data "
"can be written.")
self.output.write(s)
def __str__(self):
return self.body
def body__get(self):
if self.str is None:
return self.output.getvalue()
else:
return self.str
body = property(body__get)
示例2: generate_roles_data_from_directory
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def generate_roles_data_from_directory(directory, roles, validate=True):
"""Generate a roles data file using roles from a local path
:param directory local filesystem path to the roles
:param roles ordered list of roles
:param validate validate the metadata format in the role yaml files
:returns string contents of the roles_data.yaml
"""
available_roles = get_roles_list_from_directory(directory)
check_role_exists(available_roles, roles)
output = StringIO()
header = ["#" * 79,
"# File generated by TripleO",
"#" * 79,
""]
output.write("\n".join(header))
for role in roles:
defined_role = role.split(':')[0]
file_path = os.path.join(directory, "{}.yaml".format(defined_role))
if validate:
validate_role_yaml(role_path=file_path)
with open(file_path, "r") as f:
if ':' in role:
generated_role = role.split(':')[1]
content = generate_role_with_colon_format(f.read(),
defined_role,
generated_role)
output.write(content)
else:
shutil.copyfileobj(f, output)
return output.getvalue()
示例3: generate_index
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def generate_index(self):
display_name = escape_html(self.dir_name if self.dir_name else "/")
html = StringIO()
html.write(self.header % locals())
# If this is a subdirectory, create a link back to the parent.
if self.dir_name:
parent_dirname = ("/" + self.dir_name).rsplit("/", 1)[0]
html.write(self.parent_backlink % locals())
for subdir_name, subdir in sorted(iteritems(self.subdirs)):
subdir_link = escape_html(url_quote(
self.dir_name + "/" + subdir_name if self.dir_name
else subdir_name))
subdir_name = escape_html(subdir_name)
html.write(self.subdir_link % locals())
for filename, key in sorted(iteritems(self.contents)):
ext = splitext(filename)[-1]
icon_name = self.icons.get(ext, "binary.png")
suffix_type = self.suffix_types.get(ext, " ")
file_link = escape_html(url_quote(key.name))
filename = escape_html(filename)
last_modified = escape_html(key.last_modified)
size = str(key.size)
description = ""
html.write(self.file_link % locals())
html.write(self.footer % locals())
return html.getvalue()
示例4: __str__
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def __str__(self):
from six.moves import cStringIO as StringIO
temp_file = StringIO()
for order in self:
temp_file.write("%s\n" % str(order))
#temp_file.write("%s\n" % str(self.head_order))
return temp_file.getvalue()
示例5: bind_config
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def bind_config(self):
"""
Return the BIND configuration for this record.
"""
result = StringIO()
for value in self.values:
result.write("%-63s %7d IN %-7s " % (
self.name, self.ttl if self.ttl is not None else 60,
self.record_type))
if self.record_type == "SOA":
parts = value.split()
ns = parts[0]
email = parts[1]
soa_values = parts[2:]
result.write("%s %s (%s)" % (ns, email, " ".join(soa_values)))
elif self.record_type == "NS":
if "." in value and not value.endswith("."):
# Some Route 53 NS records are missing the terminating .
value += "."
result.write(value)
else:
result.write(value)
result.write("\n")
return result.getvalue()
示例6: __str__
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def __str__(self):
from six.moves import cStringIO as StringIO
file_str = StringIO()
for order in self:
file_str.write("%s\n" % str(order))
return file_str.getvalue()
示例7: _parse_operator
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def _parse_operator(segment, iterator):
"""Parses the operator (eg. '==' or '<')."""
stream = StringIO()
for character in iterator:
if character == constants.NEGATION[1]:
if stream.tell():
# Negation can only occur at the start of an operator.
raise ValueError('Unexpected negation.')
# We've been negated.
segment.negated = not segment.negated
continue
if (stream.getvalue() + character not in OPERATOR_SYMBOL_MAP and
stream.getvalue() + character not in OPERATOR_BEGIN_CHARS):
# We're no longer an operator.
break
# Expand the operator
stream.write(character)
# Check for existance.
text = stream.getvalue()
if text not in OPERATOR_SYMBOL_MAP:
# Doesn't exist because of a mis-placed negation in the middle
# of the path.
raise ValueError('Unexpected negation.')
# Set the found operator.
segment.operator = OPERATOR_SYMBOL_MAP[text]
# Return the remaining characters.
return chain(character, iterator)
示例8: _MiniPPrinter
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
class _MiniPPrinter(object):
def __init__(self):
self._out = StringIO()
self.indentation = 0
def text(self, text):
self._out.write(text)
def breakable(self, sep=" "):
self._out.write(sep)
def begin_group(self, _, text):
self.text(text)
def end_group(self, _, text):
self.text(text)
def pretty(self, obj):
if hasattr(obj, "_repr_pretty_"):
obj._repr_pretty_(self, False)
else:
self.text(repr(obj))
def getvalue(self):
return self._out.getvalue()
示例9: scan
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def scan(path):
"""
Performs an in-process binary module scan. That means the module is
loaded (imported) into the current Python interpreter.
"path" - a path to a binary module to scan
Returns a CIX 2.0 XML string.
"""
from gencix.python import gencixcore as gencix
name,_ = os.path.splitext(os.path.basename(path))
dir = os.path.dirname(path)
root = gencix.Element('codeintel', version='2.0', name=name)
gencix.docmodule(name, root, usefile=True, dir=dir)
gencix.perform_smart_analysis(root)
gencix.prettify(root)
tree = gencix.ElementTree(root)
stream = StringIO()
try:
stream.write('<?xml version="1.0" encoding="UTF-8"?>\n')
tree.write(stream)
return stream.getvalue()
finally:
stream.close()
示例10: _decompress_xz
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def _decompress_xz(filename):
"""Eumlates an option function in read mode for xz.
See the comment in _compress_xz for more information.
This function tries to emulate the lzma module as much as
possible
"""
if not filename.endswith('.xz'):
filename = '{}.xz'.format(filename)
try:
with open(os.devnull, 'w') as null:
string = subprocess.check_output(
['xz', '--decompress', '--stdout', filename],
stderr=null)
except OSError as e:
if e.errno == errno.ENOENT:
raise exceptions.PiglitFatalError(
'No xz binary available')
raise
# We need a file-like object, so the contents must be placed in
# a StringIO object.
io = StringIO()
io.write(string)
io.seek(0)
yield io
io.close()
示例11: ask_book_str
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def ask_book_str(self):
# Efficient string concat
file_str = StringIO()
file_str.write("------- Asks --------\n")
if self.asks != None and len(self.asks) > 0:
for k, v in self.asks.price_tree.items():
file_str.write("%s" % v)
return file_str.getvalue()
示例12: bid_book_str
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def bid_book_str(self):
# Efficient string concat
file_str = StringIO()
file_str.write("------- Bids --------\n")
if self.bids != None and len(self.bids) > 0:
for k, v in self.bids.price_tree.items(reverse=True):
file_str.write("%s" % v)
return file_str.getvalue()
示例13: ask_book_aggregated_str
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def ask_book_aggregated_str(self):
# Efficient string concat
file_str = StringIO()
file_str.write("------- Asks --------\n")
if self.asks != None and len(self.asks) > 0:
for k, v in self.asks.price_tree.items():
# aggregate
file_str.write("%s\[email protected]\t%.4f\n" % (v.volume, v.head_order.price / float(10000)))
return file_str.getvalue()
示例14: test_error
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def test_error(self):
sio = StringIO()
sio.write("bogus")
sio.seek(0)
r = flow.FlowReader(sio)
tutils.raises(flow.FlowReadError, list, r.stream())
f = flow.FlowReadError("foo")
assert f.strerror == "foo"
示例15: test_error
# 需要导入模块: from six.moves import cStringIO [as 别名]
# 或者: from six.moves.cStringIO import write [as 别名]
def test_error(self):
sio = StringIO()
sio.write("bogus")
sio.seek(0)
r = flow.FlowReader(sio)
tutils.raises(FlowReadException, list, r.stream())
f = FlowReadException("foo")
assert str(f) == "foo"