Feb 15, 2012

Swig compiling issues Mac Os Lion

One problem that happened to me, was the fact that working under a virtualenv with an specific python version as one can expect, works with ONLY that python version.
So if you try to import a module compiled with one version different with the one you are running, will no work.
The exception message:

Fatal Python error: Interpreter not initialized (version mismatch?)
Abort trap: 6

How we can solve this issue?
First, knowing which python version are we running and of course, compile the module to THAT version.
Below are some examples of how to compile a module to 2.6 or 2.7 (which is Mac Os Lion python default version)

To use python default version just type the following:

$ swig -python example.i
$ cc -c `python-config --cflags` example.c example_wrap.c
$ cc -bundle `python-config --ldflags` example.o example_wrap.o -o _example.so

If you run:

$ python-config --ldflags

the output is:

-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -ldl -framework CoreFoundation -lpython2.7 -u _PyMac_Error /System/Library/Frameworks/Python.framework/Versions/2.7/Python
$ python-config --cflags
-I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64

In case you want you use a different version of python (let's say 2.6) do the following:

$ swig -python example.i
$ cc -c `python2.6-config --cflags` example.c example_wrap.c
$ cc -bundle `python2.6-config --ldflags` example.o example_wrap.o -o _example.so

If you run

$ python2.6-config --ldflags

the output is:

-L/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config -ldl -framework CoreFoundation -lpython2.6

$ python2.6-config --cflags
-I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 

To see other installed python versions:

$ ls /usr/bin/

Feb 11, 2012

Swig Tutorial example Mac Os Lion

This tutorial is based on Leopard Tutorial but still works, thou some warnings.

First, we create an example.c file with this content:

/* File : example.c */

#include <time.h>
double My_variable = 10.0;

int fact(int n) {
    if (n <= 1) return 1;
    else return n*fact(n-1);
}

int my_mod(int x, int y) {
    return (x%y);
}

char *get_time()
{
    time_t ltime;
    time(&ltime);
    return ctime(&ltime);
}

Second, we create the interface between C and Python with the following file:

/* example.i */
%module example
%{
    /* Put header files here or function declarations like below */
    extern double My_variable;
    extern int fact(int n);
    extern int my_mod(int x, int y);
    extern char *get_time();
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();

Now that we have all the source files we need, we compile them in this way:

$ swig -python example.i
$ cc -c `python-config --cflags` example.c example_wrap.c
$ cc -bundle `python-config --ldflags` example.o example_wrap.o -o _example.so

if you have problems with this step check this other post

So, everything looks nice, but how do we use the dynamic module in python?

>>> import example
>>> example
<module 'example' from 'example.pyc'>
>>> example.cvar.My_variable
10.0
>>> example.my_mod(20,10)
0
>>> example.fact(14)
1278945280
>>> example.get_time()
'Sat Feb 11 19:27:05 2012\n'

disclaimer: this is just the tutorial from the swig's homepage, it only shows how you can actually use some C power into python. For example, scipy package, in the module cluster uses a C function wrapped into python code.

Feb 10, 2012

timeit example

An example of how to time a statement

>>> import numpy as np
>>> arr1 = np.random.randint(0,1600,(3000,3000))
>>> timeit arr1.argmin()
10 loops, best of 3: 34.2 ms per loop

Tested under Mac Lion Os proc i7 and Python 2.7.1 r271:86832

Feb 7, 2012

What's the iPad resolution?

iPad has a 1024 by 768 resolution.
Just that.

Escaping Charecters in python

Just an example commonly used
Escaping the "\" with another "\"

>>> with open('C:\\Folder1\\Folder2\\file') as file_handler:
Or just add an "r" before the path (this stands for "raw").

>>> with open(r'C:\Folder1\Folder2\file') as file_handler: