當前位置: 首頁>>代碼示例>>Python>>正文


Python FieldDescriptor.TYPE_ENUM屬性代碼示例

本文整理匯總了Python中google.protobuf.descriptor.FieldDescriptor.TYPE_ENUM屬性的典型用法代碼示例。如果您正苦於以下問題:Python FieldDescriptor.TYPE_ENUM屬性的具體用法?Python FieldDescriptor.TYPE_ENUM怎麽用?Python FieldDescriptor.TYPE_ENUM使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在google.protobuf.descriptor.FieldDescriptor的用法示例。


在下文中一共展示了FieldDescriptor.TYPE_ENUM屬性的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _get_field_value_adaptor

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def _get_field_value_adaptor(pb, field, use_enum_labels,
                             including_default_value_fields):
    if field.type == FieldDescriptor.TYPE_MESSAGE:
        # recursively encode protobuf sub-message
        return lambda pb: msg2dict(
            pb, use_enum_labels=use_enum_labels,
            including_default_value_fields=including_default_value_fields)

    if use_enum_labels and field.type == FieldDescriptor.TYPE_ENUM:
        return lambda value: enum_label_name(field, value)

    if field.type in TYPE_CALLABLE_MAP:
        return TYPE_CALLABLE_MAP[field.type]

    raise TypeError("Field %s.%s has unrecognised type id %d" % (
        pb.__class__.__name__, field.name, field.type)) 
開發者ID:shanbay,項目名稱:sea,代碼行數:18,代碼來源:format.py

示例2: protobuf_to_dict

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def protobuf_to_dict(pb, containers=CONTAINER_MAP, converters=TYPE_CALLABLE_MAP):
    result = message_to_container(pb, containers)

    # for field, value in pb.ListFields():  # only non-empty fields
    for field in pb.DESCRIPTOR.fields:  # empty fields too
        value = getattr(pb, field.name)
        if (field.message_type and field.message_type.has_options and
                field.message_type.GetOptions().map_entry):
            converter = dict
        elif field.type == FieldDescriptor.TYPE_MESSAGE:
            # recursively encode protobuf sub-message
            converter = partial(protobuf_to_dict, containers=containers,
                                converters=converters)
        elif field.type == FieldDescriptor.TYPE_ENUM:
            converter = partial(enum_to_label, field)
        else:
            converter = converters[field.type]

        if field.label == FieldDescriptor.LABEL_REPEATED:
            converter = partial(map, converter)

        result[field.name] = converter(value)

    return result 
開發者ID:daskos,項目名稱:mentor,代碼行數:26,代碼來源:protobuf.py

