python - 是否可以扩展 xml-rpc 可以序列化的内容集? [英] python - is it possible to extend the set of things that xml-rpc can serialize?
问题描述
我见过几个问题,询问如何通过 xml-rpc 调用发送 numpy.ndarray
.这不能开箱即用,因为正如 xml-rpc docs,有一组固定的支持类型,基本上仅限于内置插件和内置插件的容器.
I have seen several questions asking how to send a numpy.ndarray
through an xml-rpc call. This cannot be done out of the box because, as stated by the xml-rpc docs, there is a fixed set of supported types, basically limited to build-ins and containers of built-ins.
一个简单的解决方案是使用
A simple solution is to convert the numpy array to a list using
<要发送的数组>.tolist()
在发送端,并使用
np.array(<我收到的列表>)
在接收方.但是,假设我希望能够直接支持 ndarray
,而无需显式展平/取消展平.有没有我可以实现的接口来完成这项工作?例如,我可以使用某些将在 xml-rpc 中注册并因此用于序列化/反序列化的扁平化/取消扁平化方法来扩展 ndarray
(我知道这很棘手)吗?
on the receiving side. However, suppose I want to be able to support ndarray
directly, without explicit flattening/unflattening. Is there an interface I can implement to make this work? For example, can I extend ndarray
(I know that's tricky) with certain flattening/unflattening methods which will be registered with xml-rpc and thus used for serialization/de-serialization?
推荐答案
我认为没有任何扩展的钩子.
I don't think there are any hooks for extension.
您可以对对象进行编码/取消编码,而不是展平/解展——例如使用 pickle 或 base64 编码的 pickle.
Rather than flatten/unflatten, you could encode/unencode the object -- for instance using pickle, or base64-encoded pickle.
如果您确实要展平,请记住您需要:
If you do flatten keep in mind that you need:
<array>.shape
<array>.dtype
<array>.flat
简单的dtypes可以来回转换为字符串;如果您想支持它们,您可能需要更仔细地考虑复杂的 dtype.
Simple dtypes can be converted back and forth to strings; you may need to consider complex dtypes more carefully if you want to support them.
简洁地回答您的问题:否 - 没有可用的扩展钩子.
To answer your question succinctly: No -- no extension hooks are available.
这篇关于python - 是否可以扩展 xml-rpc 可以序列化的内容集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!