Python google.protobuf.descriptor.FieldDescriptor.TYPE_FLOAT() Examples

The following are code examples for showing how to use google.protobuf.descriptor.FieldDescriptor.TYPE_FLOAT(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pbtk   Author: marin-m   File: pburl_decoder.py    GNU General Public License v3.0 4 votes vote down vote up
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)