1. compile
#!/usr/bin/env python
eval_code = compile("100 + 200", "", "eval")
print eval(eval_code)
single_code = compile("print 'Hello World'", '', "single")
exec single_code
exec_code = compile("""
req = input('Count how many numbers?')
for eachNum in range(req):
print eachNum
""", '','exec')
exec exec_code
2.
#! /usr/bin/env python
def foo():
return True
def bar():
'bar() does not much'
return True
foo.__doc__ = 'foo() does not do much'
foo.tester = '''
if foo():
print 'PASSED'
else:
print 'FAILED'
'''
for eachAttr in dir():
obj = eval(eachAttr) #convert the string to the object
if isinstance(obj, type(foo)):
if hasattr(obj, '__doc__'):
print '\n Function "%s" has a doc string:\n\t%s' %(eachAttr, obj.__doc__)
if hasattr(obj, 'tester'):
print 'Function "%s" has a tester...executing ' %eachAttr
exec obj.tester #to execute the strings
else:
print 'Funciton "%s" has no tester...skipping' %eachAttr
else:
print '"%s" is not a Function' %eachAttr
print '________________dir()__________________'
print dir()
3.
globals/locals[……]