当前位置: 首页>>代码示例>>Python>>正文


Python json_format.MessageToJson方法代码示例

本文整理汇总了Python中google.protobuf.json_format.MessageToJson方法的典型用法代码示例。如果您正苦于以下问题:Python json_format.MessageToJson方法的具体用法?Python json_format.MessageToJson怎么用?Python json_format.MessageToJson使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在google.protobuf.json_format的用法示例。


在下文中一共展示了json_format.MessageToJson方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: edit_recipes_cfg_pb2

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def edit_recipes_cfg_pb2(self):
    """Context manager for read/modify/write'ing the recipes.cfg file in this
    repo.

    Usage:

        with repo.edit_recipes_cfg_pb2() as pb:
          pb.deps['some_repo'].revision = 'abcdefg'

    Yields a recipes_cfg_pb2.RepoSpec object decoded from the current state of
    the recipes.cfg file. Any modifications done to this object will be recorded
    back to disk.
    """
    spec = self.recipes_cfg_pb2
    yield spec
    cfg_path = os.path.join(self.path, RECIPES_CFG_LOCATION_REL)
    with open(cfg_path, 'wb') as fil:
      fil.write(jsonpb.MessageToJson(spec, preserving_proto_field_name=True)) 
开发者ID:luci,项目名称:recipes-py,代码行数:20,代码来源:fake_recipe_deps.py

示例2: load_tensorflow_model

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def load_tensorflow_model(
        path,
        inputCol,
        tfInput,
        tfOutput,
        predictionCol='predicted',
        tfDropout=None,
        toKeepDropout=False):
    with tf.Session(graph=tf.Graph()) as sess:
        new_saver = tf.train.import_meta_graph(path + '.meta')
        split = path.split('/')
        if len(split) > 1:
            new_saver.restore(sess, tf.train.latest_checkpoint("/".join(split[:-1])))
        else:
            new_saver.restore(sess, tf.train.latest_checkpoint(split[0]))
        vs = tf.trainable_variables()
        weights = sess.run(vs)
        json_graph = json_format.MessageToJson(tf.train.export_meta_graph())

    weights = [w.tolist() for w in weights]
    json_weights = json.dumps(weights)
    return SparkAsyncDLModel(
        inputCol=inputCol, modelJson=json_graph, modelWeights=json_weights,
        tfInput=tfInput, tfOutput=tfOutput, predictionCol=predictionCol, tfDropout=tfDropout, toKeepDropout=toKeepDropout
    ) 
开发者ID:lifeomic,项目名称:sparkflow,代码行数:27,代码来源:tensorflow_model_loader.py

示例3: testExtensionSerializationJsonMatchesProto3Spec

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def testExtensionSerializationJsonMatchesProto3Spec(self):
    """See go/proto3-json-spec for spec.
    """
    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'
    message_text = json_format.MessageToJson(
        message
    )
    ext1_text = ('protobuf_unittest.TestMessageSetExtension1.'
                 'messageSetExtension')
    ext2_text = ('protobuf_unittest.TestMessageSetExtension2.'
                 'messageSetExtension')
    golden_text = ('{"messageSet": {'
                   '    "[%s]": {'
                   '        "i": 23'
                   '    },'
                   '    "[%s]": {'
                   '        "str": "foo"'
                   '    }'
                   '}}') % (ext1_text, ext2_text)
    self.assertEqual(json.loads(golden_text), json.loads(message_text)) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:26,代码来源:json_format_test.py

示例4: testJsonEscapeString

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def testJsonEscapeString(self):
    message = json_format_proto3_pb2.TestMessage()
    if sys.version_info[0] < 3:
      message.string_value = '&\n<\"\r>\b\t\f\\\001/\xe2\x80\xa8\xe2\x80\xa9'
    else:
      message.string_value = '&\n<\"\r>\b\t\f\\\001/'
      message.string_value += (b'\xe2\x80\xa8\xe2\x80\xa9').decode('utf-8')
    self.assertEqual(
        json_format.MessageToJson(message),
        '{\n  "stringValue": '
        '"&\\n<\\\"\\r>\\b\\t\\f\\\\\\u0001/\\u2028\\u2029"\n}')
    parsed_message = json_format_proto3_pb2.TestMessage()
    self.CheckParseBack(message, parsed_message)
    text = '{"int32Value": "\u0031"}'
    json_format.Parse(text, message)
    self.assertEqual(message.int32_value, 1) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:18,代码来源:json_format_test.py

示例5: testDurationMessage

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def testDurationMessage(self):
    message = json_format_proto3_pb2.TestDuration()
    message.value.seconds = 1
    message.repeated_value.add().seconds = 0
    message.repeated_value[0].nanos = 10
    message.repeated_value.add().seconds = -1
    message.repeated_value[1].nanos = -1000
    message.repeated_value.add().seconds = 10
    message.repeated_value[2].nanos = 11000000
    message.repeated_value.add().seconds = -315576000000
    message.repeated_value.add().seconds = 315576000000
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, True)),
        json.loads('{'
                   '"value": "1s",'
                   '"repeatedValue": ['
                   '  "0.000000010s",'
                   '  "-1.000001s",'
                   '  "10.011s",'
                   '  "-315576000000s",'
                   '  "315576000000s"'
                   ']'
                   '}'))
    parsed_message = json_format_proto3_pb2.TestDuration()
    self.CheckParseBack(message, parsed_message) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:27,代码来源:json_format_test.py

示例6: testPreservingProtoFieldNames

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def testPreservingProtoFieldNames(self):
    message = json_format_proto3_pb2.TestMessage()
    message.int32_value = 12345
    self.assertEqual('{\n  "int32Value": 12345\n}',
                     json_format.MessageToJson(message))
    self.assertEqual('{\n  "int32_value": 12345\n}',
                     json_format.MessageToJson(message, False, True))
    # When including_default_value_fields is True.
    message = json_format_proto3_pb2.TestTimestamp()
    self.assertEqual('{\n  "repeatedValue": []\n}',
                     json_format.MessageToJson(message, True, False))
    self.assertEqual('{\n  "repeated_value": []\n}',
                     json_format.MessageToJson(message, True, True))

    # Parsers accept both original proto field names and lowerCamelCase names.
    message = json_format_proto3_pb2.TestMessage()
    json_format.Parse('{"int32Value": 54321}', message)
    self.assertEqual(54321, message.int32_value)
    json_format.Parse('{"int32_value": 12345}', message)
    self.assertEqual(12345, message.int32_value) 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:22,代码来源:json_format_test.py

示例7: testJsonEscapeString

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def testJsonEscapeString(self):
    message = json_format_proto3_pb2.TestMessage()
    if sys.version_info[0] < 3:
      message.string_value = '&\n<\"\r>\b\t\f\\\001/\xe2\x80\xa8\xe2\x80\xa9'
    else:
      message.string_value = '&\n<\"\r>\b\t\f\\\001/'
      message.string_value += (b'\xe2\x80\xa8\xe2\x80\xa9').decode('utf-8')
    self.assertEqual(
        json_format.MessageToJson(message),
        '{\n  "stringValue": '
        '"&\\n<\\\"\\r>\\b\\t\\f\\\\\\u0001/\\u2028\\u2029"\n}')
    parsed_message = json_format_proto3_pb2.TestMessage()
    self.CheckParseBack(message, parsed_message)
    text = u'{"int32Value": "\u0031"}'
    json_format.Parse(text, message)
    self.assertEqual(message.int32_value, 1) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:18,代码来源:json_format_test.py

示例8: testListValueMessage

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def testListValueMessage(self):
    message = json_format_proto3_pb2.TestListValue()
    message.value.values.add().number_value = 11.1
    message.value.values.add().null_value = 0
    message.value.values.add().bool_value = True
    message.value.values.add().string_value = 'hello'
    message.value.values.add().struct_value['name'] = 'Jim'
    message.repeated_value.add().values.add().number_value = 1
    message.repeated_value.add()
    self.assertEqual(
        json.loads(json_format.MessageToJson(message, False)),
        json.loads(
            '{"value": [11.1, null, true, "hello", {"name": "Jim"}]\n,'
            '"repeatedValue": [[1], []]}'))
    parsed_message = json_format_proto3_pb2.TestListValue()
    self.CheckParseBack(message, parsed_message) 
