Python Bits and Pieces

This page contains some ‘quick and dirty’ Python code which I have frequently found useful. Everything here is released under the GNU GPL and should be used at your own risk.

Implementing Excel worksheet functions in Python

The following example shows how to implement some statistical functions in Python, make them available via a COM server, and call them from Excel. The example uses Numerical Python.
  1. Run TestCOMServer.py to register the COM server.
  2. TestServer.xls contains the VBA code that makes the functions accessible from Excel.
[Note for the clever: It would have been more efficient to implement the COM server as an inproc server, but unfortunately the LAPACK library used in Numerical Python does not appear to be thread-safe; see the technical notes on the spreadsheet.]

A simple way to access the Windows clipboard from Python

For text or arrays of numbers, clipboard.py works fine; note that you need to install Sam Rushing’s calldll module. For more general solutions, see the Win32 Extensions or the wxPython framework.

Bootstrapping

The most naive possible algorithm to compute bootstrap confidence intervals may be found in bootstrap.py.

Root finding, minimization and robust nonlinear regression

Root finding and minimization are not part of the standard library, so I implemented them in mathutil.py.

As an example of their use, regress.py is a nonlinear regression routine which lets you use a general M-estimate (i.e. you can assume any probability distribution for the errors; least squares corresponds to assuming normally distributed errors). This routine is not thread-safe as it stands, but it can easily be modified to be thread-safe.

Simulated annealing

The sample file anneal.py contains a simple implementation of a simulated annealing algorithm for discrete optimization; you’ll need to modify it for your own purposes.

The sample file LJ.py illustrates the LJ algorithm, which has a similar flavor. Here it is used to solve an integer minimization problem that arises when carrying out a regression on time series with a structural break. (Note that regress.py is required.)

Tweaks to DCOracle

Users of Digital Creations' DCOracle package may find this useful: a slightly hacked version of dbi.py that contains __cmp__ and __hash__ methods (and a __float__ method that gets around the year 2038 problem, on affected platforms).