From: Francesc A. <fa...@ca...> - 2007-02-13 19:14:03
|
A Dimarts 13 Febrer 2007 19:49, escrigu=C3=A9reu: > Hello Francesc. > > >Mmm, after considering this a bit more seriously, I think that I'll go > > towards > > >implementing the alternative: > > > >row.fetch_all_fields() > > > >because: > > > >- It would be simpler and more efficient to implement (i.e. > > Row.__getitem__ will not be cluttered with more logic). > > > >- Accepting a [:] would make users think that complete extended slicing > > suport > > >(like [colstart:colstop:colstep]) would be implemented and it will be not > > (at > > >least initially). > > I think that slicing is very easy to implement if we have something like > fetch_all_fields which will return a list: > > def __getslice__(self, a, b): > return self.fetch_all_fields().__getslice__(a,b) > > , so both ways can be implemented at once. That would be nice, but unfortunately, things are not so easy. =46irst, __getslice__() is deprecated, so I'll assume that you mean __getit= em__. Second, NumPy doesn't support slices (even not integers!) in scalar record= =20 types (the kind of values I'm thinking to be returned=20 by .fetch_all_fields()), as can be seen in: In [54]: a=3Dnumpy.array((1,2,"asa"), "i2,i2,a3") In [55]: a['f0'] # name fields are the only indexes allowed Out[55]: 1 In [56]: a[1] =2D------------------------------------------------------------------------= =2D- <type 'exceptions.IndexError'> Traceback (most recent call last) /home/faltet/python.nobackup/Pyrex-0.9.5.1a/<ipython console> in <module>() <type 'exceptions.IndexError'>: 0-d arrays can't be indexed In [57]: a[:] =2D------------------------------------------------------------------------= =2D- <type 'exceptions.ValueError'> Traceback (most recent call last) /home/faltet/python.nobackup/Pyrex-0.9.5.1a/<ipython console> in <module>() <type 'exceptions.ValueError'>: cannot slice a 0-d array And thirdly, even though NumPy would eventually support a generalized slici= ng=20 for scalar records, Row.__getitem__ is very carefully tuned (i.e. it has lo= w=20 level logic inside) for dealing only with field names. So, just doing thing= s=20 like: return self.fetch_all_fields().__getitem__(...) is a simplistic aproximation that would render unusable many of the existin= g=20 optimizations in Row.__getitem__(). Cheers, =2D-=20 >0,0< Francesc Altet =C2=A0 =C2=A0 http://www.carabos.com/ V V C=C3=A1rabos Coop. V. =C2=A0=C2=A0Enjoy Data "-" |