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 25, 2010

Science tries to give us a better understanding of our reality. A better understanding, or model of our reality allows us to make more accurate predictions about what will happen next. Having a good model of what will happen next allows us to benefit by making decisions which will result in increased happiness and avoiding decisions which will result in decreased happiness. The way that science works begins with someone using their imagination to make a hypothesis. A hypothesis is an untested idea. A hypothesis is by definition incorrect much of the time, and unproven all of the time. It is neccessary to have hypotheses in order to have science.

An athiest believes that religious people have a model of reality which will not serve them well in making predictions. That means that they make suboptimal decisions which make their lives, and often the lives of others, worse. Therefore it is optimal for an athiest to try and encourage people in the world towards a scientific way of thinking.

I do not believe that it is constructive or optimal to antagonise people who have an incorrect model of the universe. I think, for example, that publishing cartoons which offend an incorrect person is suboptimal when it comes to the goal of enlightening them. I think a much better strategy is to give them as much information as possible so that they can arrive at their own conclusions based on the available data, and disprove their own existing incorrect hypotheses. I think that it is optimal, constructive, and safer to respect all people, including those who you think are wrong.

I think that education is a better strategy than disrespect and mockery.

It's probably not possible for a human to have a 100% accurate model of reality (otherwise that model would be reality itself). This means that each of us has a flawed model of reality. All of us are incorrect people, hence all of us can only make predictions with limited accuracy, and importantly, we require other people to build a consensus model that is more correct than the one we might individually hold.

Jedi Squirrels with Light Sabers

To summarise, we should tolerate people who are wrong because:

  • It is neccessary to be wrong in order to form hypotheses and progress science. (It is neccessary to be wrong before you can be right.)
  • Antagonising people who are wrong does not help us all make better models about reality, as much as educating them, and it often has an adverse effect of making us less safe.
  • We are all mostly wrong, and probably always will be.

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.