python descript_Python descriptor.FieldDescriptor方法代码示例

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

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

示例1: _CheckConflictRegister

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _CheckConflictRegister(self, desc):

"""Check if the descriptor name conflicts with another of the same name.

Args:

desc: Descriptor of a message, enum, service or extension.

"""

desc_name = desc.full_name

for register, descriptor_type in [

(self._descriptors, descriptor.Descriptor),

(self._enum_descriptors, descriptor.EnumDescriptor),

(self._service_descriptors, descriptor.ServiceDescriptor),

(self._toplevel_extensions, descriptor.FieldDescriptor)]:

if desc_name in register:

file_name = register[desc_name].file.name

if not isinstance(desc, descriptor_type) or (

file_name != desc.file.name):

warn_msg = ('Conflict register for file "' + desc.file.name +

'": ' + desc_name +

' is already defined in file "' +

file_name + '"')

warnings.warn(warn_msg, RuntimeWarning)

return

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:24,

示例2: AddFileDescriptor

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def AddFileDescriptor(self, file_desc):

"""Adds a FileDescriptor to the pool, non-recursively.

If the FileDescriptor contains messages or enums, the caller must explicitly

register them.

Args:

file_desc: A FileDescriptor.

"""

self._AddFileDescriptor(file_desc)

# TODO(jieluo): This is a temporary solution for FieldDescriptor.file.

# FieldDescriptor.file is added in code gen. Remove this solution after

# maybe 2020 for compatibility reason (with 3.4.1 only).

for extension in list(file_desc.extensions_by_name.values()):

self._file_desc_by_toplevel_extension[

extension.full_name] = file_desc

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:19,

示例3: _AddPropertiesForField

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _AddPropertiesForField(field, cls):

"""Adds a public property for a protocol message field.

Clients can use this property to get and (in the case

of non-repeated scalar fields) directly set the value

of a protocol message field.

Args:

field: A FieldDescriptor for this field.

cls: The class we're constructing.

"""

# Catch it if we add other types that we should

# handle specially here.

assert _FieldDescriptor.MAX_CPPTYPE == 10

constant_name = field.name.upper() + "_FIELD_NUMBER"

setattr(cls, constant_name, field.number)

if field.label == _FieldDescriptor.LABEL_REPEATED:

_AddPropertiesForRepeatedField(field, cls)

elif field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:

_AddPropertiesForNonRepeatedCompositeField(field, cls)

else:

_AddPropertiesForNonRepeatedScalarField(field, cls)

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:25,

示例4: FindExtensionByName

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def FindExtensionByName(self, full_name):

"""Loads the named extension descriptor from the pool.

Args:

full_name: The full name of the extension descriptor to load.

Returns:

A FieldDescriptor, describing the named extension.

"""

full_name = _NormalizeFullyQualifiedName(full_name)

message_name, _, extension_name = full_name.rpartition('.')

try:

# Most extensions are nested inside a message.

scope = self.FindMessageTypeByName(message_name)

except KeyError:

# Some extensions are defined at file scope.

scope = self.FindFileContainingSymbol(full_name)

return scope.extensions_by_name[extension_name]

开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:20,

示例5: __getitem__

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def __getitem__(self, extension):

from google.protobuf import descriptor

if not isinstance(extension, descriptor.FieldDescriptor):

raise KeyError('Bad extension %r.' % (extension,))

cdescriptor = extension._cdescriptor

if (cdescriptor.label != _LABEL_REPEATED and

cdescriptor.cpp_type != _CPPTYPE_MESSAGE):

return self._cmsg.GetScalar(cdescriptor)

ext = self._values.get(extension, None)

if ext is not None:

return ext

ext = self._CreateNewHandle(extension)

self._values[extension] = ext

return ext

开发者ID:katharosada,项目名称:botchallenge,代码行数:19,

示例6: _BytesForNonRepeatedElement

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _BytesForNonRepeatedElement(value, field_number, field_type):

"""Returns the number of bytes needed to serialize a non-repeated element.

The returned byte count includes space for tag information and any

other additional space associated with serializing value.

Args:

value: Value we're serializing.

field_number: Field number of this value. (Since the field number

is stored as part of a varint-encoded tag, this has an impact

on the total bytes required to serialize the value).

field_type: The type of the field. One of the TYPE_* constants

within FieldDescriptor.

"""

try:

fn = type_checkers.TYPE_TO_BYTE_SIZE_FN[field_type]

return fn(field_number, value)

except KeyError:

raise message_mod.EncodeError('Unrecognized field type: %d' % field_type)

开发者ID:katharosada,项目名称:botchallenge,代码行数:20,

示例7: GetTypeChecker

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def GetTypeChecker(field):

"""Returns a type checker for a message field of the specified types.

Args:

field: FieldDescriptor object for this field.

Returns:

An instance of TypeChecker which can be used to verify the types

of values assigned to a field of the specified type.

"""

