No subject


Thu Nov 16 16:52:29 CST 2006


"""You can also customize ctypes argument conversion to allow instances of
your own classes be used as function arguments. ctypes looks for an
_as_parameter_ attribute and uses this as the function argument. Of course,
it must be one of integer, string, or unicode: ...

If you don't want to store the instance's data in the _as_parameter_
instance variable, you could define a property  which makes the data
available."""

If I understand correctly, you could also accomplish the same thing by
implementing the from_param class method.

I don't think it's well defined what _as_parameter_ (or from_param) should
do for an arbitrary NumPy array, so there are a few options.

1. Allow the user to add _as_parameter_ or from_param to an ndarray
instance. I don't know if this is possible at all (it doesn't seem to work
at the moment because ndarray is a "built-in" type).

2. Allow _as_parameter_ to be a property with the user being able to specify
the get method at construction time (or allow the user to specify the
from_param method). For svm_node I might do something like:

def svm_node_as_parameter(self):
    return cast(self.dataptr, POINTER(svm_node))

svm_node_descr = \
dtype({'names' : ['index', 'value'],
       'formats' : [N.intc, 'f8']},
      align=1)

node = array([...],
             dtype=svm_node_descr,
             ctypes_as_parameter=svm_node_as_parameter)

3. As a next step, provide defaults for _as_parameter_ where possible. The
scalar types could set it to the corresponding ctype (or None if ctypes
can't be imported). Arrays with "basic" data, such as 'f8' and friends could
set up a property that calls ctypes.cast(self.dataptr,
POINTER(corresponding_ctype)).

Thanks for reading. :-) Comments would be appreciated. If some of my
suggestions seem implementation-worty, I'd be willing to try to implement
them.

Regards,

Albert





More information about the Numpy-discussion mailing list