[Twisted-Python] cursors
Someone, either Glyph or Tommi, mentioned that they wanted cursors a while back. "What's that," I asked; then thought "hm. I could do that." But I didn't. Only, now I did. I think. -- The moon is waxing gibbous, 58.9% illuminated, 8.2 days old. # -*- Python -*- # Kevin Turner <acapnotic@foobox.net>, November 23, 2001 # This code is in the Public Domain. from UserList import UserList import types class CursoredList(UserList): _cursorlist = None def __init__(self, seq=None): UserList.__init__(self, seq) def setCursor(self): if self._cursorlist is None: self._cursorlist = [] self._cursorlist[:] = range(len(self.data)) self._cursorLen = len(self.data) def cursor(self, key): if self._cursorlist is None: return self.__getitem__(key) if isinstance(key, types.SliceType): start, stop, step = key.start, key.stop, key.step if not step: step = 1 if start is None: start = 0 elif start < 0: start = self._cursorLen + start if (stop is None) or (stop > self._cursorLen): stop = self._cursorLen elif stop < 0: start = self._cursorLen + stop result = [] for i in xrange(start, stop, step): result.append(self.cursor(i)) return result else: key = int(key) if key < 0: key = self._cursorLen + key assert(0 <= key < self._cursorLen) new_key = self._cursorlist.index(key) return self.__getitem__(new_key) def __setitem__(self, key, value): if (self._cursorlist is not None) \ and isinstance(key, types.SliceType): self._cursorlist[key.start:key.stop] = [None] * len(value) if isinstance(key, types.SliceType): self.data[key.start:key.stop] = value else: self.data[key] = value def __getitem__(self, key): if isinstance(key, types.SliceType): return self.data[key.start:key.stop] else: return self.data[key] def __delitem__(self, key): if not isinstance(key, types.SliceType): key = slice(key, key+1) self.__setitem__(key, []) def __getslice__(self, start, stop): return self.__getitem__(slice(start, stop)) def __setslice__(self, start, stop, value): return self.__setitem__(slice(start, stop), value) def __delslice__(self, start, stop): return self.__delitem__(slice(start, stop)) def __iadd__(self, other): self.extend(other) return self def __imul__(self, multiplier): if self._cursorlist is not None: self._cursorlist.extend(([None] * len(self.data)) * (multiplier-1)) self.data[:] = self.data * multiplier def append(self, item): self[len(self):len(self)] = [item] def insert(self, i, item): self[i:i] = [item] def pop(self, i=-1): value = self[i] del self[i] return value def remove(self, item): i = self.index(item) del self[i] def reverse(self): self.data.reverse() if self._cursorlist is not None: self._cursorlist.reverse() def sort(self, cmpfunc=None): if self._cursorlist is not None: zipped = map(None, self.data, self._cursorlist) if cmpfunc: # The supplied cmpfunc won't be expecting the zipped list. cmpfunc_ = lambda x, y, c=cmpfunc: c(x[0], y[0]) zipped.sort(cmpfunc_) else: zipped.sort() for i in range(len(zipped)): self.data[i], self._cursorlist[i] = zipped[i] else: self.data.sort() def extend(self, other): self[len(self):len(self)] = other
Kevin Turner <acapnotic@twistedmatrix.com> writes:
Someone, either Glyph or Tommi, mentioned that they wanted cursors a while back. "What's that," I asked; then thought "hm. I could do that." But I didn't. Only, now I did. I think.
The idea was to replace the web.widget Deferreds bookkeeping with such a construct. Currently, all widgets to be output know their position in the list, and when a Deferred returns [deferred2, "foo", deferred3], and later deferred2 return ["bar", "baz"], deferred3's index is wrong. If instead of indexes, the Deferreds had cursors, that changed as the list modified, they'd survive. Of course, an even bigger rewrite of web.widgets and Deferred processing may be wanted. Sadly, I have no time to hack on that.. -- tv@{{hq.yok.utu,havoc,gaeshido}.fi,{debian,wanderer}.org,stonesoft.com} double a,b=4,c;main(){for(;++a<2e6;c-=(b=-b)/a++);printf("%f\n",c);}
On Sat, 2001-11-24 at 08:10, Tommi Virtanen wrote:
Kevin Turner <acapnotic@twistedmatrix.com> writes:
Someone, either Glyph or Tommi, mentioned that they wanted cursors a while back. "What's that," I asked; then thought "hm. I could do that." But I didn't. Only, now I did. I think.
The idea was to replace the web.widget Deferreds bookkeeping with such a construct. Currently, all widgets to be output know their position in the list, and when a Deferred returns [deferred2, "foo", deferred3], and later deferred2 return ["bar", "baz"], deferred3's index is wrong.
If instead of indexes, the Deferreds had cursors, that changed as the list modified, they'd survive.
Of course, an even bigger rewrite of web.widgets and Deferred processing may be wanted.
Sadly, I have no time to hack on that..
That's what I'm working on right now (see my last 3 rants on the list). I could use a bit of advice, I think, though. Maybe I'll catch you on IRC soon. So far I've refactored all normal non-deferred stuff, and I'm taking a fresh look at implementing Deferreds in widgets. Right now I'm *trying* to figure out a clever way to allow deferreds to not need to worry about position -- but I'm just now starting to figure something out. I'll talk to you some time. -- <spiv> NeuroMorphus: That's not really meaningful, though. <NeuroMorphus> spiv: it's not a matter of meaning, it's an assignment -- Chris Armstrong <<< radix@twistedmatrix.com >>> http://twistedmatrix.com/users/carmstro.twistd/
participants (3)
-
Chris Armstrong
-
Kevin Turner
-
Tommi Virtanen