Showing posts from February, 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 If you run:
$ python-config --ldflags the output is:
-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -ldl -framework CoreFoundati…

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…

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

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: