Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, March 14, 2011

Regular expressions in Eclipse: some working examples

I found very useful the Regular Expressions support in the "Find and Replace" window of the Eclipse IDE.

Here some expressions I used. Mainly a reminder for me, of course; but I think they can be useful for others as well, as working examples.

Note that you can refer to groups within the regular expression in the "Search" field, with the symbol $ followed by an increasing number: $1 for the first group defined in the regular expression, $2 for the second and so on...

---

From: qDebug() << "Hello" << world.name() << "t_int32" << "!";
To: ERS_DEBUG(2, "Hello" << world.name() << "t_int32" << "!");

Search: (qDebug[(][)])(\s*[<]+\s*)(.*)([;])
Replace: ERS_DEBUG(2, $3);

---

Thursday, April 2, 2009

Python: Exception TypeError "argument of type 'instance' is not iterable" in 'garbage collection' ignored. Fatal Python error: unexpected exception

Problem: I got a nasty Fatal error message running my Python code: "Exception exceptions.TypeError: "argument of type 'instance' is not iterable" in 'garbage collection' ignored"

Solution: I forgot to use vars() function when checking the existance of a variable in an imported module

-----------------

I got this error in my Python code today, and it took me 20 minutes to understand where the bug was.

Actually the line number showed in the error message was false. The error itself started some lines above and it propagated until the incriminated line.

So this was the error:


Exception exceptions.TypeError: "argument of type 'instance' is not iterable" in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Aborted


And the problem was an "IF" statement where I checked for a variable name in an imported module:

My "IF" was this:


if 'fileName' in myModule:
fileName = 'OutputFile_%s.py' % myModule.fileName


To fix the problem I had to correctly add "vars()", of course ;-)


if 'fileName' in vars(myModule):
fileName = 'OutputFile_%s.py' % myModule.fileName


The bug was very stupid, just forgot the "vars()" function. But it was not so easy to find because the error message Python gave was not so helpful.

Wednesday, April 1, 2009

Eclipse IDE: How to install and configure MinGW GNU C++ compiler for Windows

Eclipse IDE has the very nice CDT environment for C++ programming.

In order to use it under Windows, you have to install a C++ compiler.

Our suggestion is to install the MinGW compiler, "A MINimalistic Gnu for Windows" Gcc compiler.

Go to http://www.mingw.org/ and download the installer for Windows from here.

Then start the Installer and choose to install the basic package + the g77 compiler + Make for MinGW, as those are the tools needed for compiling C++ code with Eclipse... and not only with Eclipse ;-)

Once installed everything you have to add the MinGW path to your PATH environment variable, to make the compiler findable.

Let's say we have installed the MinGW package on C:\MinGW. Refer to the picture below.
You have to open Start --> Control Panel --> System. In the pop up window that appears select on the left side "Advanced System Settings" and then goes to the tab Advanced and choose "Environment Variables...". There look for the "Path" variable in the System Variables window below (look for PATH, instead, if you want to set the MinGW compiler only for you as user) and click on "Edit...". In the pop up window you'll find a long string to edit. Go to the end of that string line and add ";C:\MinGW\bin". Then "Ok" and again "Ok".





Now your new nice MinGW compiler is ready to be used.

To make a test open a new command prompt (Start --> All Programs --> Accessories --> Command Prompt) and try to type "mingw32-make --version". If everything worked fine you should see a window telling you the version of the newly installed package.

Now open your Eclipse IDE with CDT for C++. Eclipse now should see and should be able to use MinGW C++ compiler!! :-)

So...have fun programming C++! :-)

Eclipse: How to get rid of the squiggly orange line in C++ editor

Problem: Using the Eclipse IDE with C or C++ code I have an annoying orange squiggly line throughout my code

Solution: Changing the C/C++ Indexer Markers options


Using the Eclipse IDE to programming C++ code, when an "include file" statement is not resolved, it is marked with an orange question mark and the whole code in the file we are editing are understriked with an orange squiggly line, as shown in the picture below.



It's really annoying, in particular when we are editing a file outside the real environment where the code will be compiled, and we already know that the included files are not physically joinable in that moment ;-)

So, in order to get rid of this squiggly orange line, just open the menu Window --> Preferences --> General --> Editors --> Text editors --> Annotations, look for the orange question mark symbol called "C/C++ Indexer Markers" and check out the "Text as" option, as shown in the figure below




