How to repeat n times the same command in vim?

Say you have a nice code and you want to visually separate each function implementation

#--------------------------
def func1():
    #somecode

#--------------------------
def func2():
    #someothercode

#--------------------------

Repeating n times the ‘-‘ is easy in VIM, just write one and then press (in normal mode) 30. for another 30 ‘-‘🙂 The dot command ‘.‘ repeats the last thing you’ve done since last time you entered in normal mode, and the ’30’ specifies that you want to repeat it 30 times.

Creating, resizing, moving etc. Windows in vim. Learn with vimcast

Windows are very useful in Vim, whether you want to look at different parts of the same file or edit multiple file in the same terminal window. As always there are tons of shortcuts to memorize without which one can be lost… This information is present in Vim’s documentation and in many many… many places in the internet, but I my favorite is the vimcast tutorial.

Vimcast is a nice website offering video tutorials for vim. If like me you’re a bit lazy to read tons of documentation, and above all you like see the thing in action, vimcast is great for that. The guy has also written a book named Practical Vim, which I like very much (yes I bought it). In the boo, each section represents a theme (like section 2 is « The normal mode ») which is organized as a series of associated tips for which the author gives very detailed examples… as if you were looking at his webcasts.

Calling a C function from python with numpy arrays as arguments is easy

Python is great, it is easy to code, flexible, dynamic, etc. but it has a downside: it is very slow. If, like me, you sometimes need to speed up a few functions, there are several options. One is to use Cython, which is kind of a C extention for python… their goal is to allow you to right python-like code (i.e. easy to right and high level) and easily obtain some serious (factor of several hundreds possible) speed up, up to C speed in some cases. If the function you have to speed up looks like its going to be a pain to write in C, Cython is the way to go!

That is what I’ve been doing over the last week… until I realized it could be a bit of a pain. Cython is not magic (so I was told several times I asked for help, thanks) and there’s no way one command line is going to give you blast speed. Instead, although it is easy to transform a naive python function into a Cython version, and easy to obtain reasonable speedup, if you’re looking for a real optimization, you’ll probably need to dig into the behind the scenes of Cython… understanding why the hell Cython transformed this line of python into 10 lines of C whereas this apparently-exactly-same-kind-of-line of python into 250 lines of C… this kind of stuff. And in the end, making your code very fast may be a pain… as compared to writing it in C in the first place!

That is why, depending on how much you feel your function will be easy to write in C, I’d seriously consider to do it, rather that going through the pain of optimizing cython code. I’ll now explain how to call C functions to you python code, and even C functions that work on numpy arrays, because I’m sure that’s probably what you’re up to.

This is the C file that contains the C functions you will call from your python module. I’ve made three examples, one function that just prints the basic hello world to the scren, the second takes a numpy array and prints it, and the third one copies one array into the other. If you know C, this is all pretty simple.

#include <stdlib.h>
#include <stdio.h>
/* basic function that just print some stuff */
void helloworld(void)
{

printf("Hello World!!\n");

}
/* this will print a given array */
void displaynparray(double *array, size_t s)
{

unsigned int i;

for (i = 0; i < s; i++)
 {
 printf("array[%d] = %lf\n",i,array[i]);
 }

}

&nbsp;

/* this will copy the content of 'in' into 'out' */
void copyarray(double *in, size_t s, double *out)
{
 unsigned int i;

for (i=0; i < s; i++)
 {
 out[i] = in[i];
 }
}

then you compile your code so to produce a shared library :

gcc -fPIC -shared -o module.so module.c

Now here is the python module that will make use of the library we’ve just done :


from numpy.ctypeslib import ndpointer
import ctypes
import numpy as np




def displaynparray(lib):
    array = np.arange(0,10,dtype=np.float64)
    disp  = lib.displaynparray
    disp.restype=None
    disp.argtypes = [ndpointer(ctypes.c_double), ctypes.c_size_t]
    disp(array,array.size)


def helloworld(lib):

    hw = lib.helloworld
    hw.restype = None
    hw()


def copyarray(lib):

    arr_in  = np.arange(0,10,dtype=np.float64)
    arr_out = np.zeros(10, dtype=np.float64)

    cpa = lib.copyarray
    cpa.restype = None
    cpa.argtypes = [ndpointer(ctypes.c_double),
                    ctypes.c_size_t,
                    ndpointer(ctypes.c_double)]

    cpa(arr_in, arr_in.size, arr_out)

    print arr_in
    print arr_out