示例3: json2pb

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def json2pb(pb, js):
    """ convert JSON string to google.protobuf.descriptor instance
    :param pb: protobuf class to fill
    :param js: json input data
    """
    for field in pb.DESCRIPTOR.fields:
        if field.name not in js:
            continue
        if field.type == FD.TYPE_MESSAGE:
            pass
        elif field.type in _js2ftype:
            ftype = _js2ftype[field.type]
        else:
            raise ParseError(
                "Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,))
        value = js[field.name]
        if field.label == FD.LABEL_REPEATED:
            pb_value = getattr(pb, field.name, None)
            for v in value:
                if field.type == FD.TYPE_MESSAGE:
                    json2pb(pb_value.add(), v)
                else:
                    pb_value.append(ftype(v))
        else:
            if field.type == FD.TYPE_MESSAGE:
                json2pb(getattr(pb, field.name, None), value)
            elif field.type == FD.TYPE_ENUM:
                real_value = field.enum_type.values_by_name[value].number
                setattr(pb, field.name, real_value)
            else:
                setattr(pb, field.name, ftype(value))
    return pb 
開發者ID:RobinDavid,項目名稱:idasec,代碼行數:34,代碼來源:protobuf_json.py

示例4: pb2json

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def pb2json(pb):
    """ convert google.protobuf.descriptor instance to JSON string
    :param pb: protobuf class to be converted in json
    """
    js = {}
    # fields = pb.DESCRIPTOR.fields #all fields
    fields = pb.ListFields()  # only filled (including extensions)
    for field, value in fields:
        if field.type == FD.TYPE_MESSAGE:
            ftype = pb2json
        elif field.type == FD.TYPE_ENUM:
            ftype = str
            value = field.enum_type.values_by_number[value].name
        elif field.type in _ftype2js:
            ftype = _ftype2js[field.type]
        else:
            raise ParseError(
                "Field %s.%s of type '%d' is not supported" % (pb.__class__.__name__, field.name, field.type,))
        if field.label == FD.LABEL_REPEATED:
            js_value = []
            for v in value:
                js_value.append(ftype(v))
        else:
            js_value = ftype(value)
        js[field.name] = js_value
    return js 
開發者ID:RobinDavid,項目名稱:idasec,代碼行數:28,代碼來源:protobuf_json.py

示例5: _get_field_value_adaptor

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def _get_field_value_adaptor(pb, field, type_callable_map=TYPE_CALLABLE_MAP, use_enum_labels=False):
    if field.type == FieldDescriptor.TYPE_MESSAGE:
        # recursively encode protobuf sub-message
        return lambda pb: protobuf_to_dict(pb, type_callable_map, use_enum_labels)

    if use_enum_labels and field.type == FieldDescriptor.TYPE_ENUM:
        return lambda value: enum_label_name(field, value)

    if field.type in type_callable_map:
        return type_callable_map[field.type]

    raise TypeError("Field %s.%s has unrecognised type id %d" % (
        pb.__class__.__name__, field.name, field.type)) 
開發者ID:advboxes,項目名稱:perceptron-benchmark,代碼行數:15,代碼來源:protobuf_to_dict.py

示例6: _dict_to_protobuf

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def _dict_to_protobuf(pb, value, type_callable_map, strict):
    """ dict to protobuf

    Args:
        pb: data in dict format

    Returns:
        pb: data in proto format
    """

    fields = _get_field_mapping(pb, value, strict)

    for field, input_value, pb_value in fields:
        if field.label == FieldDescriptor.LABEL_REPEATED:
            for item in input_value:
                if field.type == FieldDescriptor.TYPE_MESSAGE:
                    m = pb_value.add()
                    _dict_to_protobuf(m, item, type_callable_map, strict)
                elif field.type == FieldDescriptor.TYPE_ENUM and isinstance(item, basestring):
                    pb_value.append(_string_to_enum(field, item))
                else:
                    pb_value.append(item)
            continue
        if field.type == FieldDescriptor.TYPE_MESSAGE:
            _dict_to_protobuf(pb_value, input_value, type_callable_map, strict)
            continue

        if field.type in type_callable_map:
            input_value = type_callable_map[field.type](input_value)

        if field.is_extension:
            pb.Extensions[field] = input_value
            continue

        if field.type == FieldDescriptor.TYPE_ENUM and isinstance(input_value, basestring):
            input_value = _string_to_enum(field, input_value)

        setattr(pb, field.name, input_value)

    return pb 
開發者ID:advboxes,項目名稱:perceptron-benchmark,代碼行數:42,代碼來源:protobuf_to_dict.py

示例7: dict_to_protobuf

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def dict_to_protobuf(dct, pb=None, containers=CONTAINER_MAP,
                     converters=REVERSE_TYPE_CALLABLE_MAP, strict=True):
    default = container_to_message(dct, containers)
    if pb:
        if default:
            pb.MergeFrom(default)
    else:
        pb = default
    pb = pb if isinstance(pb, Message) else pb()

    for k, v in dct.items():
        try:
            # TODO silently skip undifened fields
            field = pb.DESCRIPTOR.fields_by_name[k]
        except:
            if not strict:
                continue
            else:
                raise
        pb_value = getattr(pb, k, None)
        if field.label == FieldDescriptor.LABEL_REPEATED:
            for item in v:
                if field.type == FieldDescriptor.TYPE_MESSAGE:
                    dict_to_protobuf(item, pb_value.add(),
                                     containers, converters)
                elif field.type == FieldDescriptor.TYPE_ENUM:
                    pb_value.append(label_to_enum(field, item))
                else:
                    pb_value.append(item)
        elif field.type == FieldDescriptor.TYPE_MESSAGE:
            dict_to_protobuf(v, pb_value, containers, converters)
        else:
            if field.type in converters:
                v = converters[field.type](v)
            elif field.type == FieldDescriptor.TYPE_ENUM:
                v = label_to_enum(field, v)

            setattr(pb, field.name, v)

    return pb 
開發者ID:daskos,項目名稱:mentor,代碼行數:42,代碼來源:protobuf.py

示例8: _get_field_value_adaptor

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def _get_field_value_adaptor(pb, field, type_callable_map=TYPE_CALLABLE_MAP, use_enum_labels=False):
    if field.type == FieldDescriptor.TYPE_MESSAGE:
        # recursively encode protobuf sub-message
        return lambda pb: protobuf_to_dict(
            pb, type_callable_map=type_callable_map,
            use_enum_labels=use_enum_labels)

    if use_enum_labels and field.type == FieldDescriptor.TYPE_ENUM:
        return lambda value: enum_label_name(field, value)

    if field.type in type_callable_map:
        return type_callable_map[field.type]

    raise TypeError("Field %s.%s has unrecognised type id %d" % (
        pb.__class__.__name__, field.name, field.type)) 
開發者ID:favll,項目名稱:pogom,代碼行數:17,代碼來源:protobuf_to_dict.py

示例9: _dict_to_protobuf

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_ENUM [as 別名]
def _dict_to_protobuf(pb, value, type_callable_map, strict):
    fields = _get_field_mapping(pb, value, strict)

    for field, input_value, pb_value in fields:
        if field.label == FieldDescriptor.LABEL_REPEATED:
            if field.message_type and field.message_type.has_options and field.message_type.GetOptions().map_entry:
                pb_value.update(input_value)
                continue
            for item in input_value:
                if field.type == FieldDescriptor.TYPE_MESSAGE:
                    m = pb_value.add()
                    _dict_to_protobuf(m, item, type_callable_map, strict)
                elif field.type == FieldDescriptor.TYPE_ENUM and isinstance(item, six.string_types):
                    pb_value.append(_string_to_enum(field, item))
                else:
                    pb_value.append(item)
            continue
        if field.type == FieldDescriptor.TYPE_MESSAGE:
            _dict_to_protobuf(pb_value, input_value, type_callable_map, strict)
            continue

        if field.type in type_callable_map:
            input_value = type_callable_map[field.type](input_value)

        if field.is_extension:
            pb.Extensions[field] = input_value
            continue

        if field.type == FieldDescriptor.TYPE_ENUM and isinstance(input_value, six.string_types):
            input_value = _string_to_enum(field, input_value)

        setattr(pb, field.name, input_value)

    return pb 
開發者ID:favll,項目名稱:pogom,代碼行數:36,代碼來源:protobuf_to_dict.py


注:本文中的google.protobuf.descriptor.FieldDescriptor.TYPE_ENUM屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。