[Numpy-discussion] new __wrap_array__ magic method

Sebastien.deMentendeHorne at electrabel.com Sebastien.deMentendeHorne at electrabel.com
Wed Aug 10 05:08:57 CDT 2005


Hi,

Currently, we have an __array__ magic method that can be used to transform any object that implements it into an array.
It think that a more useful magic method would be, for ufuncs, a __wrap_array__ method that would return an array object and a function to use after having applied the ufunc.

For instance:

class TimeSerie:
	def __init__(self, data, times):
		self.data = data      # an array
		self.times = times    # anything, could be any metadata

	def __wrap_array__(self, ufunc):
		return self.data, lambda data: TimeSerie(data, self.times)

t = TimeSerie( arange(100), range(100) )
cos(t) # returns a TimeSerie object equivalent to TimeSerie( cos(arange(100)), range(100) )

This needs probably a change in the ufunc code that would first check if __wrap_array__ is there and if so, use it to get an array as well as a "constructor" to use for returning an object other than an array.

Benefits:
 - easier to wrap array objects with metadata without rewriting all ufunc (see MaskedArray for problematic).
 - ufunc( list ) -> list and ufunc( tuple ) -> tuple instead of returning always arrays.

Do you see an interest of doing so ? Does it need a lot of internal changes to Numeric/numarray/scipy.core ?

Best,

Sebastien


=======================================================
This message is confidential. It may also be privileged or otherwise protected by work product immunity or other legal rules. If you have received it by mistake please let us know by reply and then delete it from your system; you should not copy it or disclose its contents to anyone. All messages sent to and from Electrabel may be monitored to ensure compliance with internal policies and to protect our business. Emails are not secure and cannot be guaranteed to be error free as they can be intercepted, amended, lost or destroyed, or contain viruses. Anyone who communicates with us by email is taken to accept these risks.

http://www.electrabel.be/homepage/general/disclaimer_EN.asp
=======================================================





More information about the Numpy-discussion mailing list