hy

Convert python literal_eval string to hy


I would like to convert the following to Hy, but I can't seem to figure out how to do it; is there perhaps a way to convert a string of python code to the hy syntax? I don't know if py would work because the evaluated result of f_back may not be safe.

import ast, inspect
def test(f_back):
    return ast.literal_eval(f"inspect.currentframe(){'.f_back' * f_back}")

Solution

  • You could always evaluate x = x.f_back repeatedly in a loop instead of using metaprogramming for this.

    To address the more general question, Hy itself only implements the Hy-to-Python direction, not Python to Hy. There's Hikaru Ikuta's py2hy, but it was never updated to work with newer versions of Hy.