In this way you can keep the orange question mark warning symbol in the Eclipse C++ editor, telling you that the "include file" statement is not working; but you can get rid of this annoying orange squiggly line, as shown in the figure below, and you get back a clean code! :-)

Friday, June 6, 2008

How to measure memory consumption of a program on Linux

The Linux operating system provide detailed information about memory processes use. You can use the ps or the top tools; or you canread information written on the /proc filesystem.

There you can find detailed information for every jobs running on your machine. You can get a list of the running jobs with:


ps -A

and then look, for example, into the file /proc/5995/status, where 5995 is the Job ID:

cat /proc/5995/status

VmSize: 7660 kB
VmLck: 0 kB
VmRSS: 5408 kB
VmData: 4204 kB
VmStk: 20 kB
VmExe: 576 kB
VmLib: 2032 kB

Wednesday, January 9, 2008

VI editor: comment a whole line or N lines

If you want to comment N lines of a Bash script (for example) with the VI (or VIM) editor you have to type in ESC- mode (type ESC before typing the command):


:.,25s/^/# /

where ".,25s" stands for "from the current line to line number 25" and the sequence added to comment a line is "# " (hash plus a white space).
For a single line:

:11s/^/# /

for the 11th line or

:.s/^/# /

for the current line pointed by the cursor.



To uncomment:

:1,$s/^# //

where "$" stands for "last line", i.e. "from line 1 to the end of file".

Print the Copiright Sign in Python

To print the Copiright Sign with Python we need to use the Unicode string literal format.

A quoted string with a "u" or a "U" just before the leading quote is an Unicode string.
The escape sequence "\N{name}" prints the character specified by "name", where "name" is a standard Unicode name, a s listed in www.unicode.org/charts/

Example:

print u'\N{Copyright Sign}'

prints the Copyright sign.

Tuesday, July 10, 2007

Grave errore di stampa del OR logico nel libro C++ di Dietel (Apogeo)

(An English version of this post follows at the end)



Se anche voi, come me, avete ottenuto un errore di compilazione del tipo

stray '\166' in program

...sicuramente state seguendo la versione italiana del libro di Dietel & Dietel "C++ Fondamenti di Programmazione" (seconda edizione, delle altre non so) !!!!! ;-)

In tutto il libro, dopo aver introdotto in modo corretto l'operatore OR logico "||" a pag. 202, si trova il simbolo "¦¦" al posto del "||" !!!!!

E il codice Unicode del simbolo "¦" e' proprio il "166" del messaggio di errore.

Cambiate "¦¦" con "||" e tutto funzionera' a meraviglia! :-)

Ciao!

English:
If you get an error like the red above, you probably input a character "¦" in your code. "166" is the Unicode code of the symbol "¦".
In an italian version of the Dietel book "C++ How to Program" there's a bad typo: in the whole book the logical OR operator is written like "¦¦" instead of "||"!! And if you compile the examples you get the error above.

Wednesday, May 2, 2007

PyDev + Eclipse Error Message. How to configure the Python interpreter

If you installed the PyDev plugin into Eclipse, you may get this message when you start up Eclipse:

An internal error occurred during: "PyDev code completion: rebuilding modules"

It means that, during the rebuilding of the Code Completion Tool, the Python interpreter has not been found. It happens when your Python system was not installed on the default folder (c:\Python); for example mine is installed in D:/Programs/Python. Yes I know, it's not a good practice and it's not suggested in the Python documention.


Solution:

How to set the Python system PYTHONPAT

Being into Eclipse you have to enter in "Window" menu, than "Preferences", "Pydev", "Interpreter-Python" and here you can configure your path. Click on "New..." on the left of "Python interpreters" window, and then choose your right python folder. In the shown up list, check every folder within your main python folder and go on.
Restart Eclipse.

P.S. You could also disable the "Code Completion Tool" rebuilding, but you loose this nice feature. Anyway, if you want to disable it, you can look into "Window-Preferences-Pydev-Builders" menu.

Sunday, April 29, 2007

A nice article on ROOT, Python, PyROOT, C++, C# and IronPython and perfomaces

While I was searching the web to get information on PyROOT, I get this nice article written by another particle physicist guy :)

He also did a little performance comparison about ROOT calls wrapped in C++, pure Python and IronPython code.


http://gordonwatts.wordpress.com/2007/02/25/root-python-and-net/