Wednesday, July 1, 2009

Welcome People!

Welcome to my blog dedicated to Matlab, C and C++.
There will be some useful stuff here, just wait for it and subscribe ;)

Ciao!

Friday, May 29, 2009

How to get the current working directory in Python

In Python you can get the current directory using the function getcwd from the os module. The output is displayed as a string. Basically corresponds to the pwd command that you can use in a Unix terminal.

PS
As you can guess, getcwd stands for get current working directory


>>>>import os
>>>>os.getcwd()

Tuesday, May 5, 2009

Mac OS X: How to clear the terminal as in Windows

How do you clear the terminal in Mac OS X in a similar way as you can do in Windows with the command cls?

If you use clear in the Mac OS X terminal (and also on Linux) what you  get is a simple page scrolling. On the other hand, in the windows terminal the command cls works actually cleaning all the previously entered commands!

To clean the Mac-terminal the  Windows-way you have to use the keyboard shortcut Command+K
That's it! Enjoy!

Sunday, May 3, 2009

Print %s and %r in Python [Python 2.x]

What are the differences between %s and %r options in Python?

# Python 2.x code
string1='some text'
string2r="This string contains %r"
string2s="This string contains %s"

print string2r %string1
print string2s %string1


------------------------------------------------
Explanations

%r converts any Python object using repr()
%s converts any Python object using str().

repr() gives you the canonical representation of an object, i.e. the way you have to write in your file (strings must have at least apostrophes ); On the other hand,  str() gives you a nice representation of an object,  and this, for a string, means no apostrophes!


References:
Learn Python the hard way
http://docs.python.org


Notes on print [Python 2.x]

Python 2.5x

Attention check the differences in these  instructions involving print

1 my_name="eddie"
2 print "My name is %s" %(my_name)  # This is OK
3 print "My name is %s" %(my_name) # This will give you an error
4 print "My name is %s", my_name  # This is OK but the output is not equal to line 2 (power of comma!!!)