Hello. Need a little help with numpypy. I want to implement partition method for numpy array. Let's say I can compile npy_partition.h.src, import it through CFFI. Therefore I can write a python function my_partition(numpy_array, other_arguments...) that performs partitioning for a give numpy array. Now I want to create partition method for ndarray. As far as I understand methods of ndarray are defined in pypy/module/micronumpy/ndarray.py in a special way and ndarray.partition = my_partition doesn't work. Is it possible to add method to ndarray inside numpypy, not inside pypy's micronumpy module? -- Sergey
On 23/01/2016 6:24 PM, Sergey Matyunin wrote:
Hello.
Need a little help with numpypy. I want to implement partition method for numpy array. Let's say I can compile npy_partition.h.src, import it through CFFI. Therefore I can write a python function my_partition(numpy_array, other_arguments...) that performs partitioning for a give numpy array.
Now I want to create partition method for ndarray. As far as I understand methods of ndarray are defined in pypy/module/micronumpy/ndarray.py in a special way and ndarray.partition = my_partition doesn't work.
Is it possible to add method to ndarray inside numpypy, not inside pypy's micronumpy module? Thanks for picking this up. I would suggest you first play around with implementing partition in cffi, it may take a while to get the interface just right and you may decide that this implementation design is too unwieldy.
Here is how I would add the app level function to ndarray, similar to the tactic taken for set_string_function and ndarray.__repre__ - create a function in module/micronumpy/appbridge.py that accepts your partition function and stores it in a cache. - Exposed this new function in _numpypy by adding it to module/micronumpy/__init__.py. - create a default descr_partition() function in module/micronumpy/ndarray.py that raises a w_NotImplementedError if the cache entry is empty (see descr_repr for an example of how to use the cache function if it has been assigned) - Add the call to your new function in step 1 into numpy/core/multiarray.py, which is only used in pypy (multiarray is a compiled extension module in cpython) Matti
participants (2)
-
Matti Picus
-
Sergey Matyunin