当前位置: 首页>>代码示例>>Python>>正文


Python Common.stringToUtf8Array方法代码示例

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


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

示例1: __init__

# 需要导入模块: from pyndn.util.common import Common [as 别名]
# 或者: from pyndn.util.common.Common import stringToUtf8Array [as 别名]
    def __init__(self, array = None, copy = True):
        self._hash = None

        if array == None:
            self._array = None
        elif isinstance(array, Blob):
            # Use the existing _array.  Don't need to check for copy.
            self._array = array._array
        else:
            array = Common.stringToUtf8Array(array)

            if copy:
                # We are copying, so just make another bytearray.
                # We always use a memoryview so that slicing is efficient.
                if type(array) is _memoryviewWrapper:
                    # Use the underlying memoryview directly. (When we only
                    # support Python 3.3 or later, this check is not necessary.)
                    self._array = memoryview(bytearray(array._view))
                else:
                    self._array = memoryview(bytearray(array))
            else:
                if type(array) is bytearray:
                    # We always use a memoryview so that slicing is efficient.
                    self._array = memoryview(array)
                else:
                    # Can't take a memoryview, so use as-is.
                    self._array = array

            if not _memoryviewUsesInt and type(self._array) is memoryview:
                # memoryview elements are not int (Python versions before 3.3)
                #   so we need a wrapper which will return int elements.
                self._array = _memoryviewWrapper(self._array)
开发者ID:MAHIS,项目名称:PyNDN2,代码行数:34,代码来源:blob.py

示例2: _encodeMessageValue

# 需要导入模块: from pyndn.util.common import Common [as 别名]
# 或者: from pyndn.util.common.Common import stringToUtf8Array [as 别名]
    def _encodeMessageValue(message, encoder):
        # Note: We can't use ListFields because it sorts by field number.
        descriptor = message.DESCRIPTOR
        # Reverse so that we encode backwards.
        for field in reversed(descriptor.fields):
            tlvType = field.number

            if field.label == field.LABEL_REPEATED:
                # Reverse so that we encode backwards.
                values = reversed(getattr(message, field.name))
            else:
                if message.HasField(field.name):
                    # Make a singleton list.
                    values = [getattr(message, field.name)]
                else:
                    continue

            for value in values:
                if field.type == field.TYPE_MESSAGE:
                    saveLength = len(encoder)

                    # Encode backwards.
                    ProtobufTlv._encodeMessageValue(value, encoder)
                    encoder.writeTypeAndLength(
                      tlvType, len(encoder) - saveLength)
                elif (field.type == field.TYPE_UINT32 or
                      field.type == field.TYPE_UINT64):
                    encoder.writeNonNegativeIntegerTlv(tlvType, value)
                elif (field.type == field.TYPE_ENUM):
                    if value < 0:
                        raise RuntimeError(
                          "ProtobufTlv::encode: ENUM value may not be negative")
                    encoder.writeNonNegativeIntegerTlv(tlvType, value)
                elif (field.type == field.TYPE_BYTES or
                      field.type == field.TYPE_STRING):
                    encoder.writeBlobTlv(tlvType, Common.stringToUtf8Array(value))
                elif field.type == field.TYPE_BOOL:
                    if value:
                        encoder.writeTypeAndLength(tlvType, 0)
                else:
                    raise RuntimeError("ProtobufTlv::encode: Unknown field type")
开发者ID:MAHIS,项目名称:PyNDN2,代码行数:43,代码来源:protobuf_tlv.py


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