def main():

    lib = ctypes.cdll.LoadLibrary('./module.so')

    print 'function helloworld() : '
    helloworld(lib)

    print 'function displaynparray()'
    displaynparray(lib)

    print 'function copyarray'
    copyarray(lib)


if __name__ == '__main__':
    main()

here is what I get in my terminal when I execute the script :

python module.py
function helloworld() :
Hello World!!
function displaynparray()
array[0] = 0.000000
array[1] = 1.000000
array[2] = 2.000000
array[3] = 3.000000
array[4] = 4.000000
array[5] = 5.000000
array[6] = 6.000000
array[7] = 7.000000
array[8] = 8.000000
array[9] = 9.000000
function copyarray
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]

How do I set the name of a git branch in my terminal prompt?

I’ve been using Git for the last couple of weeks and so far I find it awsome. For those of you who don’t know what Git is, I’ll assume you’re clever enough to find out yourself, there are tons of articles on git, git tutorials and so on on the net, I won’t add mine.

If you’re a (new?) git user however, this might interest you : you’ve probably noticed that sometimes you don’t remember on which branch you are exactly, so you often have to type ‘git branch’ to remind you. This is quite boring and I found an excellent way to always know where you are : write the branch name within your terminal prompt!

So to do that, just add the following into your .profile, .bashrc or whatever shell config file you use :

git_prompt ()
{
    if ! git rev-parse --git-dir > /dev/null 2>&1; then
        return 0
    fi
  
    git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
  
    echo " [$git_branch]"
}
PROMPT_COMMAND='PS1="\u@\h$(git_prompt)\$ "'

that’s all folks… and don’t forget to source your file😉

C99 variadic macros and useful print for debugging your code

Debugging is one of the worst thing I have to do on a regular basis… the debugger is a great tool but it is often fastidious to set breakpoints and print variables step by step (in particular if you are working on parallel programs)….

Printing stuff here and there is also useful but then my code is a mess with all these if(debug) or #ifdef DEBUG everywhere (note that the second method do not take runtime if the DEBUG preprocessor flag is set). So I’ve been looking for a way to print useful debug information but without taking time when a debug flag is not set at the compilation and without making my code looking too much like 99% is debug stuff and 1% the actual code.

I found an elegant, powerfull yet very simple method to do that, with C99. Come on, it’s been more than 10 years that C99 is out, so it’s time to accept the -std=c99 flag in your gcc compilation😉. The method is based on variadic macros, which are macros taking a variable number of arguments. Check the following :

#include <stdlib.h>

#ifdef DEBUG_PRINT

#define debug_printf(fmt, ... ) \
              printf("DEBUG > at %s (%d) : " fmt,__FILE__, __LINE__,__VA_ARGS__);
#else
#define debug_printf(fmt, ...)
#endif

Rather than a long explanation, I think a small example of what this piece of code does is better. So of the flag DEBUG_PRINT is defined this :

debug_printf("Your program has %d files and an average of %d bugs per line\n",100,5);

will be expanded in :

printf("DEBUG > at %s (%d) : Your program has %d files and an average of %d bugs per line\n",__FILE__,__LINE__,100,5);

and print the following on screen :

DEBUG > at myfile.c (12) : You program has 100 files and an average of 5 bugs per line

assuming it has been written in myfile.c at line 12.

However, if DEBUG_PRINT is not defined, then your debug_printf() command will not even appear in your compiled code!

Learn Python – Switch from IDL to Python

If you’re like me, a scientist using expensive software like IDL, Matlab etc. to make your scientific data analysis, you might want to look at free alternatives. One issue however is that often, those alternative are either incomplete or with a limited community or both. Well you may want to consider Python. With Numpy and Matplotlib, Python now becomes a very powerful tool for scientific programming and now seems to be attracting more and more people. In the astrophysics community, a lot of people are switching from their old programming habits to Python. Moreover, the fact that Python is free is an attractive argument for poor university teachers who are not yet hopeless to teach scientific computing/data analysis to students. Because of that, one might expect that within the next decade, young scientists will be much closer from Python than researchers are now.

If you want to switch, there are many tutorials on the internet, but it can be quite long and fastidious to find the good ones and go through them. A good alternative, if you don’t have an expert who as nothing else to do just next to you, is to see what people who have already switched have done. Some people are actually nice enough to share their experience with us. Check this link for good advices.

Display the full path in Max OS X finder

Mac OS X only display the current folder name in the finder title bar, which is quite annoying when you often want to know the full path. It is actually easy to make the finder to display the full path, but one has to know the command, which is :

defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES

One entered in the terminal, force the finder to relaunch, and the full path of the directory you’re in will be displayed in the title bar. (Source of the trick)