I wrote a lambda function to procedurise calls to a dynamically loaded library but I seem to be coming up against namespace problems with respect to the import. My testcase is below:
def dot(*args):
''' return a dot notation string
'''
return '.'.join(map(str, args))
def test(import_name, classname):
exec('import ' + import_name)
get_global = lambda glob: eval(dot(import_name, classname, glob))
technodename = get_global('technodename')
metalstack = get_global('metalstack')
return technodename, metalstack
print(test('PROJECT', 'Quabanatu'))
the error which comes when executing the lambda function is:
NameError: name 'PROJECT' is not defined
however if I execute:
technodename = eval(dot(import_name, classname, 'metalstack' ))
instead of calling get_global it works just fine. Also if I import the PROJECT library at the start of the program the lambda function works fine. What am I missing?
BTW I know this is not the best way of doing it in this case since 'technodname' is a constant but in some of my code I am forced to look up a class variable based on another variable hence the need to use the eval function.