开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:18,代码来源:json_format_test.py

示例9: make_checkpoint

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def make_checkpoint(self, base_dir, prefix, version=None):
        base_dir = os.path.abspath(base_dir)
        os.makedirs(base_dir, exist_ok=True)
        checkpoint_params = self.checkpoint_params
        if version:
            checkpoint_path = os.path.abspath(os.path.join(base_dir, "{}{}.ckpt".format(prefix, version)))
        else:
            checkpoint_path = os.path.abspath(os.path.join(base_dir, "{}{:08d}.ckpt".format(prefix, checkpoint_params.iter + 1)))
        print("Storing checkpoint to '{}'".format(checkpoint_path))
        self.model.save(checkpoint_path + '.h5', overwrite=True)
        checkpoint_params.version = Checkpoint.VERSION
        checkpoint_params.loss_stats[:] = self.vis_cb.loss_stats.values
        checkpoint_params.ler_stats[:] = self.vis_cb.ler_stats.values
        checkpoint_params.dt_stats[:] = self.vis_cb.dt_stats.values
        checkpoint_params.total_time = time.time() - self.train_start_time
        checkpoint_params.early_stopping_best_accuracy = self.early_stopping_best_accuracy
        checkpoint_params.early_stopping_best_cur_nbest = self.early_stopping_best_cur_nbest
        checkpoint_params.early_stopping_best_at_iter = self.early_stopping_best_at_iter

        with open(checkpoint_path + ".json", 'w') as f:
            f.write(json_format.MessageToJson(checkpoint_params))

        return checkpoint_path 
开发者ID:Calamari-OCR,项目名称:calamari,代码行数:25,代码来源:earlystopping.py

示例10: main

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--files", type=str, default=[], nargs="+", required=True,
                        help="Protobuf files to convert")
    parser.add_argument("--logits", action="store_true",
                        help="Do write logits")
    args = parser.parse_args()

    files = glob_all(args.files)
    for file in tqdm(files, desc="Converting"):
        predictions = Predictions()
        with open(file, 'rb') as f:
            predictions.ParseFromString(f.read())

        if not args.logits:
            for prediction in predictions.predictions:
                prediction.logits.rows = 0
                prediction.logits.cols = 0
                prediction.logits.data[:] = []

        out_json_path = split_all_ext(file)[0] + ".json"
        with open(out_json_path, 'w') as f:
            f.write(MessageToJson(predictions, including_default_value_fields=True)) 
开发者ID:Calamari-OCR,项目名称:calamari,代码行数:25,代码来源:pred_to_json.py

示例11: to_json

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def to_json(self) -> Text:
    """Convert from an object to JSON representation of the __dict__ attribute.

    Custom generators and slice_functions are skipped, meaning that they will
    not be used when running TFDV in a setting where the stats options have been
    json-serialized, first. This will happen in the case where TFDV is run as a
    TFX component. The schema proto will be json_encoded.

    Returns:
      A JSON representation of a filtered version of __dict__.
    """
    options_dict = copy.copy(self.__dict__)
    options_dict['_slice_functions'] = None
    options_dict['_generators'] = None
    if self.schema:
      del options_dict['_schema']
      options_dict['schema_json'] = json_format.MessageToJson(self.schema)
    return json.dumps(options_dict) 
开发者ID:tensorflow,项目名称:data-validation,代码行数:20,代码来源:stats_options.py

