问题
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
<array to send>.tolist()
on the sending side, and convert back to a numpy array using
np.array(<list I received>)
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?
回答1:
I don't think there are any hooks for extension.
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
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.
[EDIT] To answer your question succinctly: No -- no extension hooks are available.
来源:https://stackoverflow.com/questions/23399836/python-is-it-possible-to-extend-the-set-of-things-that-xml-rpc-can-serialize