[Numpy-discussion] recarray fun

Travis E. Oliphant oliphant@enthought....
Thu May 1 09:03:32 CDT 2008


Stéfan van der Walt wrote:
> 2008/5/1 Travis E. Oliphant <oliphant@enthought.com>:
>   
>> Stéfan van der Walt wrote:
>>  > 2008/4/30 Christopher Barker <Chris.Barker@noaa.gov>:
>>  >
>>  >> Stéfan van der Walt wrote:
>>  >>  > That's the way, or just rgba_image.view(numpy.int32).
>>  >>
>>  >>  ah -- interestingly, I tried:
>>  >>
>>  >>  rgba_image.view(dtype=numpy.int32)
>>  >>
>>  >>  and got:
>>  >>
>>  >>  Traceback (most recent call last):
>>  >>    File "<stdin>", line 1, in <module>
>>  >>  TypeError: view() takes no keyword arguments
>>  >>
>>  >>  Since it is optional, shouldn't it be keyword argument?
>>  >>
>>  >
>>  > Thanks, fixed in r5115.
>>  >
>>  >
>>  This was too hasty.   I had considered this before.
>>
>>  The problem with this is that the object can be either a type object or
>>  a data-type object.  You can use view to both re-cast a numpy array as
>>  another subtype or as another data-type.
>>
>>  So, please revert the change until a better solution is posted.
>>     
>
> OK, I see your point.  I'm working on a patch that does the following:
>
> def view(type_or_dtype=None, dtype=None, type=None):
>     if type_or_dtype:
>          if dtype:
>              raise ValueError("Cannot specify dtype twice")
>          if type:
>              raise ValueError("Cannot specify type twice")
>
>         if isinstance(type_or_dtype,py_type):
>             type = type_or_dtype
>
>         if isinstance(type_or_dtype,numpy_dtype):
>             dtype = type_or_dtype
>
>     return x.view(type=type).view(dtype=dtype)
>
> Would that be a satisfying solution?  I'll be back around 21:00 SAST
> to attend to the matter.
>   

Yes, I think that would work.   You need to do some checking for 
type=None and dtype=None as well, though.

That way, the first argument would continue to work as now but be 
labeled correctly, but it would also support dtype= and type= keywords.  

-Travis



More information about the Numpy-discussion mailing list