示例12: parse_trace_json

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def parse_trace_json(trace):
  """Converts a binary-encoded MasterTrace proto to a JSON parser trace.

  Args:
    trace: Binary string containing a MasterTrace.

  Returns:
    JSON str, as expected by visualization tools.
  """
  as_proto = trace_pb2.MasterTrace.FromString(trace)

  # Sanitize non-UTF8 captions. One case where this occurs is for byte LSTMs,
  # which may be processing a sub-sequence of a UTF-8 multi-byte sequence.
  for component_trace in as_proto.component_trace:
    for step_trace in component_trace.step_trace:
      if isinstance(step_trace.caption, str):
        try:
          unicode(step_trace.caption, 'utf-8')
        except UnicodeDecodeError:
          step_trace.caption = repr(step_trace.caption)  # Safe encoding.

  as_json = json_format.MessageToJson(
      as_proto, preserving_proto_field_name=True)
  return as_json 
开发者ID:rky0930,项目名称:yolo_v2,代码行数:26,代码来源:visualization.py

示例13: _get_param

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def _get_param(self):
        header = self.header

        weight_dict = {}
        intercept = 0
        if not self.use_encrypt:
            lr_vars = self.model_weights.coef_
            for idx, header_name in enumerate(header):
                coef_i = lr_vars[idx]
                weight_dict[header_name] = coef_i
            intercept = self.model_weights.intercept_

        param_protobuf_obj = lr_model_param_pb2.LRModelParam(iters=self.n_iter_,
                                                             loss_history=self.loss_history,
                                                             is_converged=self.is_converged,
                                                             weight=weight_dict,
                                                             intercept=intercept,
                                                             header=header)
        from google.protobuf import json_format
        json_result = json_format.MessageToJson(param_protobuf_obj)
        LOGGER.debug("json_result: {}".format(json_result))
        return param_protobuf_obj 
开发者ID:FederatedAI,项目名称:FATE,代码行数:24,代码来源:homo_lr_host.py

示例14: _get_param

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def _get_param(self):
        header = self.header
        LOGGER.debug("In get_param, header: {}".format(header))
        if header is None:
            param_protobuf_obj = lr_model_param_pb2.LRModelParam()
            return param_protobuf_obj
        if self.need_one_vs_rest:
            # one_vs_rest_class = list(map(str, self.one_vs_rest_obj.classes))
            one_vs_rest_result = self.one_vs_rest_obj.save(lr_model_param_pb2.SingleModel)
            single_result = {'header': header, 'need_one_vs_rest': True}
        else:
            one_vs_rest_result = None
            single_result = self.get_single_model_param()
            single_result['need_one_vs_rest'] = False
        single_result['one_vs_rest_result'] = one_vs_rest_result
        LOGGER.debug("in _get_param, single_result: {}".format(single_result))

        param_protobuf_obj = lr_model_param_pb2.LRModelParam(**single_result)
        json_result = json_format.MessageToJson(param_protobuf_obj)
        LOGGER.debug("json_result: {}".format(json_result))
        return param_protobuf_obj 
开发者ID:FederatedAI,项目名称:FATE,代码行数:23,代码来源:base_logistic_regression.py

示例15: _get_param

# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import MessageToJson [as 别名]
def _get_param(self):
        header = self.header
        LOGGER.debug("In get_param, header: {}".format(header))
        if header is None:
            param_protobuf_obj = fm_model_param_pb2.FMModelParam()
            return param_protobuf_obj
        if self.need_one_vs_rest:
            # one_vs_rest_class = list(map(str, self.one_vs_rest_obj.classes))
            one_vs_rest_result = self.one_vs_rest_obj.save(fm_model_param_pb2.SingleModel)
            single_result = {'header': header, 'need_one_vs_rest': True}
        else:
            one_vs_rest_result = None
            single_result = self.get_single_model_param()
            single_result['need_one_vs_rest'] = False
        single_result['one_vs_rest_result'] = one_vs_rest_result
        LOGGER.debug("in _get_param, single_result: {}".format(single_result))

        param_protobuf_obj = fm_model_param_pb2.FMModelParam(**single_result)
        json_result = json_format.MessageToJson(param_protobuf_obj)
        LOGGER.debug("json_result: {}".format(json_result))
        return param_protobuf_obj 
开发者ID:FederatedAI,项目名称:FATE,代码行数:23,代码来源:base_factorization_machine.py


注:本文中的google.protobuf.json_format.MessageToJson方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。