How to Use uShuffle in Python

Writing a wrapper module

Download the file ushufflemodule.c, which contains the wrapper code for the three functions ushuffle, ushuffle1, and ushuffle2. Each wrapper function converts the parameters from a Python object to C data types using PyArg_ParseTuple, calls the corresponding C function, and converts the returned value back using Py_BuildValue.

Next download the two files ushuffle.c and ushuffle.h. Make two simple changes to ushuffle.c as follows. First, change the line

static const char *s_ = NULL;


static char *s_ = NULL;

Second, change the beginning of the function ushuffle1 from

void shuffle1(const char *s, int l, int k) {
	int i, j, n_lets;

	s_ = s;
	l_ = l;


void shuffle1(const char *s, int l, int k) {
	int i, j, n_lets;
	if (s_)
		free (s_);
	if ((s_ = malloc(l * sizeof(char))) == NULL) {
		fprintf(stderr, "shuffle1: malloc failed\n");
	strncpy(s_, s, l);
	l_ = l;

Building the module

Build a shared library with the following commands:

$  gcc -c ushuffle.c ushufflemodule.c
$  gcc -shared ushuffle.o ushufflemodule.o -o

Depending on the configuration of Python on your platform, you may need to pass additional options to the first gcc command. For example, if the header file Python.h is in the directory /usr/include/python2.5/, you need to add the option -I/user/include/python2.5. On some platforms you may also need the options -g -fpic.

Testing the module

>>> import ushuffle
>>> ushuffle.shuffle('aabaa', 5, 2)
>>> ushuffle.shuffle2()

>>> ushuffle.shuffle2()
>>> ushuffle.shuffle1('bbbbgb', 6, 3)
>>> ushuffle.shuffle2()

An alternative way for building and installing the module

Bartek Wilczynski from EMBL suggested that, with his setup file, the building and installation process on MacOSX 10.5.2 is as easy as the following command:

$ python install

Minghui has tested and confirmed this on MacOSX 10.4.11. Thanks to Bartek!