Tag Archives: OS X

Get started with Python on a Mac

Here are the steps I recommend for getting started with python on a Mac (OS X), particularly if you are fairly new to the language.

  1. The Mac comes with python installed, and you can run this directly from the terminal application with the command “python”.  However there is a very nice python interpreter called the “ipython notebook” which I strongly recommend using too.  Installation instructions are available here - specifically, I downloaded Enpackage Canopy and typed two commands into the terminal.  You should now be able to get an ipython notebook running in your internet browser with the terminal command:
    cd somewhere/sensible/
    ipython notebook --pylab=inline

    The last part of this means that plots get drawn inside the notebook, which is handy. If you forget to add that, you can type into the notebook itself: %pylab inline.
    Note you use shift-Enter to run all the python commands in a cell.

  2. Install pip.  This lets you easily download python packages and will be very handy later.  This is easy – from the terminal type:
    sudo easy_install pip
  3. More advanced users may want to install virtualenv, so that different projects you work on can use different versions of packages. If you’re planning on putting any of your code in production, this is a must. But if you’re just getting started, ignore it for now.
  4. Advanced users will also want to install git so you can incrementally save your own versions of code. If you’re just starting out, leave this for later too.

OK, now let’s dive in the deep end by loading some financial data from Quandl, then manipulating it with Pandas and plotting it with matplotlib. You’ll need Pandas and the Quandl package, which you can get by typing into the terminal:

pip install pandas
pip install Quandl

Now in your ipython notebook type (I recommend doing each group of statements in its own cell, so that you can run them separately; remember it’s shift-enter to run the statements):

import numpy
import pandas
import Quandl

assets = ['OFDP.ALUMINIUM_21.3',
          'OFDP.COPPER_6.3',
          'OFDP.LEAD_31.3',
          'OFDP.GOLD_2.3',
          'OFDP.ZINC_26.3']

data = Quandl.get(assets)
data.head()

data['2012':].plot()

This link gives ways you can develop the plot further.

To give an example of how to manipulate the data, you could try:

# show recent daily returns, normalised by asset volatility (ie. z-scores)
data = data.sort_index() # ensure oldest comes first
returns = (data/data.shift(1))
log_returns = numpy.log(returns)
vols = pandas.ewmstd(log_returns,span=180)  # daily vol, exp weighted moving avg
z = (returns-1)/vols # calc z-scores
z[z>50] = numpy.nan  # remove very bad data
z[z<-50] = numpy.nan
z.tail() # take a look at the most recent z-scores

That’s a very quick introduction to installing and using python on the Mac. I hope it’s helpful!
If you want more info about writing in Python itself, this link looks like a comprehensive site.

Let me know if you have any comments or improvements.

  

My first Mac app

I decided to take a break from programming iOS apps, and wrote my first Mac OS X app today.  It was surprisingly simple.  It is based on the NSDocument class, and there is an Xcode template for this.  This template has all the menus, including cutting and pasting, formatting, etc, already built in and function. The main additions to the template were putting an NSScrollView in the window in the Document.xib file so I could edit some text, and adding some code to save and read files.

Of course, there were a few hiccups, which I mostly overcame:

  • The Document.h header file does not care about the NSScrollView; you need to hook it up to the NSTextView instead.  The formatted text sits within the text view, and can be accessed via the textStorage method. This has class NSTextStorage, which is a subclass of NSAttributedString.
  • The vaguely named dataOfType:error: method is for saving data, and the readFromData:ofType:error: method for reading data.
  • I posted the approach I used for reading and writing at Stack Overflow here, because I suspect the way I came up with is not guaranteed to work.
  • You should not set the text in the text view in the reading routine, because it may not exist yet.  Save it to an instance variable.  Set the text to this ivar in the windowControllerDidLoadNib: method.
  • I saved two icons, icon_128x128.png and docIcon_128x128.png, into my project.  In my <app name>-Info.plist I set the icon file to the first file name (including the .png), and under Document types, the Icon File Name to the second file name.  This associates those pictures with my app in some places (e.g. the about box, and within Xcode), but it does not ultimately set my app’s icon in the Finder.  How do I do this?
  • It was surprisingly hard to get the compiled program out of the Xcode environment and into a finished app that appears in the Finder on my Mac. The trick is to archive it, then press “Distribute…” (of course!).  You can choose to distribute it directly, and ad-hoc, and export as “Application”.