Sunday, November 15, 2015


Recently I've come back to application programming for OS X after a long time away.  I reviewed my notes on Swift from last year (on github here, not updated for Swift 2 yet).  (I was a little dismayed at how little I remembered, but it mostly came back after 8 or 10 hours).

I bought a copy of Hillegass et al. new edition of their great book to write Cocoa applications, which now uses Swift.  I haven't actually done that much with the book yet, though it was a good reference on setting up an Xcode project and hooking up a custom NSView.

For my first project I re-wrote my Color Sudoku program (post about the old one here).  At the top is a screenshot from the new one.  I notice there are similar things out there on the web now, I don't know if they were invented independently or what, but I built my first one in 2006.  In any event, it isn't suitable for mobile because of the tiny squares, so it could probably never be a commercial success.

I've been consistently amazed at how easy programming in Swift has been.  When there was a programming error it was easy to diagnose and fix.  I particularly love the ability to break out function definitions to a new file on a whim (no header files really helps here).  The fact that function names are visible in all the files of a project is a little scary, but it makes this kind of refactoring easy.

I put the new project up on github here, and also put a copy on Dropbox here.  I included a built version of the app in the project folder.  You will have to temporarily "allow apps downloaded from anywhere" or alternatively, install Xcode and build it yourself from the source.

I'm sure there are bugs.  Let me know if you find one.

Friday, March 6, 2015

Stuff on GitHub

This is my first post for a while.  I just wanted to let you know that I have been learning how to use git.  My github repo is here.

I have placed a number of "books" there which I have worked on in the last year including

MyCrypto (just beginning)

Also, there is a collection of writeups on more than 60 topics in mathematics.  The tex files are on github here and the pdfs are on Dropbox here (48 MB).

Tuesday, August 19, 2014

Swift talks to Objective C

I am back brushing up on Objective C and Cocoa, and now working on Swift.  It is supposed to be easy to integrate Swift code with an Objective C project, but I found it difficult.  So as I've done before, I've cooked up a working example that is as simple as it could possibly get.  I thought I would post a writeup on Dropbox.

