本文整理汇总了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)
示例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")