Installing repositories under Centos 6.2 to get ipython, R and other packages

I installed Centos 6.2 and tried to install some packages such as ipython or R but they were not available like many other useful packages that are available by default in other distribution such as Fedora.

The solution is to go to this website:

Fedora wiki EPEL

Go to section “how can I use these packages” and click on epel-release-6.5.noarch.rpm for your system (either 86 or x64)

you should be prompted for a password to install the RPM on your system. Once installed, you should be able to install the missing packages.

Posted in Linux | Tagged , | 1 Comment

SED: replace a pattern in all files in one command

I have a bunch of c files and I want to change the first line that appears as


//something

into

/*something

Using sed, you can use the command ‘s/old_pattern/new_pattern/g’ where -e stands for edition, and -i to apply the changes. If you just want to see what would be the outcome, do not use the -i option.


sed -i -e 's/\/\/$Id/\/*$Id/g' *.c

Posted in Linux | Tagged | Leave a comment

crontab usage

Under Linux/Unix, tasks can be run automatically at a given time and regular intervals. Those tasks are store within the crontab (CRON TABle) that is a file which contains the schedule of cron tasks.

Crontab Restrictions

To prevent a user to use crontab, add its name in /etc/cron/cron.deny
To allow a user to use crontab, add its name in /usr/lib/cron/cron.allow.

If only cron.deny exists and is empty, all users can use crontab.
If neither file exists, only the root user can use crontab.

Crontab Commands

First, you need to specify an editor. For instance, to use vim:


export EDITOR=vim

crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

Crontab file

When using crontab -e, you then need to enter a valid command.

A crontab file has six fields. It looks like:


* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have be:

  • The star symbol *
  • A list of elements separated by commas where an element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

Crontab Example

A line in crontab file like below remove the content of temporary directories each day at 6:30 PM.


30 18 * * * rm -f /tmp/tmp*

A line in crontab file like below remove the content of temporary directories on the 1st of Jan, June and December at 00:30 PM.


30 0 1 1,6,12 * rm -f /tmp/tmp*

A line in crontab file like below remove the content of temporary directories on the 10th of each month and every Monday at 12.05 and 12.10


5,10 12 10 * 1 rm -f /tmp/tmp*

mailing

Add this to the top of the crontab:

MAILTO="yourname@yourdomain.com"

Posted in Linux | Tagged | Leave a comment

SVN properties

SVN allows to store some useful keywords which can be set, the value of which will be substituted for ‘tags’ in the file itself.

For instance, if the Id keyword is set, place this line in your code

__revision__ = "$Id: gendoc.py 2252 2010-02-08 17:43:10Z username $"

The string between $Id: and $ is going to be automatically replaced in the future by the proper Date, SVN revision and author. This is especially useful when you want to associate a revision with an executable.

Yet, most of the time the properties are not set. One way to do it is manually:

svn propset svn:keywords "Author Date Id Rev URL"  example.py

If you already have a bunch of files, just use a linux tools such as find:

find . -name "*py" | xargs svn propset svn:keywords "Author Date Id Rev URL"

Finally, if you create a file, again you have to set the properties. Hopefully, there is a configuration options that will automatically set the properties to the new added/commited files. Change the configuration file in ~/.subversion/config:

enable-auto-props = yes
[auto-props]
*.c = svn:keywords=Author Date Id Rev URL;svn:eol-style=native
*.h = svn:keywords=Author Date Id Rev URL;svn:eol-style=native
*.cc = svn:keywords=Author Date Id Rev URL;svn:eol-style=native
*.cpp = svn:keywords=Author Date Id Rev URL;svn:eol-style=native
*.py = svn:keywords=Author Date Id Rev URL;svn:eol-style=native
Posted in Computer Science, Linux | Tagged , | 5 Comments

python: how to flatten a nested list

Simple nested list of strings

Suppose that you have a simple nested list. Simple in the sense that there is only one level:

data = [['a','b'], ['c'], ['d']]

you can use the operator module and reduce built-in function as follows

import operator
reduce(operator.add, data)
['a', 'b', 'c', 'd']

A more generic version would use list comprehension:

flat_list = [item for sublist in data for item in sublist]

this is equivalent (but faster) shortcut for:

flat_list = []
for sublist in data:
    for item in sublist:
        flat_list.append(item)
Posted in Python | Tagged | Leave a comment

ssh on remote host without typing the password

When you connect to a remote host with SSH, you need to enter the password, which can be cumbersome on the long term when connections are frequent. Here is a solution to make the connection automatic.

First, if not already done, you will need to create a local public/private keys. You may choose between 2 versions using either “ssh-keygen” (SSH version 1) or “ssh-keygen -t rsa” (ssh version 2). We consider only the version 2 in this example, which generates 2 files called id_rsa and id_rsa.pub.

cd
cd .ssh
ssh-keygen -t rsa

The last command will ask you for a ‘passphrase’. You can enter an empty passphrase, which is not recommended though.

Second, you need to copy your id_rsa.pub file on the remote host, where you should also find a directory called .ssh in the home.
However, the file id_rsa.pub on the remote host must be renamed into authorized_keys2. Here the number 2 is related to the fact that we used the version 2 of SSH. If you used “ssh-keygen” without option (i.e. version 1) then you should rename id_rsa.pub into “authorized_keys“.

To copy the file on the remote host, type:

scp ~/.ssh/id_rsa.pub user@remotehost:/home/user/.ssh/authorized_keys2

Adapt the code above to your needs by replacing user, remotehost and /home/user path to your needs.

You can now connect to the host without password:

ssh user@remotehost
Posted in Linux | Tagged , | Leave a comment

vim usage: wrapping, spelling, completion, indentation, …

code folding

To fold a number of lines (5 for example) press zf5j.

To fold code within curly brackets use za

The folded lines will be replaced in the buffer with a line that looks like:

+----- 5 lines: }-----

To open the folded text, press zo while the cursor is at the above line.
To close it back, press zc.

Switch case

Use the ~ sign.

Auto indentation

to auto-indent a piece of code, highlight it in visual mode, and press =. To auto-indent the current line, press ==.

Completion

Ctrl+P

Wrapping line

type gqgq : it wraps the current line

Spelling

In your .vimrc configuration file, type

setlocal spell spelllang=en_us

or within vim itself, type

set spell

then, the commands you need to know are :

  • ]s      move to the next mispelled word
  • [s      move to the previous mispelled word
  • zg      add a word to the dictionary
  • zug   undo the addition of a word to the dictionary
  • z=    view spelling suggestions for a mispelled word

deletion

  • x: delete a character
  • 5x: delete 5 characters
  • dw: delete a word
  • dw: delete 5 words
  • dd: delete a line
  • 5dd: delete 5 lines
  • d$ or D: delete all data from the cursor to the end of the line
  •  d0: delete all data from the cursor to the beginning of the line
  • dw and db: delete from cursor to the end or beginnin of the word

others

  •  u. undo
  •  ~: switch small/big caps
Posted in Linux | Tagged | Leave a comment

Python: how to share a local directory on the web

A simple way to share a local directory on internet (temporarily) can be performed with Python. First, go to the directory you want to share and then type:

[admin@localhost Work] python -m SimpleHTTPServer 7000
Serving HTTP on 0.0.0.0 port 7000 ...

Now the entire directory could be accessed on the network by typing http://:7000 in a web browser.

To stop the sharing, just press Ctrl + C .

Example : I want to share my music directory . So I will move to my music directory through cd command and type there python -m SimpleHTTPServer 7000 where 7000 is the port number. Then I will tell the ip of my machine to the person with whom I want to share my data. In the browser he types http://:. Screenshots of the same is shown below :

Posted in Python | Tagged | Leave a comment

Malloc and Casting

In C, the malloc function is used to allocate memory. Its prototype is:

void *malloc(size_t size);

It returns a void pointer (void *), which indicates that it is a pointer to a region of unknown data type therefore it is a type-unsafe behaviour: malloc allocates based on byte count but not on type. In C++, the new operator returns a pointer whose type relies on the operand. So, you should cast the returned pointer to a specific type:

int *ptr;
ptr = malloc(1024 * sizeof (*ptr)); // Without a cast
ptr = (int *) malloc(1024 * sizeof (int)); // With a cast

Advantages to casting

    compatibility with C++, which does require the cast to be made.
    If the cast is present and the type of the left-hand-side pointer is subsequently changed, a warning will be generated to help the programmer in correcting behaviour that otherwise could become erroneous.
    The cast allows for older versions of malloc that originally returned a char *.

Disadvantages to casting

    Under the ANSI C standard, the cast is redundant.
    Adding the cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found:
    in the absence of a prototype for malloc, the standard requires that the C compiler assume malloc returns an int. If there is no cast, a warning is issued when this integer is assigned to the pointer; however, with the cast, this warning is not produced, hiding a bug.

References: wikipedia
See also : allocation 2D arrays in C

Posted in Computer Science | Tagged , | 2 Comments

vim: replace tabs by spaces

In the vim editor, the default behaviour of the tab key is to print a tab … not surprising. When coding, you may prefer tab to be replaced by spaces (because this is your coding style for instance). This feature can be implemented easily by editing (or creating) the special file .vimrc in the home directory. Then, just add these 3 lines:

set tabstop=8
set shiftwidth=8
set expandtab
Posted in Linux | Tagged | Leave a comment