Running NumPy code in a Python Anywhere web console

In Chapter 1, we already saw a Python Anywhere console in action, without having an account. This recipe will require you to have an account, but don't worry—it's free; at least if you don't need too many resources.

Signing up is a pretty straightforward process and will not be covered here. NumPy is already installed along with a long list of other Python software. For a complete list, see https://www.pythonanywhere.com/batteries_included/.

We will setup a simple script that gets price data from Google Finance every minute, and does simple statistics with the prices using NumPy.

How to do it...

Once we have signed up, we can login and have a look at the Python Anywhere dashboard:

How to do it...
  1. Write the code.

    The complete code for this example is as follows:

    import urllib2
    import re
    import time
    import sys	
    import numpy
    
    prices = numpy.array([])
    
    for i in xrange(3):
      req = urllib2.Request('http://finance.google.com/finance/info?client=ig&q=' + sys.argv[1])
      req.add_header('User-agent', 'Mozilla/5.0')
      response = urllib2.urlopen(req)
      page = response.read()
      m = re.search('l_cur" : "(.*)"', page)
      prices = numpy.append(prices, float(m.group(1)))
      avg = prices.mean()
      sigma = prices.std()
     
       evFactor = float(sys.argv[2])
      bottom = avg - devFactor * sigma
      top = avg + devFactor * sigma
      timestr = time.strftime("%H:%M:%S", time.gmtime())
     
      print timestr, "Average", avg, "-Std", bottom, "+Std", top 
      time.sleep(60)

    Most of it is standard Python, except the bits where we grow a NumPy array containing prices and calculate the mean and standard deviation of the prices. A URL is used to download price data in JSON format from Google Finance given a stock ticker such as AAPL. This URL could change, of course.

    Next, we parse the JSON with regular expressions to extract a price. This price is added to a NumPy array. We compute the mean and standard deviation for the prices. The price is printed with a timestamp bottom and top, based on the standard deviation times some factor to be specified by us.

  2. Upload the code.

    After we are done with the code on our local machine, we can upload the script to Python Anywhere. Go to the dashboard, and click on the Files tab. Upload the script from the widget at the bottom of the page.

  3. Run the code.

    To run the code, click on the Consoles tab, and then click on the Bash link. Python Anywhere should create a bash console for us right now.

    We can now run our program for AAPL with a one standard deviation band, as shown in the following screenshot:

    How to do it...

How it works...

Python Anywhere is perfect if you want to run NumPy code on a remote server; especially, if you need your program to execute at scheduled times. For the free account, at least, it's not so convenient to do interactive work, since there is a certain lag whenever you enter text in the web console.

However, as we saw, it is possible to create and test a program locally, and upload it to Python Anywhere. This frees resources on your local machine as well. We can do fancy things such as sending emails based on the stock price, for instance, or schedule our scripts to be activated during trading hours. By the way, this is also possible with Google App Engine, but it is done the Google way; so you will need to learn about their API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset