def create_function_from_source(function_source, imports=None):
 ns = {}
 import_keys = []
 try:
  if (imports is not None):
   for statement in imports:
    exec statement in ns
   import_keys = list(ns.keys())
  exec function_source in ns
 except Exception as e:
  msg = (u'\nError executing function:\n %s\n' % function_source)
  msg += u'\n'.join([u'Functions in connection strings have to be standalone.', u'They cannot be declared either interactively or inside', u'another function or inline in the connect string. Any', u'imports should be done inside the function'])
  raise_from(RuntimeError(msg), e)
 ns_funcs = list((set(ns) - set((import_keys + [u'__builtins__']))))
 assert (len(ns_funcs) == 1), u'Function or inputs are ill-defined'
 funcname = ns_funcs[0]
 func = ns[funcname]
 return func

def create_function_from_source ( function_source , imports = None ) : ns = { } import_keys = [ ] try : if ( imports is not None ) : for statement in imports : exec statement in ns import_keys = list ( ns . keys ( ) ) exec function_source in ns except Exception as e : msg = ( u'\nError executing function:\n %s\n' % function_source ) msg += u'\n' . join ( [ u'Functions in connection strings have to be standalone.' , u'They cannot be declared either interactively or inside' , u'another function or inline in the connect string. Any' , u'imports should be done inside the function' ] ) raise_from ( RuntimeError ( msg ) , e ) ns_funcs = list ( ( set ( ns ) - set ( ( import_keys + [ u'__builtins__' ] ) ) ) ) assert ( len ( ns_funcs ) == 1 ) , u'Function or inputs are ill-defined' funcname = ns_funcs [ 0 ] func = ns [ funcname ] return func
return a function object from a function source parameters function_source : pickled string string in pickled form defining a function imports : list of strings list of import statements in string form that allow the function to be executed in an otherwise empty namespace .