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


Python FieldDescriptor.TYPE_BYTES屬性代碼示例

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


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

示例1: produce

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_BYTES [as 別名]
def produce(obj, pb, sep):
    for ds, val in pb.ListFields():
        for val in (val if ds.label == ds.LABEL_REPEATED else [val]):
            
            if ds.cpp_type == ds.CPPTYPE_MESSAGE:
                origlen = len(obj)
                produce(obj, val, sep)
                obj.insert(origlen, '%dm%d' % (ds.number, len(obj) - origlen))
                continue
            
            elif ds.type == ds.TYPE_STRING:
                if sep == '!':
                    val = val.replace('*', '*2A').replace('!', '*21')
                else:
                    val = quote(val, safe='~()*!.\'')
            
            elif ds.type == ds.TYPE_BYTES:
                val = urlsafe_b64encode(val).decode('ascii').strip('=')
            
            elif ds.type == ds.TYPE_BOOL:
                val = int(val)
            
            obj.append('%d%s%s' % (ds.number, types_enc[ds.type], val))
    
    return obj 
開發者ID:marin-m,項目名稱:pbtk,代碼行數:27,代碼來源:pburl_decoder.py

示例2: _decode_repeated_field

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_BYTES [as 別名]
def _decode_repeated_field(message, field, value_list):
    """Decode repeated field."""
    if field.type == FieldDescriptor.TYPE_MESSAGE:
        for value in value_list:
            decode(getattr(message, field.name).add(), value)
    else:
        try:
            for value in value_list:
                if field.type == FieldDescriptor.TYPE_BYTES:
                    value = base64.b64decode(value)
                getattr(message, field.name).append(value)
        except (ValueError, TypeError) as e:
            # ValueError: invalid enum value, negative unsigned int value, or
            # invalid base64
            # TypeError: mismatched type
            logger.warning('Message %r ignoring repeated field %s: %s',
                           message.__class__.__name__, field.name, e)
            # Ignore any values already decoded by clearing list
            message.ClearField(field.name) 
開發者ID:tdryer,項目名稱:hangups,代碼行數:21,代碼來源:pblite.py

示例3: _decode_field

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_BYTES [as 別名]
def _decode_field(message, field, value):
    """Decode optional or required field."""
    if field.type == FieldDescriptor.TYPE_MESSAGE:
        decode(getattr(message, field.name), value)
    else:
        try:
            if field.type == FieldDescriptor.TYPE_BYTES:
                value = base64.b64decode(value)
            setattr(message, field.name, value)
        except (ValueError, TypeError) as e:
            # ValueError: invalid enum value, negative unsigned int value, or
            # invalid base64
            # TypeError: mismatched type
            logger.warning('Message %r ignoring field %s: %s',
                           message.__class__.__name__, field.name, e) 
開發者ID:tdryer,項目名稱:hangups,代碼行數:17,代碼來源:pblite.py

示例4: consume

# 需要導入模塊: from google.protobuf.descriptor import FieldDescriptor [as 別名]
# 或者: from google.protobuf.descriptor.FieldDescriptor import TYPE_BYTES [as 別名]
def consume(obj, pb, sep):
    while obj:
        field = obj.pop(0)
        index, type_, val = match('(\d+)(\w)(.*)', field).groups()
        type_ = types_dec[type_]
        
        if int(index) not in pb.DESCRIPTOR.fields_by_number:
            warn('Unknown index: !' + field)
            if type_ == fd.TYPE_MESSAGE:
                del obj[:int(val)]
            continue
        
        field = pb.DESCRIPTOR.fields_by_number[int(index)]
        repeated = field.label == field.LABEL_REPEATED
        field = field.name
        
        if type_ == fd.TYPE_MESSAGE:
            if not repeated:
                getattr(pb, field).SetInParent()
                consume(obj[:int(val)], getattr(pb, field), sep)
            else:
                consume(obj[:int(val)], getattr(pb, field).add(), sep)
            
            del obj[:int(val)]
            continue
        
        elif type_ == fd.TYPE_STRING:
            if sep == '!':
                val = val.replace('*21', '!').replace('*2A', '*')
            else:
                val = unquote(val)
        
        elif type_ == fd.TYPE_BYTES:
            val = urlsafe_b64decode(val + '=' * (-len(val) % 4))
        elif type_ == "base64_string":
            val = urlsafe_b64decode(val + '=' * (-len(val) % 4)).decode('utf8')
        
        elif type_ == fd.TYPE_BOOL:
            val = bool(int(val))
        
        elif type_ in (fd.TYPE_DOUBLE, fd.TYPE_FLOAT):
            val = float(val)
        else:
            val = int(val)
        
        if not repeated:
            setattr(pb, field, val)
        else:
            getattr(pb, field).append(val) 
開發者ID:marin-m,項目名稱:pbtk,代碼行數:51,代碼來源:pburl_decoder.py


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