Java versus Python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sharkBet
    Junior Member
    • Jan 2014
    • 7

    #1

    Java versus Python

    Hello,

    I am looking at the sample codes (https://github.com/betfair/API-NG-sample-code ).

    I am surprised to see the Java sample code so much longer than the python code.
    The Python code is 1 file of about 200 lines. Java is about 50 files, some of which are 200 lines.

    This makes me want to use Python rather than Java (I have experience with both).

    Am I missing something? Is there something for which Java is much better? Is there some downside in using Python?

    Thanks!
  • AlgoTrader
    Junior Member
    • Mar 2012
    • 243

    #2
    I am also very impressed with Python conciseness. Python is really great, it has somewhat strange syntax though.

    The scripting languages like Python, JavaScript, Perl are often preferable comparing to the languages like C#/Java/C++.

    I use JavaScript, it is not so concise as Python, but it has three different things, asynchronous operation (I don't care anything is pending, asynchronous stuff make possible do things in parallel), the other stuff is plain old C style syntax, the last one is JSON is Java Script Object Notation, every JSON is a valid JavaScript code.

    People who work with Java/C# think in term of classes, they need classes for everything. I prefer think in terms of objects, every API-NG request is object and every response is also object. JSON is a representation of objects, every JSON is in fact just a serialized form of object.

    Well, objects can be generated without classes! The example is:
    Code:
    var point = {x:100, y:200};
    The point is not all the objects are required to be instances of classes. The objects can be created directly! That's what the dynamic languages like Python, JS, PHP are about.

    If synchronous stuff is OK for you, then Python is the best choice.
    Betfair Bots Made Easy

    Comment

    • MrChameleon
      Junior Member
      • Apr 2009
      • 7

      #3
      Hi AlgoTrader

      Could you translate this from python to node.js please? It's from the BDP documentation (non-interactive login).

      My V6 app was in python but I'm keen to try node.js for NG. I have run the python code (it works) and tried and tried with the node.js 'https' and 'request' libraries but I just can't make it work!

      Code:
      #!/usr/bin/env python
       
      import requests
       
      #openssl x509 -x509toreq -in certificate.crt -out CSR.csr -signkey privateKey.key
       
       
      payload = 'username=myusername&password=password'
      headers = {'X-Application': 'SomeKey', 'Content-Type': 'application/x-www-form-urlencoded'}
       
      resp = requests.post('https://identitysso.betfair.com/api/certlogin', data=payload, cert=('client-2048.crt', 'client-2048.key'), headers=headers)
       
      if resp.status_code == 200:
        resp_json = resp.json()
        print resp_json['loginStatus']
        print resp_json['sessionToken']
      else:
        print "Request failed."
      Much obliged

      Chris

      Comment

      • Neophyte
        Junior Member
        • Sep 2016
        • 12

        #4
        Algotrader,

        I have been trying to build an application using javascript but the asynchronous nature of it is giving me a big headache. I have an app that works with about 10 markets but I cannot scale it up. I make a call to listMarketbook for 10 markets and then I make a call to listCurrent Orders. When I scale it up I make repeated calls to listMarketBook for 5 or 10 markets at a time, gathering prices before making a call to istCurrentOrders to check my positions. I realise that I could probably check my positions from listMarketBook which might solve my problem in the short term but it is bound to happen again so I need to work out how to deal with it.

        The problem is that when I make repeated calls to listMarketBook I invariably get locked out for TOO_MANY_REQUESTS after a few minutes because I have more than 3 calls queued to listMarketBook or listCurrentOrders at the same time. This happens because javascript does not wait for each call to complete before invoking the next one. I have started learning Python which seems popular and I believe would solve the problem but I thought I would have one final shot at understanding how to resolve this situation with Javascript.

        I have tried using both setTimeout and process.nextTick to control the situation with no success. With only 10 markets the app can run for days repeatedly looping round but scaling up creates the problem. Below is an illustration of the code I have tried using to control it in slices of 5 markets at a time but it won’t run for more than a few minutes.

        Can you point me in the right direction as to how to stop Javascript running asynchronously and thus breaching the data request limits of betfair exchange or should I switch to Python.

        var nn = Markets.length;
        // slice markets into slices of 5 markets at a time
        for(var y = 0; y < Math.floor(nn/5)+1; y++) {
        var marketSlice ;
        marketSlice = Markets.slice(y*5,y*5+5);

        console.log("marketSlice = "+marketSlice);
        if(marketSlice.length ===0) continue;
        // getmarketBook is the function which invokes listmarketBook
        process.nextTick(getMarketBook,options, selection,marketSlice);
        } // end of y loop
        // if y has reached the end of the markets go on to listCurrentOrders
        if(y===Math.floor(nn/5+1)){
        console.log(" y = "+y);
        process.nextTick(checkOrders,options,selection);
        // have also tried setTimeout(checkOrders,0,options,selection);
        // checkOrders is the function which invokes listCurrentOrders
        }

        Comment

        • Lopiner
          Junior Member
          • Feb 2009
          • 117

          #5
          Hi Neophyte

          Algotrader is much more experience than i am, i'm a VB.net newbie at most but i have run into similar problems.
          You are much better off using listMarketBook for prices and orders, this way you can be sure that your information about prices and orders is in synch and you can think about a market as an independent instance that has no more relations in your code, no more secondary calls need to me made (this is much more error resilient), all the information is there, you just need to be aware of some global information like your balance, maybe your daily PL, etc.

          To solve the problem of the "TOO_MANY_REQUESTS" you can have a shared list/dictionary of market ID's that are waiting to be executed and whenever you put a market in queue for a listMarketBook you first check if the marketid is in there in queue, if its not you put it there and add it to the listMarketBook. When you receive your data from the listMarketBook response you make sure to remove the marketid from the waiting list. This is scalable and can be run asynchronously.
          I have seen this queue pattern a lot in other situations, its just like a waiting line, every thread can put someone waiting in line if he is not there already. Your listMarketBook will consume the maximum number of market id's it can consume from the list.
          fooledbyabet.com

          Comment

          • Neophyte
            Junior Member
            • Sep 2016
            • 12

            #6
            Lopiner,

            Many thanks for this response and I am sorry I have not thanked you sooner. No matter how many times I set Forum options to email me when there is a response on a thread I have replied to it still does not tell me.

            I didn’t think there were any replies to this question so I gave up on Javascript and started to learn Python. I am now using Python and T00_MANY_REQUESTS has never been a problem with it. Nevetheless I have noted your suggestion and will try and remember it as it sounds like a good technique that would have helped me with asynchronous programming.

            I have also switched to using ListMarketBook for Orders as well as prices. Much more efficient but first I got Python to work using ListMarketBook followed by listCurrent Orders just to make sure I had beaten my original problem.

            Many thanks for your help which certainly pointed me in the right direction.

            Comment

            Working...
            X