javascript to python

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Oct 2 18:16:30 CEST 2008


Joe Hrbek a écrit :
> Could someone help me translate to something that would close to it in
> python?  The anonymous functions are giving me problems.
> 
>     var dataListener = {
>       data : "",
>       onStartRequest: function(request, context){},
>       onStopRequest: function(request, context, status){
>         instream.close();
>         outstream.close();
>         listener.finished(this.data);
>       },
>       onDataAvailable: function(request, context, inputStream, offset,
> count){
>         this.data += instream.read(count);
>       },
>     };


class DataListener(object):
    def __init__(self):
        self.data = ''
    def on_start_request(self, request, context):
        pass
    def on_stop_request(self, request, context, status):
        instream.close()
        oustream.close()
        listener.finished(self.data)
    def on_data_available(self, request, context,
                          input_stream, offset, count):
        self.data += instream.read(count)

data_listener = DataListener()


Or if you want a more straightforward (but less pythonic) translation:


class dataListener(object):
    data = ''

    @classmethod
    def onStartRequest(this, request, context):
        pass

    @classmethod
    def onStopRequest(this, request, context, status):
        instream.close()
        oustream.close()
        listener.finished(this.data)

    @classmethod
    def onDataAvailable(this, request, context,
                          inputStream, offset, count):
        this.data += instream.read(count)





More information about the Python-list mailing list