本文整理汇总了Python中google.protobuf.internal.containers.RepeatedScalarFieldContainer方法的典型用法代码示例。如果您正苦于以下问题:Python containers.RepeatedScalarFieldContainer方法的具体用法?Python containers.RepeatedScalarFieldContainer怎么用?Python containers.RepeatedScalarFieldContainer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.internal.containers
的用法示例。
在下文中一共展示了containers.RepeatedScalarFieldContainer方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def set(prop, val):
if val is None:
return
if type(prop) is RepeatedScalarFieldContainer:
if type(val) is list:
for i in val:
prop.append(i)
else:
prop.append(val)
elif type(prop) is RepeatedCompositeFieldContainer:
x = prop.add()
# x.phase= val
x = val
else:
prop = val
return prop # use for immutable fields
示例2: create_clusters_meta
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def create_clusters_meta(cluster_ids: RepeatedScalarFieldContainer) -> Tuple[List[Cluster], Dict[str, int]]:
try:
unannotated_ids = set((int(c) for c in cluster_ids))
clusters_list = [str(c) for c in sorted(unannotated_ids)]
annotated = False
except ValueError:
clusters_list = sorted(set((c for c in cluster_ids)))
annotated = True
clusters: List[Cluster] = []
cluster_mapping = {}
for cluster_id, cluster_name in enumerate(clusters_list):
clusters.append(
{"id": cluster_id, "description": cluster_name if annotated else f"Unannotated Cluster {cluster_name}"}
)
cluster_mapping[str(cluster_name)] = cluster_id
return clusters, cluster_mapping
示例3: generate_solver_proto
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def generate_solver_proto(**kwargs):
sp = caffe_pb2.SolverParameter()
for k,v in kwargs.iteritems():
if not hasattr(sp, k):
raise ValueError('The argument \'%s\' is not part of the Caffe solver parameters!')
elif v is not None:
elem = getattr(sp, k)
if type(elem) == RepeatedScalarFieldContainer:
elem.append(v)
elif k == 'solver_mode':
setattr(sp, k, sp.SolverMode.DESCRIPTOR.values_by_name[v].number)
else:
setattr(sp, k, v)
return sp
示例4: _assign_to_field
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def _assign_to_field(obj, name, val):
'Helper to assign an arbitrary value to a protobuf field'
target = getattr(obj, name)
if isinstance(target, containers.RepeatedScalarFieldContainer):
target.append(val)
elif isinstance(target, containers.RepeatedCompositeFieldContainer):
target = target.add()
target.CopyFrom(val)
elif isinstance(target, (int, float, bool, str, bytes)):
setattr(obj, name, val)
elif isinstance(target, message.Message):
target.CopyFrom(val)
else:
raise RuntimeError("Unsupported type: {}".format(type(target)))
示例5: parse_kernel
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def parse_kernel(lparam):
# pasre the kernel size from the caffe config parameters
# easiest to do this case by case to cover the different caffe protobuf defs
key = 'kernel_size'
if hasattr(lparam, key):
ks = getattr(lparam, key)
if type(ks) is int and ks > 0:
return [ks]*2
if type(ks) is list or type(ks) is RepeatedScalarFieldContainer:
ks = list(ks)
if len(ks) > 3:
raise NotImplementedError()
if len(ks) > 0 and all([x > 0 for x in ks]):
return [ks[0], ks[-1]]
# ks is [] or has an element that is <= 0
# try to parse from kernel_h and _w
key = 'kernel'
if not (hasattr(lparam, key+'_h') and hasattr(lparam, key+'_w')):
raise ValueError('Can not parse kernel size')
k_h = getattr(lparam, key+'_h')
k_w = getattr(lparam, key+'_w')
if k_h == 0 and k_w == 0:
import ipdb; ipdb.set_trace()
raise ValueError('Can not parse kernel size')
return [k_h, k_w]
示例6: parse_pad
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def parse_pad(lparam):
# pasre the padding size from the caffe config parameters
# easiest to do this case by case to cover the different caffe protobuf defs
key = 'pad'
if hasattr(lparam, key):
ps = getattr(lparam, key)
if type(ps) is int:
return [ps]*2
if type(ps) is list or type(ps) is RepeatedScalarFieldContainer:
ps = list(ps)
if len(ps) > 3:
raise NotImplementedError()
if len(ps) > 0:
return [ps[0], ps[-1]]
# default to [0, 0]
ps = [0, 0]
try:
k_h = getattr(lparam, key+'_h')
ps[0] = k_h
k_w = getattr(lparam, key+'_w')
ps[1] = k_w
except:
pass
return ps
示例7: parse_stride
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def parse_stride(lparam):
# pasre the strides from the caffe config parameters
# easiest to do this case by case to cover the different caffe protobuf defs
key = 'stride'
if hasattr(lparam, key):
ss = getattr(lparam, key)
if type(ss) is int:
return [ss]*2
if type(ss) is list or type(ss) is RepeatedScalarFieldContainer:
ss = list(ss)
if len(ss) > 3:
raise NotImplementedError()
if len(ss) > 0 and all([x > 0 for x in ss]):
return [ss[0], ss[-1]]
# default to [1, 1]
ss = [1, 1]
try:
k_h = getattr(lparam, key+'_h')
if k_h > 0:
ss[0] = k_h
k_w = getattr(lparam, key+'_w')
if k_w > 0:
ss[1] = k_w
except:
pass
return ss
示例8: _DefaultValueConstructorForField
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def _DefaultValueConstructorForField(field):
"""Returns a function which returns a default value for a field.
Args:
field: FieldDescriptor object for this field.
The returned function has one argument:
message: Message instance containing this field, or a weakref proxy
of same.
That function in turn returns a default value for this field. The default
value may refer back to |message| via a weak reference.
"""
if _IsMapField(field):
return _GetInitializeDefaultForMap(field)
if field.label == _FieldDescriptor.LABEL_REPEATED:
if field.has_default_value and field.default_value != []:
raise ValueError('Repeated field default value not empty list: %s' % (
field.default_value))
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
# We can't look at _concrete_class yet since it might not have
# been set. (Depends on order in which we initialize the classes).
message_type = field.message_type
def MakeRepeatedMessageDefault(message):
return containers.RepeatedCompositeFieldContainer(
message._listener_for_children, field.message_type)
return MakeRepeatedMessageDefault
else:
type_checker = type_checkers.GetTypeChecker(field)
def MakeRepeatedScalarDefault(message):
return containers.RepeatedScalarFieldContainer(
message._listener_for_children, type_checker)
return MakeRepeatedScalarDefault
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
# _concrete_class may not yet be initialized.
message_type = field.message_type
def MakeSubMessageDefault(message):
result = message_type._concrete_class()
result._SetListener(
_OneofListener(message, field)
if field.containing_oneof is not None
else message._listener_for_children)
return result
return MakeSubMessageDefault
def MakeScalarDefault(message):
# TODO(protobuf-team): This may be broken since there may not be
# default_value. Combine with has_default_value somehow.
return field.default_value
return MakeScalarDefault
示例9: _DefaultValueConstructorForField
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def _DefaultValueConstructorForField(field):
"""Returns a function which returns a default value for a field.
Args:
field: FieldDescriptor object for this field.
The returned function has one argument:
message: Message instance containing this field, or a weakref proxy
of same.
That function in turn returns a default value for this field. The default
value may refer back to |message| via a weak reference.
"""
if field.label == _FieldDescriptor.LABEL_REPEATED:
if field.has_default_value and field.default_value != []:
raise ValueError('Repeated field default value not empty list: %s' % (
field.default_value))
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
# We can't look at _concrete_class yet since it might not have
# been set. (Depends on order in which we initialize the classes).
message_type = field.message_type
def MakeRepeatedMessageDefault(message):
return containers.RepeatedCompositeFieldContainer(
message._listener_for_children, field.message_type)
return MakeRepeatedMessageDefault
else:
type_checker = type_checkers.GetTypeChecker(field)
def MakeRepeatedScalarDefault(message):
return containers.RepeatedScalarFieldContainer(
message._listener_for_children, type_checker)
return MakeRepeatedScalarDefault
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
# _concrete_class may not yet be initialized.
message_type = field.message_type
def MakeSubMessageDefault(message):
result = message_type._concrete_class()
result._SetListener(message._listener_for_children)
return result
return MakeSubMessageDefault
def MakeScalarDefault(message):
# TODO(protobuf-team): This may be broken since there may not be
# default_value. Combine with has_default_value somehow.
return field.default_value
return MakeScalarDefault
示例10: _DefaultValueConstructorForField
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def _DefaultValueConstructorForField(field):
"""Returns a function which returns a default value for a field.
Args:
field: FieldDescriptor object for this field.
The returned function has one argument:
message: Message instance containing this field, or a weakref proxy
of same.
That function in turn returns a default value for this field. The default
value may refer back to |message| via a weak reference.
"""
if _IsMapField(field):
return _GetInitializeDefaultForMap(field)
if field.label == _FieldDescriptor.LABEL_REPEATED:
if field.has_default_value and field.default_value != []:
raise ValueError('Repeated field default value not empty list: %s' % (
field.default_value))
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
# We can't look at _concrete_class yet since it might not have
# been set. (Depends on order in which we initialize the classes).
message_type = field.message_type
def MakeRepeatedMessageDefault(message):
return containers.RepeatedCompositeFieldContainer(
message._listener_for_children, field.message_type)
return MakeRepeatedMessageDefault
else:
type_checker = type_checkers.GetTypeChecker(field)
def MakeRepeatedScalarDefault(message):
return containers.RepeatedScalarFieldContainer(
message._listener_for_children, type_checker)
return MakeRepeatedScalarDefault
if field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:
# _concrete_class may not yet be initialized.
message_type = field.message_type
def MakeSubMessageDefault(message):
assert getattr(message_type, '_concrete_class', None), (
'Uninitialized concrete class found for field %r (message type %r)'
% (field.full_name, message_type.full_name))
result = message_type._concrete_class()
result._SetListener(
_OneofListener(message, field)
if field.containing_oneof is not None
else message._listener_for_children)
return result
return MakeSubMessageDefault
def MakeScalarDefault(message):
# TODO(protobuf-team): This may be broken since there may not be
# default_value. Combine with has_default_value somehow.
return field.default_value
return MakeScalarDefault
示例11: _AddPropertiesForRepeatedField
# 需要导入模块: from google.protobuf.internal import containers [as 别名]
# 或者: from google.protobuf.internal.containers import RepeatedScalarFieldContainer [as 别名]
def _AddPropertiesForRepeatedField(field, cls):
"""Adds a public property for a "repeated" protocol message field. Clients
can use this property to get the value of the field, which will be either a
RepeatedScalarFieldContainer or RepeatedCompositeFieldContainer (see
below).
Note that when clients add values to these containers, we perform
type-checking in the case of repeated scalar fields, and we also set any
necessary "has" bits as a side-effect.
Args:
field: A FieldDescriptor for this field.
cls: The class we're constructing.
"""
proto_field_name = field.name
property_name = _PropertyName(proto_field_name)
def getter(self):
field_value = self._fields.get(field)
if field_value is None:
# Construct a new object to represent this field.
field_value = field._default_constructor(self)
# Atomically check if another thread has preempted us and, if not, swap
# in the new object we just created. If someone has preempted us, we
# take that object and discard ours.
# WARNING: We are relying on setdefault() being atomic. This is true
# in CPython but we haven't investigated others. This warning appears
# in several other locations in this file.
field_value = self._fields.setdefault(field, field_value)
return field_value
getter.__module__ = None
getter.__doc__ = 'Getter for %s.' % proto_field_name
# We define a setter just so we can throw an exception with a more
# helpful error message.
def setter(self, new_value):
raise AttributeError('Assignment not allowed to repeated field '
'"%s" in protocol message object.' % proto_field_name)
doc = 'Magic attribute generated for "%s" proto field.' % proto_field_name
setattr(cls, property_name, _FieldProperty(field, getter, setter, doc=doc))