javascript to python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Hrbek

    javascript to python

    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(reques t, context){},
    onStopRequest: function(reques t, context, status){
    instream.close( );
    outstream.close ();
    listener.finish ed(this.data);
    },
    onDataAvailable : function(reques t, context, inputStream, offset,
    count){
    this.data += instream.read(c ount);
    },
    };
  • lkcl

    #2
    Re: javascript to python


    On Oct 2, 5:54 pm, Joe Hrbek <joe.hr...@gmai l.comwrote:
    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(s elf, 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

    instream.close( )
    outstream.close ()
    listener.finish ed(self.data)

    def onDataAvailable (self, request, context, inputStream,
    offset, count):

    global instream

    self.data += instream.read(c ount)


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

    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.

    Comment

    • lkcl

      #3
      Re: javascript to python

      On Oct 2, 7:42 pm, Bruno Desthuilliers
      <bdesth.quelque ch...@free.quel quepart.frwrote :
      lkcl a écrit :
      >
      >
      >
      On Oct 2, 5:54 pm, Joe Hrbek <joe.hr...@gmai l.comwrote:
      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(s elf, 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.finish ed(self.data)
      >
      def onDataAvailable (self, request, context, inputStream,
      offset, count):
      >
      global instream
      >
      idem
      >
      self.data += instream.read(c ount)
      >
      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.__ini t__ = 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 "dataListen er". 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.onSto pRequest = function(reques t, context,
      status) {
      instream.close( );
      outstream.close ();
      listener.finish ed(this.data);
      };
      __dataListener. prototype.onDat aAvailable = function(reques t,
      context, inputStream, offset, count) {
      this.data += instream.read(c ount);
      };
      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(t his, request, context, status):
      instream.close( )
      oustream.close( )
      listener.finish ed(this.data)

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

      class dataListener:
      def __init__(self):
      self.data = ''
      self.onStartReq uest = onStartRequest
      self.onStopRequ est = onStopRequest
      self.onDataAvai lable = 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.

      Comment

      • lkcl

        #4
        Re: javascript to python

        On Oct 3, 10:29 am, Bruno Desthuilliers <bruno.
        42.desthuilli.. .@websiteburo.i nvalidwrote:
        lkcl a écrit :On Oct 2, 7:42 pm, Bruno Desthuilliers
        <bdesth.quelque ch...@free.quel quepart.frwrote :
        lkcl a écrit :
        Not 'mine' - I'm not the OP.
        whoops, yes - i missed that. sorry!
        And as far as I'm concerned, the point is
        exactly here : it's doing "a little bit more" than the original code.
        yeah, i know. and that "bit more" gets you a proper representation
        of the python "class" concept.

        i was merely pointing out that if you want to _really_ translate the
        original code into python - _really_ strictly - it's not actually
        possible. because python doesn't have the concept of non-prototyping
        (but... see below: i believe i may stand corrected on that)

        [..snip..] and is
        actually useless for the OP's use case (else the OP's code wouldn't use
        litteral object notation but a full-blown prototype).
        i know :)
        it's
        overriding the "prototype" of dataListener, making it a true "class"
        object.
        >
        There's nothing like a notion of "class" in javascript - as you of
        course already know.
        okay,okay :) class-like :)

        Indeed. But the point is that Python - while close to a prototype-based
        language in many aspects - is still class-based. The closer Python
        translation of the OP's javascript snippet is probably the one I gave
        using a 'class singleton' - that is, using the class itself as an
        object.
        oh is _that_ how you do it. thanks. i always wondered how you did
        class singletons in python.
        python-source-to-javascript-source tool like pyjamas won't give you back
        the original javascript snippet (which is by no mean a criticism of
        pyjamas - it would just be way too complicated to automatize such a
        translation).
        well... you _say_ that... but... actually, if that's the real way to
        represent class singletons, and it's an accurate representation of the
        OP's javascript, and a good example, then _yes_, pyjs should
        definitely have that added as a feature - to understand that a class
        singleton _can_ get mapped to the much more efficient javascript
        example you gave.

        not that many people would _want_ to do that, so it goes onto the
        "diminishin g returns TODO list", but...
        Nope. You defined functions outside the object's scope, and you still
        have to instanciate dataListener. Also, this above code just won't work
        - unless you explicitely pass the dataListener instance to the
        functions, ie:
        >
        d = dataListener()
        d.onDataAvailab le(d, ...)
        yeah - i didn't realise what the python class singleton thing was.
        It seem you didn't read my other translation proprosal, so I repost it here:
        class dataListener(ob ject):
        data = ''
        i did - i just didn't understand its significance.

        so - to get this straight: when you do class clsname(object) , and you
        have the indentation and declaration of variables (e.g. data, above)
        at the same level as the functions, it means that there's only one of
        them? (i.e. a singleton)?

        so, if i do this:

        d = dataListener()
        e = dataListener()

        d.data = "fred"

        print f.data

        will return "fred"?

        l.

        Comment

        • lkcl

          #5
          Re: javascript to python

          so, if i do this:
          >
          d = dataListener()
          e = dataListener()
          >
          d.data = "fred"
          >
          print f.data
          duh, duh - that should be print e.data :)

          Comment

          Working...