----- Original Message -----
> From: Albert-Jan Roskam <fo...@yahoo.com> > To: eryksun <eryk...@gmail.com> > Cc: "tutor@python.org" <tutor@python.org> > Sent: Monday, September 17, 2012 9:42 AM > Subject: Re: [Tutor] is this use or abuse of __getitem__ ? > >& lt;snip> > >> >> http://code.google.com/p/psutil >> > > > I looked at the website and it looks like a cool module. Some of it appeared > to > be *nix only, but I'll have to dig into it more. > But it's also just like you said: how far should one go with micromanaging > things? Maybe some things are the caller's responsibility. > > >> If you're also supporting the iterator protocol with the __iter__ >> method, then I think a helper _items(start, stop, step) generator >> function would be a good idea. >> >> Here's an updated example (not tested however; it's just a > suggestion): >> >> >> import operator >> >> def _items(self, start=0, stop=None, step=1): >> if stop is None: >> stop = self.nCases >> >> for i in range(start, stop, step): >> retcode1 = self.iomodule.SeekNextCase(self.fh, > ctypes.c_long(i)) >> self.caseBuffer, self.caseBufferPtr = self.getCaseBuffer() >> retcode2 = self.iomodule.WholeCaseIn(self.fh, > self.caseBufferPtr) >> record = struct.unpack(self.structFmt, self.caseBuffer.raw) >> if any([retcode1, retcode2]): >> raise RuntimeError("Error retrieving record %d [%s, >> %s]" % >> (i, retcodes[retcode1], retcodes[retcode2])) >> yield record >> >> >> def __iter__(self): >> return self._items() >> >> >> def __getitem__(self, key): >> >> is_slice = isinstance(key, slice) >> >> if is_slice: >> start, stop, step = key.indices(self.nCases) >> else: >> key = operator.index(key) >> start = key + self.nCases if key < 0 else key >> if not 0 <= start < self.nCases: >> raise IndexError >> stop = start + 1 >> step = 1 >> >> records = self._items(start, stop, step) >> if is_slice: >> return list(records) >> return next(records) >> >> > > Awesome, thank you so much. This has been very inspiring! When this is > working, > I'm thinking about also implementing an Ellipsis (just because it's > possible ;-), so I can cut out certain parts of the data using a numpy array. > Another idea would be to also use __getitem__ as a dictionary. So when the > data > contains an id (let's say ssn), r = Reader(key="ssn"); > r.data["87654321"] returns the corresponding (first available) record. > But although this is cool from an educational perspective, I do wonder > whether, > from a design perspective, it's a good idea to make __getitem__ this fully > packed. > To respond to my own reply: I couldn't resist checking out numpy/Ellipsis (maybe because "Ellipsis" is such a weird statement ;-) Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 >>> import numpy >>> class C (object): def __init__(self, records): self.records = records def __getitem__(self, key): print key, type(key) if key is Ellipsis or Ellipsis in key or isinstance(key, tuple): return numpy.array(self.records)[key].tolist() >>> records = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> c = C(records) >>> c[...] # all records Ellipsis [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> c[1,...] # second record (1, Ellipsis) <type 'tuple'> [4, 5, 6] >>> c[...,1] # second column (Ellipsis, 1) <type 'tuple'> [2, 5, 8] >>> c[0,1] # value on first row, second column (0, 1) <type 'tuple'> 2 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor