本文整理匯總了Python中google.protobuf.descriptor.FieldDescriptor.LABEL_REPEATED屬性的典型用法代碼示例。如果您正苦於以下問題:Python FieldDescriptor.LABEL_REPEATED屬性的具體用法?Python FieldDescriptor.LABEL_REPEATED怎麽用?Python FieldDescriptor.LABEL_REPEATED使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類google.protobuf.descriptor.FieldDescriptor
的用法示例。
在下文中一共展示了FieldDescriptor.LABEL_REPEATED屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _handle_field_values
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def _handle_field_values(pb, field_values,
use_enum_labels, including_default_value_fields):
result_dict = {}
extensions = {}
for field, value in field_values:
if field.message_type and field.message_type.has_options and \
field.message_type.GetOptions().map_entry:
result_dict[field.name] = dict()
value_field = field.message_type.fields_by_name['value']
type_callable = _get_field_value_adaptor(
pb, value_field,
use_enum_labels, including_default_value_fields)
for k, v in value.items():
result_dict[field.name][k] = type_callable(v)
continue
type_callable = _get_field_value_adaptor(
pb, field, use_enum_labels, including_default_value_fields)
if field.label == FieldDescriptor.LABEL_REPEATED:
type_callable = repeated(type_callable)
if field.is_extension:
extensions[str(field.number)] = type_callable(value)
continue
result_dict[field.name] = type_callable(value)
return result_dict, extensions
示例2: _handle_default_value_fields
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def _handle_default_value_fields(pb, keys, result_dict):
for field in pb.DESCRIPTOR.fields:
if keys and field.name not in keys:
continue
# Singular message fields and oneof fields will not be affected.
if ((field.label != FieldDescriptor.LABEL_REPEATED and
field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE) or
field.containing_oneof):
continue
if field.name in result_dict:
# Skip the field which has been serailized already.
continue
if _is_map_entry(field):
result_dict[field.name] = {}
elif _is_repeat_label(field):
result_dict[field.name] = []
else:
result_dict[field.name] = field.default_value
return result_dict
示例3: json2pb
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def json2pb(pb, js):
''' convert JSON string to google.protobuf.descriptor instance '''
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)
else:
setattr(pb, field.name, ftype(value))
return pb
示例4: pb2json
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def pb2json(pb):
''' convert google.protobuf.descriptor instance to JSON string '''
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 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
示例5: __setitem__
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def __setitem__(self, extension_handle, value):
"""If extension_handle specifies a non-repeated, scalar extension
field, sets the value of that field.
"""
_VerifyExtensionHandle(self._extended_message, extension_handle)
if (extension_handle.label == FieldDescriptor.LABEL_REPEATED or
extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE):
raise TypeError(
'Cannot assign to extension "%s" because it is a repeated or '
'composite type.' % extension_handle.full_name)
# It's slightly wasteful to lookup the type checker each time,
# but we expect this to be a vanishingly uncommon case anyway.
type_checker = type_checkers.GetTypeChecker(extension_handle)
# pylint: disable=protected-access
self._extended_message._fields[extension_handle] = (
type_checker.CheckValue(value))
self._extended_message._Modified()
示例6: pb2dict
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def pb2dict(obj):
"""
Takes a ProtoBuf Message obj and convertes it to a dict.
"""
adict = {}
if not obj.IsInitialized():
return None
for field in obj.DESCRIPTOR.fields:
if not getattr(obj, field.name):
continue
if not field.label == FD.LABEL_REPEATED:
if not field.type == FD.TYPE_MESSAGE:
adict[field.name] = getattr(obj, field.name)
else:
value = pb2dict(getattr(obj, field.name))
if value:
adict[field.name] = value
else:
if field.type == FD.TYPE_MESSAGE:
adict[field.name] = \
[pb2dict(v) for v in getattr(obj, field.name)]
else:
adict[field.name] = [v for v in getattr(obj, field.name)]
return adict
示例7: pb2json
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def pb2json(pb, useFieldNumber=False):
''' convert google.protobuf.descriptor instance to JSON string '''
js = {}
# fields = pb.DESCRIPTOR.fields #all fields
fields = pb.ListFields() #only filled (including extensions)
for field,value in fields:
if useFieldNumber:
key = field.number
else:
key = field.name
if field.type == FD.TYPE_MESSAGE:
ftype = partial(pb2json, useFieldNumber=useFieldNumber)
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[key] = js_value
return js
示例8: protobuf_to_dict
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [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
示例9: protobuf_to_dict
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def protobuf_to_dict(pb, type_callable_map=TYPE_CALLABLE_MAP, use_enum_labels=False):
result_dict = {}
extensions = {}
for field, value in pb.ListFields():
if field.message_type and field.message_type.has_options and field.message_type.GetOptions().map_entry:
result_dict[field.name] = dict(value)
continue
type_callable = _get_field_value_adaptor(pb, field, type_callable_map, use_enum_labels)
if field.label == FieldDescriptor.LABEL_REPEATED:
type_callable = repeated(type_callable)
if field.is_extension:
extensions[str(field.number)] = type_callable(value)
continue
result_dict[field.name] = type_callable(value)
if extensions:
result_dict[EXTENSION_CONTAINER] = extensions
return result_dict
示例10: traverse_field_options
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def traverse_field_options(fields, prefix):
field_options = []
for field in fields:
assert isinstance(field, FieldDescriptorProto)
full_name = prefix + '-' + field.name
option = None
if field.type == FieldDescriptor.TYPE_MESSAGE and field.label != \
FieldDescriptor.LABEL_REPEATED:
if field.options:
for fd, val in field.options.ListFields():
if fd.full_name == 'voltha.yang_inline_node':
field_options.append(
{'name': full_name,
'option': fd.full_name,
'proto_name': val.id,
'proto_type': val.type
}
)
return field_options
示例11: json2pb
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [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
示例12: pb2json
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [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
示例13: _IsValidPath
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def _IsValidPath(message_descriptor, path):
"""Checks whether the path is valid for Message Descriptor."""
parts = path.split('.')
last = parts.pop()
for name in parts:
field = message_descriptor.fields_by_name.get(name)
if (field is None or
field.label == FieldDescriptor.LABEL_REPEATED or
field.type != FieldDescriptor.TYPE_MESSAGE):
return False
message_descriptor = field.message_type
return last in message_descriptor.fields_by_name
示例14: _MergeMessage
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def _MergeMessage(
node, source, destination, replace_message, replace_repeated):
"""Merge all fields specified by a sub-tree from source to destination."""
source_descriptor = source.DESCRIPTOR
for name in node:
child = node[name]
field = source_descriptor.fields_by_name[name]
if field is None:
raise ValueError('Error: Can\'t find field {0} in message {1}.'.format(
name, source_descriptor.full_name))
if child:
# Sub-paths are only allowed for singular message fields.
if (field.label == FieldDescriptor.LABEL_REPEATED or
field.cpp_type != FieldDescriptor.CPPTYPE_MESSAGE):
raise ValueError('Error: Field {0} in message {1} is not a singular '
'message field and cannot have sub-fields.'.format(
name, source_descriptor.full_name))
if source.HasField(name):
_MergeMessage(
child, getattr(source, name), getattr(destination, name),
replace_message, replace_repeated)
continue
if field.label == FieldDescriptor.LABEL_REPEATED:
if replace_repeated:
destination.ClearField(_StrConvert(name))
repeated_source = getattr(source, name)
repeated_destination = getattr(destination, name)
if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE:
for item in repeated_source:
repeated_destination.add().MergeFrom(item)
else:
repeated_destination.extend(repeated_source)
else:
if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE:
if replace_message:
destination.ClearField(_StrConvert(name))
if source.HasField(name):
getattr(destination, name).MergeFrom(getattr(source, name))
else:
setattr(destination, name, getattr(source, name))
示例15: _IsValidPath
# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import LABEL_REPEATED [as 別名]
def _IsValidPath(message_descriptor, path):
"""Checks whether the path is valid for Message Descriptor."""
parts = path.split('.')
last = parts.pop()
for name in parts:
field = message_descriptor.fields_by_name[name]
if (field is None or
field.label == FieldDescriptor.LABEL_REPEATED or
field.type != FieldDescriptor.TYPE_MESSAGE):
return False
message_descriptor = field.message_type
return last in message_descriptor.fields_by_name