Make an executable script

Add the following line at the beggining of the file:


and make it executable with

chmod a+x <file>

To check if the file is being executed from de command line or is being imported as a module:

if __name__ == "__main__":
    print "Invocado desde linea de comando."
    print "Importado desde otro modulo o desde el interprete."

Howtos for the Python interpreter

Reload a module:

>>> reload(modulo)

List the attributes of an object (without the methods):

>>> vars(o).keys()

Evaluate a string as a Python expression

>>> eval('[(0,-1), (1,0), (2,1), (3,2), (4,3)]\n')
[(0, -1), (1, 0), (2, 1), (3, 2), (4, 3)]

Simple beep

>>> print '\a'

A lot of beeps:

>>> print '\a'*100

Measure execution time


On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms.
>>> import time
>>> t0 = time.clock()
... hacer algo ...
>>> t = time.clock() - t0

For small code snippets use the timeit module. To do profiling read The Python Profilers.