本文整理汇总了Python中google.protobuf.text_format.MessageToString方法的典型用法代码示例。如果您正苦于以下问题:Python text_format.MessageToString方法的具体用法?Python text_format.MessageToString怎么用?Python text_format.MessageToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.text_format
的用法示例。
在下文中一共展示了text_format.MessageToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RunSteps
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def RunSteps(api):
text = text_format.MessageToString(api.buildbucket.build)
api.step('build', ['echo'] + text.splitlines())
api.step('hostname', ['echo', api.buildbucket.host])
api.step('is_critical', ['echo', api.buildbucket.is_critical()])
child_build_tags = [
'%s:%s' % t
for t in api.buildbucket.tags_for_child_build.iteritems()
]
api.step('tags_for_child_build', ['echo'] + child_build_tags)
api.assertions.assertEqual(
api.buildbucket.bucket_v1,
api.properties.get('expected_bucket_v1'))
api.assertions.assertEqual(
api.buildbucket.builder_name,
api.buildbucket.build.builder.builder)
api.assertions.assertEqual(
api.buildbucket.gitiles_commit,
api.buildbucket.build.input.gitiles_commit)
示例2: main
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def main(argv):
if len(argv) != 2:
sys.exit('One argument, a directory containing a font family')
fontdir = argv[1]
is_new = True
old_metadata_file = os.path.join(fontdir, 'METADATA.pb')
if os.path.isfile(old_metadata_file):
is_new = False
metadata = _MakeMetadata(fontdir, is_new)
text_proto = text_format.MessageToString(metadata, as_utf8=True)
desc = os.path.join(fontdir, 'DESCRIPTION.en_us.html')
if os.path.isfile(desc):
print('DESCRIPTION.en_us.html exists')
else:
_WriteTextFile(desc, 'N/A')
_WriteTextFile(os.path.join(fontdir, 'METADATA.pb'), text_proto)
示例3: save_pipeline_config
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def save_pipeline_config(pipeline_config, directory):
"""Saves a pipeline config text file to disk.
Args:
pipeline_config: A pipeline_pb2.TrainEvalPipelineConfig.
directory: The model directory into which the pipeline config file will be
saved.
"""
if not file_io.file_exists(directory):
file_io.recursive_create_dir(directory)
pipeline_config_path = os.path.join(directory, "pipeline.config")
config_text = text_format.MessageToString(pipeline_config)
with tf.gfile.Open(pipeline_config_path, "wb") as f:
tf.logging.info("Writing pipeline config file to %s",
pipeline_config_path)
f.write(config_text)
示例4: testPrintExotic
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintExotic(self, message_module):
message = message_module.TestAllTypes()
message.repeated_int64.append(-9223372036854775808)
message.repeated_uint64.append(18446744073709551615)
message.repeated_double.append(123.456)
message.repeated_double.append(1.23e22)
message.repeated_double.append(1.23e-18)
message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
message.repeated_string.append('\u00fc\ua71f')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(message)),
'repeated_int64: -9223372036854775808\n'
'repeated_uint64: 18446744073709551615\n'
'repeated_double: 123.456\n'
'repeated_double: 1.23e+22\n'
'repeated_double: 1.23e-18\n'
'repeated_string:'
' "\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
'repeated_string: "\\303\\274\\352\\234\\237"\n')
示例5: testPrintExoticAsOneLine
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintExoticAsOneLine(self, message_module):
message = message_module.TestAllTypes()
message.repeated_int64.append(-9223372036854775808)
message.repeated_uint64.append(18446744073709551615)
message.repeated_double.append(123.456)
message.repeated_double.append(1.23e22)
message.repeated_double.append(1.23e-18)
message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
message.repeated_string.append('\u00fc\ua71f')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(
message, as_one_line=True)),
'repeated_int64: -9223372036854775808'
' repeated_uint64: 18446744073709551615'
' repeated_double: 123.456'
' repeated_double: 1.23e+22'
' repeated_double: 1.23e-18'
' repeated_string: '
'"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""'
' repeated_string: "\\303\\274\\352\\234\\237"')
示例6: testPrintMessageSet
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintMessageSet(self):
message = unittest_mset_pb2.TestMessageSetContainer()
ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
message.message_set.Extensions[ext1].i = 23
message.message_set.Extensions[ext2].str = 'foo'
self.CompareToGoldenText(
text_format.MessageToString(message), 'message_set {\n'
' [protobuf_unittest.TestMessageSetExtension1] {\n'
' i: 23\n'
' }\n'
' [protobuf_unittest.TestMessageSetExtension2] {\n'
' str: \"foo\"\n'
' }\n'
'}\n')
message = message_set_extensions_pb2.TestMessageSet()
ext = message_set_extensions_pb2.message_set_extension3
message.Extensions[ext].text = 'bar'
self.CompareToGoldenText(
text_format.MessageToString(message),
'[google.protobuf.internal.TestMessageSetExtension3] {\n'
' text: \"bar\"\n'
'}\n')
示例7: testPrintMessageSetAsOneLine
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintMessageSetAsOneLine(self):
message = unittest_mset_pb2.TestMessageSetContainer()
ext1 = unittest_mset_pb2.TestMessageSetExtension1.message_set_extension
ext2 = unittest_mset_pb2.TestMessageSetExtension2.message_set_extension
message.message_set.Extensions[ext1].i = 23
message.message_set.Extensions[ext2].str = 'foo'
self.CompareToGoldenText(
text_format.MessageToString(message, as_one_line=True),
'message_set {'
' [protobuf_unittest.TestMessageSetExtension1] {'
' i: 23'
' }'
' [protobuf_unittest.TestMessageSetExtension2] {'
' str: \"foo\"'
' }'
' }')
示例8: testPrintMessageExpandAnyRepeated
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintMessageExpandAnyRepeated(self):
packed_message = unittest_pb2.OneString()
message = any_test_pb2.TestAny()
packed_message.data = 'string0'
message.repeated_any_value.add().Pack(packed_message)
packed_message.data = 'string1'
message.repeated_any_value.add().Pack(packed_message)
self.assertEqual(
text_format.MessageToString(message),
'repeated_any_value {\n'
' [type.googleapis.com/protobuf_unittest.OneString] {\n'
' data: "string0"\n'
' }\n'
'}\n'
'repeated_any_value {\n'
' [type.googleapis.com/protobuf_unittest.OneString] {\n'
' data: "string1"\n'
' }\n'
'}\n')
示例9: testPrettyPrintMultiLine
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrettyPrintMultiLine(self, message_module):
def printer(m, indent, as_one_line):
if m.DESCRIPTOR == message_module.TestAllTypes.NestedMessage.DESCRIPTOR:
line_deliminator = (' ' if as_one_line else '\n') + ' ' * indent
return 'My lucky number is:%s%s' % (line_deliminator, m.bb)
return None
message = message_module.TestAllTypes()
msg = message.repeated_nested_message.add()
msg.bb = 42
self.CompareToGoldenText(
text_format.MessageToString(
message, as_one_line=True, message_formatter=printer),
'repeated_nested_message { My lucky number is: 42 }')
self.CompareToGoldenText(
text_format.MessageToString(
message, as_one_line=False, message_formatter=printer),
'repeated_nested_message {\n My lucky number is:\n 42\n}\n')
示例10: testPrettyPrintMultipleParts
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrettyPrintMultipleParts(self, message_module):
def printer(m, indent, as_one_line):
del indent, as_one_line
if m.DESCRIPTOR == message_module.TestAllTypes.NestedMessage.DESCRIPTOR:
return 'My lucky number is %s' % m.bb
return None
message = message_module.TestAllTypes()
message.optional_int32 = 61
msg = message.repeated_nested_message.add()
msg.bb = 42
msg = message.repeated_nested_message.add()
msg.bb = 99
msg = message.optional_nested_message
msg.bb = 1
self.CompareToGoldenText(
text_format.MessageToString(
message, as_one_line=True, message_formatter=printer),
('optional_int32: 61 '
'optional_nested_message { My lucky number is 1 } '
'repeated_nested_message { My lucky number is 42 } '
'repeated_nested_message { My lucky number is 99 }'))
示例11: visualize_embeddings
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def visualize_embeddings(summary_writer, config):
"""Stores a config file used by the embedding projector.
Args:
summary_writer: The summary writer used for writting events.
config: `tf.contrib.tensorboard.plugins.projector.ProjectorConfig`
proto that holds the configuration for the projector such as paths to
checkpoint files and metadata files for the embeddings. If
`config.model_checkpoint_path` is none, it defaults to the
`logdir` used by the summary_writer.
Raises:
ValueError: If the summary writer does not have a `logdir`.
"""
logdir = summary_writer.get_logdir()
# Sanity checks.
if logdir is None:
raise ValueError('Summary writer must have a logdir')
# Saving the config file in the logdir.
config_pbtxt = text_format.MessageToString(config)
file_io.write_string_to_file(
os.path.join(logdir, projector_plugin.PROJECTOR_FILENAME), config_pbtxt)
示例12: testPrintExotic
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintExotic(self, message_module):
message = message_module.TestAllTypes()
message.repeated_int64.append(-9223372036854775808)
message.repeated_uint64.append(18446744073709551615)
message.repeated_double.append(123.456)
message.repeated_double.append(1.23e22)
message.repeated_double.append(1.23e-18)
message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
message.repeated_string.append(u'\u00fc\ua71f')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(message)),
'repeated_int64: -9223372036854775808\n'
'repeated_uint64: 18446744073709551615\n'
'repeated_double: 123.456\n'
'repeated_double: 1.23e+22\n'
'repeated_double: 1.23e-18\n'
'repeated_string:'
' "\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""\n'
'repeated_string: "\\303\\274\\352\\234\\237"\n')
示例13: testPrintExoticAsOneLine
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintExoticAsOneLine(self, message_module):
message = message_module.TestAllTypes()
message.repeated_int64.append(-9223372036854775808)
message.repeated_uint64.append(18446744073709551615)
message.repeated_double.append(123.456)
message.repeated_double.append(1.23e22)
message.repeated_double.append(1.23e-18)
message.repeated_string.append('\000\001\a\b\f\n\r\t\v\\\'"')
message.repeated_string.append(u'\u00fc\ua71f')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(
message, as_one_line=True)),
'repeated_int64: -9223372036854775808'
' repeated_uint64: 18446744073709551615'
' repeated_double: 123.456'
' repeated_double: 1.23e+22'
' repeated_double: 1.23e-18'
' repeated_string: '
'"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\\'\\""'
' repeated_string: "\\303\\274\\352\\234\\237"')
示例14: testPrintInIndexOrder
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testPrintInIndexOrder(self):
message = unittest_pb2.TestFieldOrderings()
message.my_string = '115'
message.my_int = 101
message.my_float = 111
message.optional_nested_message.oo = 0
message.optional_nested_message.bb = 1
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(
message, use_index_order=True)),
'my_string: \"115\"\nmy_int: 101\nmy_float: 111\n'
'optional_nested_message {\n oo: 0\n bb: 1\n}\n')
self.CompareToGoldenText(
self.RemoveRedundantZeros(text_format.MessageToString(message)),
'my_int: 101\nmy_string: \"115\"\nmy_float: 111\n'
'optional_nested_message {\n bb: 1\n oo: 0\n}\n')
示例15: testMapOrderSemantics
# 需要导入模块: from google.protobuf import text_format [as 别名]
# 或者: from google.protobuf.text_format import MessageToString [as 别名]
def testMapOrderSemantics(self):
golden_lines = self.ReadGolden('map_test_data.txt')
# The C++ implementation emits defaulted-value fields, while the Python
# implementation does not. Adjusting for this is awkward, but it is
# valuable to test against a common golden file.
line_blacklist = (' key: 0\n', ' value: 0\n', ' key: false\n',
' value: false\n')
golden_lines = [line for line in golden_lines if line not in line_blacklist]
message = map_unittest_pb2.TestMap()
text_format.ParseLines(golden_lines, message)
candidate = text_format.MessageToString(message)
# The Python implementation emits "1.0" for the double value that the C++
# implementation emits as "1".
candidate = candidate.replace('1.0', '1', 2)
self.assertMultiLineEqual(candidate, ''.join(golden_lines))
# Tests of proto2-only features (MessageSet, extensions, etc.).