if (field.cpp_type == _FieldDescriptor.CPPTYPE_STRING and

field.type == _FieldDescriptor.TYPE_STRING):

return UnicodeValueChecker()

if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:

return EnumValueChecker(field.enum_type)

return _VALUE_CHECKERS[field.cpp_type]

# None of the typecheckers below make any attempt to guard against people

# subclassing builtin types and doing weird things. We're not trying to

# protect against malicious clients here, just people accidentally shooting

# themselves in the foot in obvious ways.

开发者ID:katharosada,项目名称:botchallenge,代码行数:24,

示例8: AddFileDescriptor

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def AddFileDescriptor(self, file_desc):

"""Adds a FileDescriptor to the pool, non-recursively.

If the FileDescriptor contains messages or enums, the caller must explicitly

register them.

Args:

file_desc: A FileDescriptor.

"""

self._AddFileDescriptor(file_desc)

# TODO(jieluo): This is a temporary solution for FieldDescriptor.file.

# Remove it when FieldDescriptor.file is added in code gen.

for extension in file_desc.extensions_by_name.values():

self._file_desc_by_toplevel_extension[

extension.full_name] = file_desc

开发者ID:apple,项目名称:coremltools,代码行数:18,

示例9: FindExtensionByNumber

​点赞 6

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def FindExtensionByNumber(self, message_descriptor, number):

"""Gets the extension of the specified message with the specified number.

Extensions have to be registered to this pool by calling

AddExtensionDescriptor.

Args:

message_descriptor: descriptor of the extended message.

number: integer, number of the extension field.

Returns:

A FieldDescriptor describing the extension.

Raise:

KeyError: when no extension with the given number is known for the

specified message.

"""

return self._extensions_by_number[message_descriptor][number]

开发者ID:enricofer,项目名称:go2mapillary,代码行数:20,

示例10: _IsMessageSetExtension

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _IsMessageSetExtension(field):

return (field.is_extension and

field.containing_type.has_options and

field.containing_type.GetOptions().message_set_wire_format and

field.type == descriptor.FieldDescriptor.TYPE_MESSAGE and

field.label == descriptor.FieldDescriptor.LABEL_OPTIONAL)

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:8,

示例11: FindExtensionByName

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def FindExtensionByName(self, full_name):

"""Loads the named extension descriptor from the pool.

Args:

full_name: The full name of the extension descriptor to load.

Returns:

A FieldDescriptor, describing the named extension.

Raises:

KeyError: if the extension cannot be found in the pool.

"""

full_name = _NormalizeFullyQualifiedName(full_name)

try:

# The proto compiler does not give any link between the FileDescriptor

# and top-level extensions unless the FileDescriptorProto is added to

# the DescriptorDatabase, but this can impact memory usage.

# So we registered these extensions by name explicitly.

return self._toplevel_extensions[full_name]

except KeyError:

pass

message_name, _, extension_name = full_name.rpartition('.')

try:

# Most extensions are nested inside a message.

scope = self.FindMessageTypeByName(message_name)

except KeyError:

# Some extensions are defined at file scope.

scope = self._FindFileContainingSymbolInDb(full_name)

return scope.extensions_by_name[extension_name]

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:31,

示例12: FindAllExtensions

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def FindAllExtensions(self, message_descriptor):

"""Gets all the known extension of a given message.

Extensions have to be registered to this pool by calling

AddExtensionDescriptor.

Args:

message_descriptor: descriptor of the extended message.

Returns:

A list of FieldDescriptor describing the extensions.

"""

return list(self._extensions_by_number[message_descriptor].values())

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:15,

示例13: testHandWrittenReflection

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def testHandWrittenReflection(self):

# Hand written extensions are only supported by the pure-Python

# implementation of the API.

if api_implementation.Type() != 'python':

return

FieldDescriptor = descriptor.FieldDescriptor

foo_field_descriptor = FieldDescriptor(

name='foo_field', full_name='MyProto.foo_field',

index=0, number=1, type=FieldDescriptor.TYPE_INT64,

cpp_type=FieldDescriptor.CPPTYPE_INT64,

label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,

containing_type=None, message_type=None, enum_type=None,

is_extension=False, extension_scope=None,

options=descriptor_pb2.FieldOptions())

mydescriptor = descriptor.Descriptor(

name='MyProto', full_name='MyProto', filename='ignored',

containing_type=None, nested_types=[], enum_types=[],

fields=[foo_field_descriptor], extensions=[],

options=descriptor_pb2.MessageOptions())

class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):

DESCRIPTOR = mydescriptor

myproto_instance = MyProtoClass()

self.assertEqual(0, myproto_instance.foo_field)

self.assertTrue(not myproto_instance.HasField('foo_field'))

