本文整理汇总了Python中msgpack.ExtType方法的典型用法代码示例。如果您正苦于以下问题:Python msgpack.ExtType方法的具体用法?Python msgpack.ExtType怎么用?Python msgpack.ExtType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msgpack
的用法示例。
在下文中一共展示了msgpack.ExtType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def encode(obj):
if type(obj) in (list, tuple) or isinstance(obj, PVector):
return [encode(item) for item in obj]
if isinstance(obj, Mapping):
encoded_obj = {}
for key in obj.keys():
encoded_obj[encode(key)] = encode(obj[key])
return encoded_obj
if isinstance(obj, _native_builtin_types):
return obj
if isinstance(obj, Set):
return ExtType(TYPE_PSET, packb([encode(item) for item in obj], use_bin_type=True))
if isinstance(obj, PList):
return ExtType(TYPE_PLIST, packb([encode(item) for item in obj], use_bin_type=True))
if isinstance(obj, PBag):
return ExtType(TYPE_PBAG, packb([encode(item) for item in obj], use_bin_type=True))
if isinstance(obj, types.FunctionType):
return ExtType(TYPE_FUNC, encode_func(obj))
if isinstance(obj, Receiver):
return ExtType(TYPE_MBOX, packb(obj.encode(), use_bin_type=True))
# assume record
cls = obj.__class__
return ExtType(0, packb([cls.__module__, cls.__name__] + [encode(item) for item in obj],
use_bin_type=True))
示例2: default
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def default(self, obj):
replacer = self.__type_replacements.get(type(obj), None)
if replacer:
obj = replacer(obj)
if isinstance(obj, set):
return tuple(obj) # msgpack module can't deal with sets so we make a tuple out of it
if isinstance(obj, uuid.UUID):
return str(obj)
if isinstance(obj, complex):
return msgpack.ExtType(0x30, struct.pack("dd", obj.real, obj.imag))
if isinstance(obj, datetime.datetime):
if obj.tzinfo:
raise errors.SerializeError("msgpack cannot serialize datetime with timezone info")
return msgpack.ExtType(0x32, struct.pack("d", obj.timestamp()))
if isinstance(obj, datetime.date):
return msgpack.ExtType(0x33, struct.pack("l", obj.toordinal()))
if isinstance(obj, decimal.Decimal):
return str(obj)
if isinstance(obj, numbers.Number):
return msgpack.ExtType(0x31, str(obj).encode("ascii")) # long
if isinstance(obj, array.array):
if obj.typecode == 'c':
return obj.tostring()
if obj.typecode == 'u':
return obj.tounicode()
return obj.tolist()
return self.class_to_dict(obj)
示例3: _msgpack_default
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _msgpack_default(cls, obj):
obj_type, obj_value = _serialize_ext_dump(obj)
obj_type = next(i[0] for i in cls._ext_types.items() if i[1] == obj_type)
if its.py_v3 and isinstance(obj_value, str):
obj_value = obj_value.encode(cls.encoding)
return msgpack.ExtType(obj_type, obj_value)
示例4: _msgpack_ext_hook
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _msgpack_ext_hook(cls, code, obj_value):
default = msgpack.ExtType(code, obj_value)
if its.py_v3 and isinstance(obj_value, bytes):
obj_value = obj_value.decode(cls.encoding)
obj_type = cls._ext_types.get(code)
return _serialize_ext_load(obj_type, obj_value, default)
示例5: _default_encoder
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _default_encoder(self, obj):
obj_type = obj.__class__
try:
typename, marshaller = self._marshallers[obj_type]
except KeyError:
raise LookupError('no marshaller found for type "{}"'
.format(obj_type.__class__.__name__)) from None
state = marshaller(obj)
data = typename + b':' + self.serialize(state)
return ExtType(self.custom_type_code, data)
示例6: _custom_object_hook
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _custom_object_hook(self, code: int, data: bytes):
if code == self.custom_type_code:
typename, payload = data.split(b':', 1)
state = self.deserialize(payload)
try:
cls, unmarshaller = self._unmarshallers[typename]
except KeyError:
raise LookupError('no unmarshaller found for type "{}"'
.format(typename.decode('utf-8'))) from None
instance = cls.__new__(cls)
unmarshaller(instance, state)
return instance
else:
return ExtType(code, data)
示例7: _msgpack_default
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _msgpack_default(obj):
obj_type, obj_value = _serialize_ext_dump(obj)
obj_type = next(i[0] for i in _MSGPACK_EXT_TYPES.items() if i[1] == obj_type)
if sys.version_info[0] == 3:
obj_value = obj_value.encode('utf-8')
return msgpack.ExtType(obj_type, obj_value)
示例8: _msgpack_ext_hook
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _msgpack_ext_hook(code, obj_value):
default = msgpack.ExtType(code, obj_value)
if sys.version_info[0] == 3:
obj_value = obj_value.decode('utf-8')
obj_type = _MSGPACK_EXT_TYPES.get(code)
return _serialize_ext_load(obj_type, obj_value, default)
示例9: __init__
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def __init__(self, file_like=None, read_size=0, use_list=True,
object_hook=None, object_pairs_hook=None, list_hook=None,
encoding=None, unicode_errors='strict', max_buffer_size=0,
ext_hook=ExtType):
if file_like is None:
self._fb_feeding = True
else:
if not callable(file_like.read):
raise TypeError("`file_like.read` must be callable")
self.file_like = file_like
self._fb_feeding = False
self._fb_buffers = []
self._fb_buf_o = 0
self._fb_buf_i = 0
self._fb_buf_n = 0
self._max_buffer_size = max_buffer_size or 2**31-1
if read_size > self._max_buffer_size:
raise ValueError("read_size must be smaller than max_buffer_size")
self._read_size = read_size or min(self._max_buffer_size, 2048)
self._encoding = encoding
self._unicode_errors = unicode_errors
self._use_list = use_list
self._list_hook = list_hook
self._object_hook = object_hook
self._object_pairs_hook = object_pairs_hook
self._ext_hook = ext_hook
if list_hook is not None and not callable(list_hook):
raise TypeError('`list_hook` is not callable')
if object_hook is not None and not callable(object_hook):
raise TypeError('`object_hook` is not callable')
if object_pairs_hook is not None and not callable(object_pairs_hook):
raise TypeError('`object_pairs_hook` is not callable')
if object_hook is not None and object_pairs_hook is not None:
raise TypeError("object_pairs_hook and object_hook are mutually "
"exclusive")
if not callable(ext_hook):
raise TypeError("`ext_hook` is not callable")
示例10: _serializer
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _serializer(registry, obj):
handler = registry.match(obj)
if handler is None:
raise ValueError("No serialization handler registered"
" for type '%s'" % (type(obj).__name__))
return msgpack.ExtType(handler.identity, handler.serialize(obj))
示例11: _unserializer
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def _unserializer(registry, code, data):
handler = registry.get(code)
if not handler:
return msgpack.ExtType(code, data)
else:
return handler.deserialize(data)
示例12: from_client
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def from_client(msg):
def handler(obj):
if type(obj) is msgpack.ExtType:
if obj.code == BUFFER_TYPE_ID:
return vim.buffers[msgpack.unpackb(obj.data)]
if obj.code == WINDOW_TYPE_ID:
return vim.windows[msgpack.unpackb(obj.data) - 1]
if sys.version_info.major != 2:
# python3 needs decode
obj = decode_if_bytes(obj)
return obj
return walk(handler, msg)
示例13: to_client
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def to_client(msg):
def handler(obj):
if type(obj) == BUFFER_TYPE:
return msgpack.ExtType(BUFFER_TYPE_ID, msgpack.packb(obj.number))
if type(obj) == WINDOW_TYPE:
return msgpack.ExtType(WINDOW_TYPE_ID, msgpack.packb(obj.number))
if type(obj) == vim.Function:
try:
return obj.name.encode()
except Exception:
return ""
return obj
return walk(handler, msg)
示例14: default
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def default(obj):
if isinstance(obj, PasteFile):
return msgpack.ExtType(42, obj.to_dict())
raise TypeError('Unknown type: %r' % (obj,))
示例15: ext_hook
# 需要导入模块: import msgpack [as 别名]
# 或者: from msgpack import ExtType [as 别名]
def ext_hook(code, data):
if code == 42:
p = PasteFile.from_dict(data)
return p
return msgpack.ExtType(code, data)