Thursday, August 27, 2015

eval,exec, execfile, compile and py_compile in python

Importance of eval, exec, execfile, compile; and the py_compile module:

This function executes an expression string and returns the result.

>>> eval('2+3')
>>> eval("'udhay'*3")

#exec statement executes a string containing arbitrary python code
>>> exec("print 'Hello'")
>>> exec('2+34')
>>> a=[1,2,3,45]
>>> exec "for i in a: print i"                                                                                                                      
>>> #execfile(filename,globals,locals) -function executes the contents of a file
>>> execfile("")

In [1]: globals={'x':7,'y':10,'birds':['parrot','pigeon','sparrow']}

In [2]: locals={}                                                                                                                                    

In [3]: a=eval("3*x+4*y",globals,locals)

In [4]: a                                                                                                                                            
Out[4]: 61

In [5]: exec "for b in birds: print b" in globals,locals


# when a string is passed to exec,eval(), or execfile(), parser first compiles to create bytecode.

# To remove this redundant process every time, compile will create precompiled bytecode,
# which can be used everytime, till the code is not changed

#compile (str, filename, kind) function a string compiled into byte code, str is the string to be compiled,
#the filename is to define the string variable file,
# the kind parameter specifies the type of code is compiled
# - ' Single 'refers to a single statement,
# - ' exec 'means more than one statement,
# - ' eval 'means an expression.

#compile () function returns a code object, the object, of course, can also be passed to the eval () function
#and the exec statement to perform, for example, :

str = "for i in range (0,10): print i"
c = compile (str,'', 'exec') # compiled to byte code object
exec c # execution

str2 = "3 * x + 4 * y"
c2 = compile (str2,'', 'eval') # compiled expression

py_compile - It is a module to create bytecode file, .pyc
In [14]: import py_compile

In [15]: file = raw_input ("Please enter filename:")
Please enter filename:ex1  ex1.pyc  
Please enter

In [16]: py_compile.compile (file);            

In [17]: ls *.pyc                                                                                                                                    

In Python 2.x input(...) is equivalent to eval(raw_input(...)), in Python 3.x raw_input was renamed input
>>> a=input()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'udha' is not defined
>>> udha
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'udha' is not defined
>>> a=input()
>>> a=raw_input()

Initiating static local HTTP Server using different scripting languages

Initiating HTTP Server using different scripting languages ---------------------------------------------------------- Python -------...