javascript to python

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Oct 2 21:42:35 CEST 2008


lkcl a écrit :
> On Oct 2, 5:54 pm, Joe Hrbek <joe.hr... at gmail.com> 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
> (see http://pyjamas.sf.net) and compare the
> resultant javascript to your original code-fragment.
> 
> l.

I did. Here's the result:

function __dataListener() {
}
function dataListener() {
     var instance = new __dataListener();
     if(instance.__init__) instance.__init__.apply(instance, arguments);
     return instance;
}


function __dataListener_initialize() {
     if(__dataListener.__was_initialized__) return;
     __dataListener.__was_initialized__ = true;
     pyjs_extend(__dataListener, __pyjslib_Object);
     __dataListener.prototype.__class__.__new__ = dataListener;
     __dataListener.prototype.__init__ = function() {
     var data = '';
     };
     __dataListener.prototype.__class__.__init__ = function() {
         return 
__dataListener.prototype.__init__.call.apply(__dataListener.prototype.__init__, 
arguments);
     };
     __dataListener.prototype.__class__.__init__.unbound_method = true;
     __dataListener.prototype.__init__.instance_method = true;
     __dataListener.prototype.onStartRequest = function(request, context) {
     };
     __dataListener.prototype.__class__.onStartRequest = function() {
         return 
__dataListener.prototype.onStartRequest.call.apply(__dataListener.prototype.onStartRequest, 
arguments);
     };
     __dataListener.prototype.__class__.onStartRequest.unbound_method = 
true;
     __dataListener.prototype.onStartRequest.instance_method = true;
     __dataListener.prototype.onStopRequest = function(request, context, 
status) {
     instream.close();
     outstream.close();
     listener.finished(this.data);
     };
     __dataListener.prototype.__class__.onStopRequest = function() {
         return 
__dataListener.prototype.onStopRequest.call.apply(__dataListener.prototype.onStopRequest, 
arguments);
     };
     __dataListener.prototype.__class__.onStopRequest.unbound_method = true;
     __dataListener.prototype.onStopRequest.instance_method = true;
     __dataListener.prototype.onDataAvailable = function(request, 
context, inputStream, offset, count) {
     this.data += instream.read(count);
     };
     __dataListener.prototype.__class__.onDataAvailable = function() {
         return 
__dataListener.prototype.onDataAvailable.call.apply(__dataListener.prototype.onDataAvailable, 
arguments);
     };
     __dataListener.prototype.__class__.onDataAvailable.unbound_method = 
true;
     __dataListener.prototype.onDataAvailable.instance_method = true;
}
__dataListener_initialize();


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 !-)



More information about the Python-list mailing list