myproto_instance.foo_field = 23

self.assertEqual(23, myproto_instance.foo_field)

self.assertTrue(myproto_instance.HasField('foo_field'))

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:30,

示例14: testPackedOptions

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def testPackedOptions(self):

proto = unittest_pb2.TestAllTypes()

proto.optional_int32 = 1

proto.optional_double = 3.0

for field_descriptor, _ in proto.ListFields():

self.assertEqual(False, field_descriptor.GetOptions().packed)

proto = unittest_pb2.TestPackedTypes()

proto.packed_int32.append(1)

proto.packed_double.append(3.0)

for field_descriptor, _ in proto.ListFields():

self.assertEqual(True, field_descriptor.GetOptions().packed)

self.assertEqual(descriptor.FieldDescriptor.LABEL_REPEATED,

field_descriptor.label)

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:16,

示例15: _IsPresent

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _IsPresent(item):

"""Given a (FieldDescriptor, value) tuple from _fields, return true if the

value should be included in the list returned by ListFields()."""

if item[0].label == _FieldDescriptor.LABEL_REPEATED:

return bool(item[1])

elif item[0].cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE:

return item[1]._is_present_in_parent

else:

return True

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:12,

示例16: GetTypeChecker

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def GetTypeChecker(field):

"""Returns a type checker for a message field of the specified types.

Args:

field: FieldDescriptor object for this field.

Returns:

An instance of TypeChecker which can be used to verify the types

of values assigned to a field of the specified type.

"""

if (field.cpp_type == _FieldDescriptor.CPPTYPE_STRING and

field.type == _FieldDescriptor.TYPE_STRING):

return UnicodeValueChecker()

if field.cpp_type == _FieldDescriptor.CPPTYPE_ENUM:

if SupportsOpenEnums(field):

# When open enums are supported, any int32 can be assigned.

return _VALUE_CHECKERS[_FieldDescriptor.CPPTYPE_INT32]

else:

return EnumValueChecker(field.enum_type)

return _VALUE_CHECKERS[field.cpp_type]

# None of the typecheckers below make any attempt to guard against people

# subclassing builtin types and doing weird things. We're not trying to

# protect against malicious clients here, just people accidentally shooting

# themselves in the foot in obvious ways.

开发者ID:ryfeus,项目名称:lambda-packs,代码行数:28,

示例17: _MakeFieldDescriptor

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _MakeFieldDescriptor(self, field_proto, message_name, index,

is_extension=False):

"""Creates a field descriptor from a FieldDescriptorProto.

For message and enum type fields, this method will do a look up

in the pool for the appropriate descriptor for that type. If it

is unavailable, it will fall back to the _source function to

create it. If this type is still unavailable, construction will

fail.

Args:

field_proto: The proto describing the field.

message_name: The name of the containing message.

index: Index of the field

is_extension: Indication that this field is for an extension.

Returns:

An initialized FieldDescriptor object

"""

if message_name:

full_name = '.'.join((message_name, field_proto.name))

else:

full_name = field_proto.name

return descriptor.FieldDescriptor(

name=field_proto.name,

full_name=full_name,

index=index,

number=field_proto.number,

type=field_proto.type,

cpp_type=None,

message_type=None,

enum_type=None,

containing_type=None,

label=field_proto.label,

has_default_value=False,

default_value=None,

is_extension=is_extension,

extension_scope=None,

options=_OptionsOrNone(field_proto))

开发者ID:abhisuri97,项目名称:auto-alt-text-lambda-api,代码行数:43,

示例18: _MakeFieldDescriptor

​点赞 5

# 需要导入模块: from google.protobuf import descriptor [as 别名]

# 或者: from google.protobuf.descriptor import FieldDescriptor [as 别名]

def _MakeFieldDescriptor(self, field_proto, message_name, index,

is_extension=False):

"""Creates a field descriptor from a FieldDescriptorProto.

For message and enum type fields, this method will do a look up

in the pool for the appropriate descriptor for that type. If it

is unavailable, it will fall back to the _source function to

create it. If this type is still unavailable, construction will

fail.

Args:

field_proto: The proto describing the field.

message_name: The name of the containing message.

index: Index of the field

is_extension: Indication that this field is for an extension.

Returns:

An initialized FieldDescriptor object

"""

if message_name:

full_name = '.'.join((message_name, field_proto.name))

else:

full_name = field_proto.name

return descriptor.FieldDescriptor(

name=field_proto.name,

full_name=full_name,

index=index,

number=field_proto.number,

type=field_proto.type,

cpp_type=None,

message_type=None,

enum_type=None,

containing_type=None,

label=field_proto.label,

has_default_value=False,

default_value=None,

is_extension=is_extension,

extension_scope=None,

options=field_proto.options)

开发者ID:sklearn-theano,项目名称:sklearn-theano,代码行数:43,

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值