A Little Python Makes Dad Happy

My daughter, Jessica was born the Thursday before Father’s Day in 1992, the 3rd of September. That means this year her birthday occurs on Father’s Day.

That begs the question, what other years will this occur? Here’s some Python code that answers the question.

# Find all the years when my daughter's
# birthday and Father's day coincide
# from the year of her birth till 2100

import time

for i in range(1992, 2100):
	dt = "3 09 " + str(i)
	tt = time.strptime(dt, "%d %m %Y")
    	if tt.tm_wday == 6:
    		print i

I love you, Jessica. My favourite Father’s Day gift ever, always.


Writing A Web App With Python

I have spent my spare time in the past two weeks writing a web app. I couldn’t find a good web site that offered an easy personal journal. I wanted something that was a bit like a blog but with less fuss and totally private.

So I decided to try writing one. The language choice was obvious, Python. The next question was which framework to use. I did a web search and discovered that the choice quickly came down to two.

Django is the older giant in the room compared to Flask. The two also have a different philosophies. Django has it all built-in while Flask seems to be a thin wrapper over Werkzeug and Jinja2 mostly providing request and session handling while leaving almost everything else to extensions. Continue reading

Jupyter Releasing Some Nice Software

The Jupyter group have released an alpha version of a new Notebook environment called JupyterLab

JupyterLab is browser based, just like the old notebook system but adds a multiple pane environment. I’m not going to go into the details of the collaboration between the large number of organisations that have gone in to the development, go read the blog post announcing JupyterLab. Suffice to say that I’m glad such a high powered group are working on my favourite Python environment.

I installed the alpha (it’s quickly done with pip) and had a look. It’s an exciting looking development and will make a brilliant Python development environment.

At the moment it seems to be suffering from minor speed problems and minor layout problems in Safari (they are minor, don’t appear in Google Chrome and Safari is not currently listed as a supported browser so I’m not going to complain too loud.)

The built in editor can syntax colour Python. It even has colour themes for those, like me, who like a particular look in their editor. At the moment it is indenting only two characters with a tab (PEP 8 says it should be 4) and if you hit return with the cursor in column 1 then you get a first level indent on the next line.

These are the sort of problems you an expect in alpha software. I think I might install the current development version from Github and check there before filing a couple of bug reports. I’m a bit idiosyncratic, nothing I like more than spending an hour or two getting a bug down to it’s essentials and filing a report.

IPython 5

They have also released a new version of IPython they are calling IPython 5.0 LTS. It has some nice new features including syntax highlighting as you type and much better multi-line support. This is due to shifting from various command line interfaces to the purely Python readline replacement prompt_toolkit.

I think the move to prompt_toolkit is going to show major dividends as the library (currently at version 1.0.3) adds yet more functionality and that functionality moves into IPython. Jonathon Slenders, the author of the library, is also developing clones of Vim and tmux in pure Python using it and intends to fold features from those projects back in to prompt_toolkit.

They are designating this as “Long Term Support” as it will be the last IPython to run under Python 2. IPython 6 will require Python 3. Not is all lost though, they say they will continue to support Python 2 kernels with Jupyter Notebooks (and we assume the new Jupyter Lab). As they say in their announcement “For the 5.x series releases we are making an exception to that rule: until the end of 2017 the core team will do its best to provide fixes for critical bugs in the 5.x release series. Beyond that, we will deprioritise this work, but we will continue to accept pull requests from the community to fix bugs through 2018 and 2019, and make releases when necessary.” So it will be a while before us OS X users are forced to run Python 3 for IPython and break PyObjC and it’s brethren which are written in 2.7 (we can also hope that well before the 20202 deadline Apple moves to Python 3 and does the port of PyObjC.)

Easy Python Development

Taken together these two new releases improve Python development enormously for me. I have always been a fan of iterative development of my code in IPython and this just makes the explore and iterate method easier and easier.

Conda, Python 3 and Jupyter

So it seems Dr Drang has similar New Year’s Resolutions to me. He is shifting the way he codes and uses Python.

I’ve been using the Anaconda install of Python and IPython (now part of Jupyter) for quite a while but certainly wanted to move to using conda instead of pip and virtualenv to handle module installs and environments.

So I have now converted entirely to conda but the move to Python 3 is harder. I have a Python 3 environment installed on my Mac and do try and use it. The lack of PyObjC in Python 3 does slow me down in having it as the default however. Conda handles the combination of modules and virtual environment much better than the usual tools.

Using Jupyter notebooks instead of the IPython console is harder still. I tend to do my Python development as a real hack and the IPython console seems easier than the semi-permanence of cells in a Jupyter notebook. A notebook is a nice way of documenting and coding side by side. It’s growing on me as a way to work on my Python scripts.

If you do any serious work in Python then let me recommend Anaconda and Jupyter.


IPython Install Made Easy for Macintosh

Great news for those that want to run IPython on any platform. Continuum Analytics offers a marvellous tool (for free) called Anaconda that will install Python and iPython in one swift step.

Not only can you install all the requirements in one swift step but you can even install them in your home folder if you don’t have administrator access to your Mac.

The icing on the cake is that Anaconda also takes over the task of virtualenv, allowing you to build specific environments with different sets of libraries or a different version of Python. They even offer a package install tool and package repository, all checked to see they work properly with the current version of Anaconda.

Anaconda installs a huge list of packages along with Python and IPython, if you want to install just a few packages they offer a tool, miniconda, that installs only the tools and Python so you can pick and choose what else gets installed.

This is a highly recommended set of tools for Python and IPython development. They even have installs for Windows and one for Linux that packs the installer in a bash script so it can be installed anywhere (once again you can easily install it in your home directory.)

Hacking The Philips Hue

Philips hue mot vanlig lampa...

Philips Hue (Photo credit: Patrick Strandberg)

A short time ago I bought myself a Philips Hue starter pack and installed the three globes in my lounge room.

I must say that I love the way you can set the colour and brightness of these things. Having installed and played with the iPhone app it came to me that I should have a bit of a hack and see what I could do.

My first need was to find a way to turn the lights down as the evening gets late. I thought that would be a nice way to remind myself it was getting late and to think about going to bed.

I decided Python was the way to go since I can run it on both my server, the iPhone and iPad. I discovered a nicely usable library for Python and quickly wrote the required script. Then I just installed it as an item in the root crontab so now the lights get turned down to half power at 10:30 every night. The script required is tiny:


from phue import Bridge
b = Bridge('')
b.set_light([1,2,3], 'bri', 127, transitiontime=300)

Continue reading

Installing iPython On OS X Mountain Lion

ipython plot example

An iPython notebook showing a plot.


NOTE: There is now a much easier and powerful way to install IPython on the Mac. See my post, ‘IPython Install Made Easy for Macintosh’ for details.


I have to admit I really like Python. As languages go it has me impressed. If you take python and combine it with the power of a good shell you get iPython. I’ve found iPython extremely useful for system administration. Installing basic iPython is fairly easy, installing it so you get all its power is a little harder.

This install process assumes you have nothing yet installed on a clean system, you may have already installed some things.

To test this properly I started by creating a fresh install of Mountain Lion on an external drive.

For some of the installs you will need the Xcode command line tool chain. Install XCode then open “Preferences” and click on the “Downloads” tab. This allows you to install the XCode command line tools.

Now there is an argument about the various package managers for OS X. The three available are Fink, MacPorts and Homebrew. Personally I used to use Fink then MacPorts and I’ve now decided that Homebrew is the best of the bunch. Certainly Homebrew seem better at keeping their packages up to date and working.

Continue reading