def ast_to_callable(ast_tree):
    """ Convert an ast containing a single function definition into a callable
    version of that function.

    This is the most magical part of this whole program.
    `compile` takes the AST tree and turns it into a code
    object which contains compiled python ready to execute
    but it isn't directly callable. `exec` takes the code object and
    runs it. Since the code object contains a function definition, executing
    it is equivelent to redefining the function. That means that there is now
    a new version of the function defined in this scope.

    Think of this like changing a cell in a jupyter notebook and rerunning it.
    """

    code_object = compile(ast_tree, 'magic_land.py', 'exec')
    exec(code_object)

    # More magic to actually find the new function so we can return it.
    # This is why you normally try not to write code
    # that uses a lot of introspection and metaprogramming.
    function_name = code_object.co_consts[0].co_name
    function = locals()[function_name]

    return function