It is here.  (It's a pdf printed from Sphinx html).  The original will be on github soon.  Good as long as Dropbox yet lives.

Monday, August 18, 2014

Still kicking

I have an online persona on another site.  Looks like this:

You can find the attribution here.

Anyway, that version of me shows a quote (maybe Descartes?):  "if I can still compute, I'm not dead yet!"  So, anyway I'm still computing and I'm not dead yet.  I have various projects on github which you can find under my username (telliott99), including a basic review for Java, a little bit of Crypto, and my old book on Python.

Wednesday, July 23, 2014

Simple server

I've been working on a new demonstration project that I'd like to tell you about.

When I want to run a bioinformatics script on a sequence file, I just go to the command line and do something like:

python infilename arg1 arg2 > results.txt

But suppose instead the scenario is that I have co-workers who just will not do this, and they pester me until I run an analysis for them.  I'd like to explore an alternative approach:  set up a simple web server that will guide the command-line-phobic user through the process of choosing a script and a sequence file and also entering the appropriate options for that script.  We can use html forms to do the work.

I've written a skeleton web server (using the Flask framework and the development server that comes with it) as a proof of concept.  It is not a web server in the usual sense and it's not actually exposed to the web.  It is designed to be run in the background on a single machine, with the permissions of the user, and can read and write files with those permissions.  The "server" could be configured to startup on Launch, if desired, so the user would never even be aware of that.

I've put what I have so far on github:

git clone git://

As currently configured, the python scripts don't actually do anything except read the options data and return a nice image.  However, the web pages are wired up together. The command line output shows that all the data is flowing through as desired.  Here is a screenshot of the index page:

What you would need to do to run the demo is to use pip to install the Flask framework.  Many people use virtualenv to set up something like this, and I did that for my first run through with this flask tutorial.

However, my setup is a separate python from the System python, installed with Homebrew, and I can easily replace it at any time.  I don't worry about the risks of experimentation, and I don't have so much stuff there that I worry about conflicts.  The new Python comes first in my $PATH:

> which python
> which pip
> pip install flask

I'm not actually using the WTForms in the current version of the project, but if you did want them you would need flask-wtf.  I did not install any of the other extensions for this project.  With flask available from python, and the project downloaded, just cd into the project directory and do

> ./
 * Running on
 * Restarting with reloader

Point your browser at localhost:5000 and you should see the index page.

The python code to render the form is simple:

The form itself is built from two templates, a base template that provides the header, and this:

The form data is routed back to the correct url/route/function to start the next steps (by "action=/prog_request").

The main issues are:  enforcing that the user provide a filename and make a choice of script, showing an options page with appropriate options for that script, and then dispatching correctly.  I'll have something to say about those in another post.

I am also just getting started with git, so for reference here is what I did to get this up on github.  I got a free account (and Homebrew installed git), and configured my editor.  That's important because the git commit step requires a commit message, and the default editor that comes up is vim, which I don't know how to use.  (Yes, I know "everyone" loves it).

git config --global core.editor TextMate

then from the scripter directory

git init
git add .
git commit
git remote add origin
git push -u origin master

github prompts for my credentials and then says the magic words which mean success.  One more task for the future:  setup ssh to do the push.

Wednesday, June 19, 2013

matplotlib followup for Python 3 on OS X 10.8

In my last post, I reported success in installing matplotlib on OS X. I realize now that this is not anything new to report, since the currently recommended install process (which I read belatedly today) in the README is to use Homebrew (or MacPorts), which is what I did. I'm just happy to know that it works.

I thought I'd say a word about Python 3 here.

I did

$ brew install python3 --framework

which first installed readline and sqlite (linked into /usr/local/opt), as well as gdbm and xz, and also Distribute and pip.

So now we have:

$ ls /usr/local/lib/python3.3/site-packages/
__pycache__    setuptools-0.6c11-py3.3.egg-info
distribute-0.6.45-py3.3.egg  setuptools.pth

$ python3
Python 3.3.2 (default, Jun 19 2013, 15:41:32) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import autotest
== CPython 3.3.2 (default, Jun 19 2013, 15:41:32) [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)]
==   Darwin-12.4.0-x86_64-i386-64bit little-endian
==   /usr/local/bin
Testing with flags: sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1)
[  1/372] test_grammar
[  2/372] test_opcodes
[  3/372] test_dict
[  4/372] test_builtin

The tests hang at this point, and I haven't figured out why yet. They do fine with my system Python. Then I did:

$ pip3 install numpy
$ pip3 install nose

since we need numpy for matplotlib (and don't get it for free as with System Python).

Following this advice, I did:

$ python3
>>> import numpy
>>> numpy.test('full')

Ran 4808 tests in 64.611s

<nose.result.TextTestResult run=4808 errors=0 failures=0>

Now to matplotlib

$ git clone git://
$ cd matplotlib
$ python3 build
$ sudo python3 install

(Grabs pyparsing, dateutil, tornado).

$ cd..
$ python3
>>> import matplotlib.pyplot as plt
>>> Y = [1,4,9,16]
>>> plt.scatter(range(len(Y)),Y,s=250,color='r')
<matplotlib.collections.PathCollection object at 0x1094e4650>
>>> plt.savefig('example.png')


$ pip3 install cython

had a permissions problem, so I did

$ sudo chmod -R 755 /usr/local/lib/python3.3/site-packages/
$ pip3 install cython

Now, for scipy

$ git clone git:// scipy
$ cd scipy
$ python3 build
$ sudo python3 install

$ cd ..
$ python3
>>> from scipy.stats import norm
>>> norm.cdf(2)

>>> import scipy
>>> scipy.test('full')

Ran 7486 tests in 725.812s

FAILED (KNOWNFAIL=42, SKIP=296, failures=82)

Some failures, but all in all it looks good, and a very easy install.

Tuesday, June 18, 2013

Matplotlib (and SciPy) on OS X Mountain Lion

Yesterday I installed matplotlib on my MacBook---for about the 10th time. This can be a complex undertaking, but a basic installation is relatively easy, so I thought I would outline it here.

Some things I did that made it easier:

• I used a clean install of OS X. I did this to make sure cruft (links, alternative versions of libraries from old installs) does not interfere. Also, this way I can be sure that things work for the reasons I have written about here.

To do the install I just made a second partition on my hard drive and installed OS X on it from a USB installer. If you've never done this before, good instructions for the USB part are here.

It's quite straightforward. Use the App store to update when the install is complete. I now have OS X 10.8.4.

Although it's not necessarily good practice, I used the same username and password for my accounts on OS X on both partitions, this allows me to read and write files in both accounts easily using the Finder.

• I used the System Python. This doesn't appear to be a very popular choice, but it avoids the headache of having multiple Pythons and various $PATH issues, etc. If you do install a second Python, make sure it's a framework version, since even saving a plot as a png using matplotlib will fail otherwise. I may do some more experiments later, but I think this is the right choice

My Python is

$ which python

(well, it's not really the whole of Python on OS X, but that is a whole 'nother story).

$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)

