A blog of Python-related topics and code.

Copying a directory tree with globbed files

To copy a directory tree with only the files matching a globbed *Unix style pathname pattern) expression:

Making a map out of dots

The code below generates stylised maps with land regions depicted by spaced markers (circles, polygons, etc). It requires the Python library, global-land-mask:

Two's complement and Python indexing

Integers are represented in computers as binary (base 2) numbers. If the integer is unsigned (i.e.) non-negative, then there is a straightforward representation as the sequence of bits (1 or 0) which comprise the binary number. For example, the number 13 is $1101_2$ ($1(2^3) + 1(2^2) + 0(2^1) + 1(2^0)$), or stored in 8 bits, 00001101. In such a representation there is no space to indicate whether the number is negative or positive and if a fixed number of bits is used, then overflow or underflow can occur in arithmetic operations that lead to results larger than can be represented in that fixed number of bits or less that zero. For example, the maximum number that can be represented in 8 bits is $255 = 11111111_2$. Adding one to this leads to the incorrect result $0 = 00000000_2$ since there is no room for the 9th bit required to represent $256=100000000_2$.

The Kapitska pendulum

The Kapitza pendulum is an inverted pendulum that can balance stably when driven by an oscillating vertical displacement. As with previous posts, the position of the pendulum bob with time can be described using Lagrangian mechanics. In a coordinate system with the pendulum anchor driven vertically around (0,0) and the y-axis pointing up, the components of the bob position and velocity as a function of time are:

How wet was Cambridge in February?

Just a quick (20 min) analysis of the rainfall in Cambridge in February since 1996 to see how wet it has been compared with previous years. Data are taken from the amateur-maintained weather station on the roof of the Cambridge University Computer Laboratory.