import traceback


def myfunc(f, args):
    ''' Run inline Python code '''
    # check to make sure no import calls are made
    params = ", ".join(['x', 'y', 'z'][:len(args)])
    myf = ''.join(['def f(', params, '): return ', f.replace('import', '')])
    userdefined = {}
    exec(myf, globals(), userdefined)
    return list(userdefined.values())[0](*args)


def myfunc_import(parent, f, args):
    ''' Run Python code imported from Journal '''
    if 'def myblock(lc,' in f:
        base_class = parent.tw.lc  # pre-v107, we passed lc
    else:
        base_class = parent.tw  # as of v107, we pass tw
    userdefined = {}
    try:
        exec(f, globals(), userdefined)
        return userdefined['myblock'](base_class, args)
    except BaseException:
        traceback.print_exc()
        return None