A bonus is that we already have numpy

>>> import numpy
>>> numpy.__version__

• I used Homebrew to get the most important matplotlib prerequisites, libpng and freetype. zlib is also a prerequisite but comes with OS X.

The instructions for Homebrew are here. Weirdly, I got a prompt for an admin password, which happened because the directory where Homebrew puts stuff, /usr/local, did not exist yet. So I bailed from the process and first did:

sudo mkdir /usr/local

Now, look for issues revealed by brew doctor and fix them if needed. Finally:

$ brew doctor
Your system is ready to brew.
$ brew update
Already up-to-date.
$ brew install pkgconfig libpng freetype
$ brew list
freetype libpng  pkg-config

the "Bottles" mentioned in the output I cut means these are pre-built.

I got pkgconfig because it helped with building/linking problems previously, but should not really be necessary when the libraries are in /usr/local. Not sure why it is some times pkg-config and other times pkgconfig, but OK:

$ ls /usr/local/bin/pkg-config 
$ ls /usr/local/lib/pkgconfig/
freetype2.pc libpng.pc libpng15.pc

(If you use e.g. the XQuartz versions of these two libraries, you can set the PKG_CONFIG_PATH environment variable to point to the correct directory, and pkg-config will do the rest).

Now we're ready for matplotlib.

$ git clone git://
$ cd matplotlib
$ python build
$ sudo python install

And test it:

cd ..
>>> import matplotlib.pyplot as plt
>>> Y = [1,4,9,16]
>>> plt.scatter(range(len(Y)),Y,s=250,color='r')
<matplotlib.collections.PathCollection object at 0x103963110>
>>> plt.savefig('example.png')


>>> import matplotlib
>>> matplotlib.test()
..KK.KK.....KK.KK..KK.KK.KK.KK.KK  etc.

I didn't test extensively but there don't seem to be massive failures. And matplotlib has lots of optional prerequisites I didn't install that would explain some failures.

It's worth pointing out that the matplotlib install process grabs several additional packages:

$ ls /Library/Python/2.7/site-packages

Let's get a few more things while we're at it:

$ sudo easy_install pip
Password: ****
Searching for pip
Best match: pip 1.3.1
Installing pip script to /usr/local/bin
Installing pip-2.7 script to /usr/local/bin

Come to think of it, SciPy would be a great addition. For that we need gfortran (Fortran compiler) and Cython:

$ brew install gfortran
(takes a while, several dependencies)..

Homebrew doesn't have Cython so:

$ sudo pip install cython

And now for SciPy:

$ git clone git:// scipy

$ cd scipy
$ python build
$ sudo python install

$ cd ..
$ python
>>> from scipy.stats import norm
>>> norm.cdf(2)
If you check for the libraries loaded (I used export DYLD_PRINT_LIBRARIES=1), you'll see we're using Accelerate.

Finally, get PyCogent too..

$ git clone git://
$ cd pycogent
$ python build
$ sudo python install

That was pretty easy!