Computing bits — 1

Hello World,

As it says on the tin, these posts will be about tips and tricks I'll gather this and there about computing. If they were (are) useful to me, they're likely to be helpful for someone else. So I'll put them here so everybody (including me) can find them when necessary. Be warned : I'm in team (Gnu/)Linux and I follow the Curch of Vim after having spent a few years in the Church of Emacs (so I'm happy with both).
As a scientist I also use Python to read and process SPICAV data. And the cloud model I use is in FORTRAN which is a necessary pain to me. And when it comes to writing something (reports, papers, posters and slides) I'm most definitely in the team LaTeX.

Luckily enough this matches perfectly with my PhD advisor's habits. Luckily because, as to quote him : “[He] would have made [me] change”. Pfiuu!

Vim + Python = PROFIT


Vim is a great text editor, whose only decent competitor is Emacs (but Emacs is almost an OS). I already had my vim set for LaTeX with the latex-mode but I was looking for something equivalent for Python. And of course, there is one.

python-mode has several nice features such as autocompletion, syntax checking, warnings, block folding and is PEP8 compliant. I haven't been though all it's potential, but it will probably save me time! For short: it is worth trying.

Oh and by the way, I had some issues with the space/tab indentation : PEP8 recommends the use of 4 spaces instead  of tabulations, and my code was sometimes mixing both which is bad. Python-mode yelled at me for it but I didn't know how to fix that nicely. Again, vim is very helpful: an appropriate setting in your .vimrc and the following trick replaced nicely all tabs by spaces and makes sure that all indentations will always be set by spaces and not tabs. All is explained here.

Where is that variable used ?


As mentioned above, I have a code in Fortran. To be fair it is great and works well but I'm not yet fully familiar with it. So when I found myself looking for the first appearance of a specific variable in all the subroutines spread across several files in different folders and without a knowledge of the global structure of the code, I was kind of screwed.

Punched card
Where FORTRAN should have stayed.

But there comes the almighty command line (and Google too) ! I knew that grep was a command to search a string in the output of another command using a pipe for example. But I didn't know that it could read in all the files you want and recursively (which was exactly what I needed)!

grep -rnw 'directory' -e "pattern" -n is line number and -w stands match the whole word. Along with these, --exclude or --include parameter could be used for efficient searching. Something like:

grep --include={*.c,*.h} -rnw 'directory' -e "pattern"

will only search through the files which have .c or .h extensions. Similarly a sample use of  --exclude

grep --exclude=*.o -rnw 'directory' -e "pattern"

will exclude searching all the files ending with .o extension. And using the option -l gives just the file name.

For more, and as usual : man grep !

Commentaires

Posts les plus consultés de ce blog

Pourquoi les doctorants devraient être sur les réseaux sociaux.

Scienca vojaĝo en Danujo

[Video en esperanto] Vi pagas sciencon kvarfoje !