On Oct 2, 7:42 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > lkcl a écrit : > > > > > On Oct 2, 5:54 pm, Joe Hrbek <[EMAIL PROTECTED]> wrote: > >> Could someone help me translate to something that would close to it in > >> python? The anonymous functions are giving me problems. > > > class dataListener: > > def __init__(self): > > data = "" > > def onStartRequest(self, request, context): > > pass > > def onStopRequest(self, request, context, status): > > > # TODO: pass these three in to dataListener as > > # params to the constructor, so it's > > # def __init__(self, instream, outstream, listener) > > # and do self.instream = instream > > > global instream > > global outstream > > global listener > > Since you don't rebind them, you don't need the global statement for > these three identifiers > > > instream.close() > > outstream.close() > > listener.finished(self.data) > > > def onDataAvailable(self, request, context, inputStream, > > offset, count): > > > global instream > > idem > > > self.data += instream.read(count) > > And then you have a class. Calling instance methods on the class won't > work. Looks like there's something missing... > > > question. > > > why are request and context being ignored? > > why is there an inputStream argument to onDataAvailable, yet > > there's a global variable (in the javascript) called > > instream? is it the same? > > > all this, and more, thanks to the awfulness that is javascript :) > > None of "this, and more" is because of javascript. You'll find bad code > in every language (and without more context, you can't tell if it's bad > code - might as well be the right thing to do). > > FWIW, javascript is a very interesting and powerful language. > > > for fits and giggles, compile the above python using > > pyjs.py, the python-to-javascript compiler > > (seehttp://pyjamas.sf.net) and compare the > > resultant javascript to your original code-fragment. > > > l. > > I did. Here's the result:
ok - these are the "important" bits. notice that the pyjamas compiler is doing a little bit more than your original code: it's overriding the "prototype" of dataListener, making it a true "class" object. this is where there's a key departure from the original code and the translation to python: the original code isn't actually a class, at all - it's more like a.... c struct that has function pointers in it. by declaring a python "class", the javascript equivalent is to add to "prototypes". > __dataListener.prototype.__init__ = function() { > var data = ''; > }; [ ... so for example here, now when you declare _any number_ of "dataListener"s, each and every one will have its __init__ function called. in your original example, you're effectively making one and only one "dataListener". you're kinda... it's a bit like having a lambda-class (nameless class) and declaring one and only one instance of that python class... ] > __dataListener.prototype.onStopRequest = function(request, context, > status) { > instream.close(); > outstream.close(); > listener.finished(this.data); > }; > __dataListener.prototype.onDataAvailable = function(request, > context, inputStream, offset, count) { > this.data += instream.read(count); > }; so - yeah, you can see that (apart from the .prototype, which is necessary to make a "class a la javascript") it's a pretty accurate translation back to the original javascript. > All this, and more, thanks to the strange idea that it would be better > to write javascript in Python instead of writing it in javascript !-) *lol* :) fortunately, pyjs.py does that translation for you ha ha. like they say on brainiac, "STOP! we do these experiments, so you don't have to". yes - it's because in the translated python, dataListener was declared as a class, whereas in the original javascript, the corresponding concept (prototypes) are not made use of. if you wanted to experiment, you could try this: def onStartRequest(this, request, context): pass def onStopRequest(this, request, context, status): instream.close() oustream.close() listener.finished(this.data) def onDataAvailable(this, request, context, inputStream, offset, count): this.data += instream.read(count) class dataListener: def __init__(self): self.data = '' self.onStartRequest = onStartRequest self.onStopRequest = onStopRequest self.onDataAvailable = onDataAvailable which you will find to be more accurately representative of the original javascript, conceptually. i.e taking into account that in the _original_ javascript, you don't have any "prototypes". but - i don't believe it to be what you actually want, even though it's "a slightly more accurate representation". l. -- http://mail.python.org/mailman/listinfo/python-list