Setting up Django and SQL Server on Linux

Thu Sep 26, 2013 by brett

This post is about how to get Django on a Linux box connected to SQL Server. It took me quite a while to get working, so I’m documenting what needs to be done to save other people time.

Installing the right packages

I installed unixODBC-devel, python-devel and freetds via yum. I then install pyodbc with pip, and django-pyodbc from Github. I got my django-pyodbc package from [Lionheart’s GitHub Repo](

odbcinst.ini file

In order to access the database, you will need the following entries in the odbcinst.ini file:


Checking that you can access the Database

You can use the following command to check that you can see the database

tsql -H hostname -p port -U username -P password

Getting the Django file right

Using Lionheart’s Github django-pyodbc package, you need to set the Django database *ENGINE* parameter to **“django_pyodbc”**. The *NAME* setting is set to the database name. Set *HOST* to be **“hostname,port”**. Finally, a very important setting is the *OPTIONS* value. This should be set to:

'OPTIONS' : { 'host_is_server' : True, 'extra_params' : 'TDS_VERSION=8.0', }

The *host_is_server* option tells the django_pyodbc layer to treat the hostname as an actual hostname, rather than a DSN. The *extra_params* option tells the FreeTDS layer to use TDS protocol version 8, which is important otherwise you’ll get Unicode errors when you try and communicate with the database.

Using my phone as desktop

Wed Sep 25, 2013 by brett

I am experimenting at the moment with using my phone as my primary computing device. I think that this is the future. There are phones coming out (hello iPhone 5s) with remarkably powerful processors. We can connect our phones to a bluetooth keyboard at the moment. The next step is to be able to send the display through to a nearby screen.

The good news is that there should be an exponential increase in the need for iOS and Android programmers.

Markov Chain Monte Carlo as Frogger

Thu Jul 4, 2013 by brett

I read a really interesting post on Reddit explaining Markov Chain Monte Carlo, which drew the analogy between MCMC and a long game of Frogger.

Frogger is a game where you play a frog trying to cross a road by jumping. Imagine that you keep track of each jump from place to place. If the frog dies at a position, you know the position is dangerous. Now imagine repeating a whole series of trials and working out the probability distributions of each positions by aggregating the trials.

It’s a neat analogy!

Shaping Experience

Thu Jun 27, 2013 by brett

I had heard a while ago that the ideal holiday was one in which you had a fairly bad start to it, but with a peak experience close to the end of the holiday. The rationale is that we tend to base our judgement of the holiday on the range of our trough-to-peak experiences. The larger the spread between the trough and the peak, with the peak occurring towards the end of the experience, the better we perceive the experience to be.

I couldn’t remember where this information came from, but today I was reading the Wikipedia article on the book “Thinking, Fast and Slow”, and it refers to this result.

I’ve been thinking about how we can use this shaping of our recollections to improve our behaviours. One of the things I’m trying to improve is my use of the adaptive time-boxing of tasks. I set the length of time for the time-box based on the amount of energy and focus I currently have. During periods where I am lacking mental energy, I allocate very short amounts of time for my time-boxes - even as little as 5 minutes. An improvement to my time-boxing regime might be to do something like spending the last few minutes of the allocated time doing something like listening to a favourite piece of music while I work. The idea being to try and shape the experience to being something more enjoyable - to create an anchor to make it more likely to keep doing time-boxes.

Surviving Post-Scarcity

Fri Jun 21, 2013 by brett

I’ve been reading the “Beyond Scarcity” series on FTAlphaville recently, and it’s made some very interesting points. The posts argue that the current economic environment is deflationary with regard to goods. I think that is true, and one of the reasons is because of technology. Firstly technology is constantly making everything more efficient and because of global competition this is both reducing the production costs and making goods cheaper. Secondly technology is causing structural unemployment, which means less people have money to spend and there is less money flowing around the economy. Other factors causing deflation are the tight monetary conditions, the aging population, and potentially the effects of quantitative easing.

I think there are also a few things that are inflationary. Firstly the price of food is going up, and because of climate change and increases in the World’s population I think this will continue to go up. Secondly the cost of accommodation in major cities is going up. In London this is partly due to housing being seen as a way for Europeans to move their wealth out of Europe. The world-wide trend I think is caused by big cities being more desirable because of jobs and the efficiency of living in them. Thirdly, the risk premium associated with all kinds of assets is increasing. This is due to the increased volatility of financial markets as well as external factors such as climate change and political risks.

In a deflationary environment “Cash is King” and debtors are punished because the money they have to pay back is worth a lot more in the future. In an inflationary environment you don’t want to hold cash as it is worth much less in the future.

The question is how to structure your life to make the most of the deflation of goods, and also handle the potentially increasing costs of food, accommodation, health-care and various other risks.

My current thinking is that a basket of high-dividend-yield stocks in fairly solid companies is the way to go. With some cash in the bank for emergencies. I want to try and achieve other passive income streams - for example; building mobile apps and blogging. I think that a “Frugal Maker” society will begin to become dominant, where people try and reduce their living expenses as much as possible. Building and repairing their own stuff where they can. I definitely think that our society is at a point of rapid evolution, and we all need to stay on top of the structural changes in order to maximise our lives.

Some thoughts on getting older

Sun Jun 16, 2013 by brett

So I was thinking about being older today, and I saw this Louis CK clip about how older people are smarter - “Older people are smarter, and if you get into an argument with someone who’s older, you should listen. It doesn’t mean they’re always right; but even if they’re wrong, their wrongness is rooted in more experience.”

I was thinking that he is both right and wrong about this. Some people are smarter by virtue of having more experience. Others have lived essentially the same day over and over for 50 years. I think what counts is both the volatility of your experience, and the learning you’ve taken from those experiences.

Making Space in Time

Fri Jun 14, 2013 by brett

Sometimes I think the best way to get things done is just to allocate space in the day in order to achieve them. For example; I find it hard to write blog posts. Left to my own devices, my blog would resemble a desolate wasteland. But all I need to do is allocate 10 minutes out of my day in order to write something, and I can get something written that I can upload to my blog.

This is the beauty of time-boxing, of the Pomodoro technique: It forces you to allocate a fixed section of time in which to achieve something. If you just make a space in time it’s amazing what you can do.

Alzheimer Disease and Memory Palaces

Wed Jun 12, 2013 by brett

I just watched a very interesting TED talk given by Kasper Bormans about his PhD research into using the Method of Loci (or Memory Palace technique) to help Alzheimer patients retain their memories of other people for longer.

I liked the way he is using virtual reality to create the “Memory Palace”. In effect he is using software to help improve the functioning of wetware.

Creating Flashcards

Mon Jun 3, 2013 by brett

I’ve been studying for another Maths exam. This time it’s the Open University M343 “Applications of Probability” course. It’s exam time so I’ve been making flash-cards to study with.

My current process for making the flashcards is to open the PDF course notes, run my Flashcard Creator program, and copy and paste the PDF exercises / solutions into Flashcard Creator, which then saves the PDF regions to a directory.

I open and transfer to the directory with all the PDF regions of exercises / solutions, and run a script that calls my PdfRectToPng program.

The PdfRectToPng program converts the PDF files to PNG files and also trims them down, so they aren’t A4-sized.

Finally I run the following script on the directory that creates the Anki text import file.

import os, sys, glob

class Problem:
    def __init__(self, key):
        self.key = key
        self.questions = []
        self.answers = []

if len(sys.argv) != 2 or not os.path.isdir(sys.argv[1]):
    print("Usage: %s <image_dir>" % (sys.argv[0], ))

problems = {}

image_dir = sys.argv[1]
files = glob.glob(os.path.join(image_dir, '*.png'))
for filename in files:
    bn = os.path.basename(filename)
    probkey = bn[0:11]
    qa_text = bn[12:13]

    if not probkey in problems:
        problems[probkey] = Problem(probkey)
    prob = problems[probkey]
    if qa_text == 'q':

probkeys = problems.keys()
for probkey in probkeys:
    prob = problems[probkey]

    sectnum = probkey[6:8]
    qnum = probkey[9:11]

    #sys.stdout.write("<h2>Section %s Question %s</h2>\n" % (sectnum,     qnum))

    for q in prob.questions:
        sys.stdout.write('<img src="%s" />' % (q, ))
    for a in prob.answers:
        sys.stdout.write('<img src="%s" />' % (a, ))

Productivity for 2013-05-31

Fri May 31, 2013 by brett

Not a terribly productive day today. I alternated between studying for my Open University M343 statistics exam and working on Judga Property - my latest iOS app. I also went to the gym, which is that big break in the middle of the day.