June 12, 2010

If you want to have some Python libraries installed in your system somewhere other than the standard place ("site-packages"), here's how to do it. This can be useful if you don't want them to interfere with your operating-system installed path, or if you don't want to become root/admin in order to install them.

This problem is solved by things like virtualenv and buildout, but I find that this will suffice for a lot of cases, or if you don't want to learn the ins and outs of those systems.

First, create a directory somewhere for your alternative Python libraries to go into:

$ mkdir ~/my-python-libraries/

You only have to do that step once.

Next, tell future Pythons that we have an alternative path where libraries live by setting an environment variable:

$ export PYTHONPATH=~/my-python-libraries/

This means that Python will check that directory for Python libraries when you try to import them. If you are always using that library path, you could put it in your .profile or .bashrc file so that it is set on login.

Now when you install new libraries, make sure they go into that alternative path:

$ cd some-library
$ python setup.py install --prefix=$PYTHONPATH --install-purelib=$PYTHONPATH --install-platlib=$PYTHONPATH --install-scripts=$PYTHONPATH/bin --install-data=$PYTHONPATH

One good thing to do is make a handy little one-line script which sets the PYTHONPATH variable when you want to start working on a project which needs those libraries. You could put it in ~/bin or in your project's root or scripts/ subdirectory.

You can also make a script in your ~/bin directory which runs the python setup.py command with those command line arguments set and call it something like "install-python-library".

June 3, 2010

I had a hard time finding where I had written this down, and I will almost certainly need it again.

update: More power-of-two fun at Frank's blog with Fast power-of-two modulo.

To find the nearest power of two (linearly) to a given number:

pow(2, int(log(n, 2) + 0.5))

To find the next highest power of two:

int(pow(2, ceil(log(n, 2))))

That's Python code, but pretty much applies in any language. There are some very fast log2 implementations out there if you need them.

Also, if you only have access to log10 in your programming environment (as I did when I worked that out), you might need this:

log2(n) = log10(n) / log10(2)

This is expressed generally as:

logX(n) = logY(n) / logY(X)

(Those two aren't Python code).

May 30, 2010

This 3d printing stuff is addictive.

May 18, 2010

PyCon AU Logo

There is a new Python conference running this year for the first time. It's in Sydney, Australia, at the end of June. I'll be speaking, and I'm very excited about attending too; there are a number of high quality topics evident in the conference schedule. I'm particularly happy not to have a clashing time slot with my friend and occasional colleague, Simon Wittber, as his talk looks fascinating. If you are into Python, you should go!

Go to the PyCon AU 2010 website for more details.

May 17, 2010

With the recent news that Android phones are outselling phones running the iPhone OS in the USA in the first quater of 2010, I wanted to write down something that I have been ranting at my more patient friends for the last few months. The open development platform style of Android is like evolution, whilst the closed Apple platforms are more like intelligent design.

  • Apple periodically come out with a single near-perfect product.
  • Android products get incrementally better with each iteration from each phone company.

  • Apple must make every product a hit - they can't afford failures like Apple TV. Failures are very expensive for Apple.

  • Android allows Google to outsource failure. Infact, failures by the 3rd party companies who make Android phones actually help the evolution of other Android products as all products in that class can learn and benefit from the mistakes of eachother. This is like the transfer of genetic information. Failing phones are part of the survival of the fittest style of interaction that occurs in nature.

  • Like the traditional Mac platform strategy, the iPhone strategy is about elite, expensive, "cool" devices with fantastic usability.

  • The Android strategy is more like the PC platform of years past. It will result in a wide choice of generally uglier phones that don't work as well as the iPhone, but that slowly get better and better. Everyone will use them anyway, much to the confusion of die-hard Apple fans. This will be because market pressures will ensure that the cheapest phones of the highest quality, which give the user the most control and choice, will be the most popular. Often the most successful new designs will be copied from market leaders like Apple and other Android makers.

Evolution is dirty, ugly, and messy, but the end result is always more successful in a competitive ecosystem. It's interesting to consider which strategy will be more robust in the smartphone market.