How to name a Python file

Every Python source file can essentially be considered as a module. The following rule applies to the name of a module, if it needs to be imported without problems:

(letter|"_") (letter | digit | "_")*

That is, the rule for naming a Python file is the same as naming a variable in it. First character has to be letter or underscore, followed by any number of letters, digits or underscores. No other characters, like dash, are allowed.

If the file is not named correctly, the following error or warning might be generated on importing it:

Invalid name for Python module

Tried with: Python 2.7.6 and Ubuntu 14.04

How to read from binary file in Octave

The fopen and fread functions can be used to read binary data from a file. Assume, we have stored the values of a matrix of 100 rows and 100 columns in binary format in a file. Such a file could be produced from a C or C++ program. Let us also assume that that data type of the values is short of 2 bytes.

To read the data from this binary file and reconstruct the original matrix:

> bfile = fopen('matdata.bin');
> bdata = fread(bfile, 'short');
> size(bdata)
ans =
  10000 1
> B = reshape(bdata, 100, 100);

These functions are from Matlab and so will work in it too.

Tried with: Octave 3.8.1 and Ubuntu 14.04

The file program

file is one of those Linux utilities that is not popular, but useful to know. It reads the magic pattern of a file and determines the type of the file. It might be useful to try on files that you come across with no extensions or with extensions that you are not familiar with.

Tried with: Ubuntu 12.04 LTS

How to find the process that has locked a file or directory in Ubuntu

When you open a program in Ubuntu, it might quit complaining that another process has locked a file or directory it is trying to open.

For example, here is an error of this kind:

E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
E: Unable to lock the administration directory (/var/lib/dpkg/), is another process using it?

To continue, you might have to find the offending process and stop it. The lsof command can be used for this purpose.

For example, to find out which process has locked the /var/lib/dpkg directory in the above example:

 $ lsof | grep /var/lib/dpkg

Tried with: Ubuntu 12.04.1 LTS

Agent Ransack: Search inside Office, PDF and other files

There is no grep or ack on Windows. Windows 7 has a good search engine that can be used to search for text inside files. However, it requires indexing to be turned on for this feature. I do not like the indexing happening in the background nor do I like indexed information to be stored before I search for something.

As a good grep replacement, I like Agent Ransack. It can search for a particular text in text files and files of other formats. It builds the index on the fly and does not store it for future use. It includes support for searching inside popular binary file formats like Microsoft Office, Libre Office and PDF.

Tried with: Agent Ransack (Release Build 762 – 2010.03.29.47911)